Write a complete Java program that will do the following Rea
Solution
import java.io.IOException;
import java.util.Scanner;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Surya
*/
public class NumberCharcter {
public static void main(String argv[]) throws IOException
{
//variable declaration
int total_sum=0,total_validChar=0,num;
char c=\'z\';
Scanner sc = new Scanner(System.in);
float average;
//entering into the loop
while(true)//loop breaks when c=x or X
{
System.out.print(\"Enter a number:\");
num = sc.nextInt();//taking number as input
System.out.print(\"Enter a Character:\");
c = sc.next().charAt(0);//taking character
if(c==\'x\'||c==\'X\')break;
if(c==\'a\'||c==\'A\')//adding of 2
{
total_validChar++;
num = num+2;
System.out.println(\"2 added to the number is: \"+num);
total_sum = total_sum+num;
}
else if(c==\'s\'||c==\'S\')//subtracting of 2
{
total_validChar++;
num = num-2;
System.out.println(\"2 subtracted to the number is: \"+num);
total_sum = total_sum+num;
}
else if(c==\'m\'||c==\'M\')//multiplying of 2
{
total_validChar++;
num = num*2;
System.out.println(\"2 Multiplied to the number is: \"+num);
total_sum = total_sum+num;
}
else//invalid character error message notifying
{
System.out.println(\"Wrond Number..enter correct char\ \");
}
}
System.out.println(\"The total legal charcters entered are: \"+total_validChar);//printing results
System.out.println(\"The total sum of all results:\"+total_sum);
average = (float)total_sum/(float)total_validChar;
System.out.println(\"The Average all results:\"+average);
}
}
ouput:-
run:
Enter a number:5
Enter a Character:a
2 added to the number is: 7
Enter a number:6
Enter a Character:m
2 Multiplied to the number is: 12
Enter a number:4
Enter a Character:s
2 subtracted to the number is: 2
Enter a number:3
Enter a Character:x
The total legal charcters entered are: 3
The total sum of all results:21
The Average all results:7.0
BUILD SUCCESSFUL (total time: 18 seconds)
import java.util.Scanner;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Surya
*/
public class negative {
public static void main(String argv[])
{
//variable declarations
int num,sum=0,c=0;
Scanner sc = new Scanner(System.in);
float average;
while(true)//loop
{
System.out.print(\"Enter number:\");//asking user input
num =sc.nextInt();
if(num<0)break;//if negative number then stops
sum =sum+num;//summing all numbers
c++;//counting numbers
}
average = (float)sum/(float)c;
System.out.println(\"The average of the given all numbers:-\"+average); //printing average
}
}
output:-
run:
Enter number:4
Enter number:2
Enter number:7
Enter number:9
Enter number:5
Enter number:-3
The average of the given all numbers:-5.4
BUILD SUCCESSFUL (total time: 8 seconds)


