Write a complete Java program, that will do the following: Read in a number and
ID: 3936977 • Letter: W
Question
Write a complete Java program, that will do the following: Read in a number and a character. If the character is an 'a' or 'A" add 2 to the number and output "2 added to the number is "; If the character is 's' or a 'S" subtract 2 with a similar output to 2) IF the character is 'm' or 'M' multiply by 2 with a similar output to 2) If the character is anything else, output wrong number. Use a while statement and stop when an 'x' or 'X" is entered the loop stops. Count how many add, subtracts and multiplications and then the total legal characters were entered and print the results out after the loop is finished Add all of the adds, subtract, and multiplication results, find the total sum of all and then their average. Write a program to read in numbers and add them up and find their average. The program stops asking for input once a negative number is entered. Use a whileExplanation / Answer
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)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.