write aprogram that prompts the user to enter fourn umbers t

write aprogram that prompts the user to enter fourn umbers. the numbers should be added and if the sum of the numbers is evenly divisible by 2 then display the sum. the loop should ask the user if he or she wishes to perform the operation again. if so, the loop should repeat, otherwise it should terminate.( use do while loop)

Solution

SumNumbers.java

import java.util.Scanner;


public class SumNumbers {

  
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       int n = 4,i=0;
       int sum = 0;
       char ch=\'\\0\';
       do{
           do{
               System.out.print(\"Enter a number: \");
               int num = scan.nextInt();
               sum = sum + num;
               i++;
           }while(i<n);
           if(sum % 2 == 0){
               System.out.println(\"Sum is \"+sum);
           }
           System.out.print(\"Do you want to continue (y or n): \");
           ch = scan.next().charAt(0);
           i = 0;
           sum=0;
       }while(ch != \'n\' && ch != \'N\');
   }

}

Output:

Enter a number: 1
Enter a number: 2
Enter a number: 3
Enter a number: 4
Sum is 10
Do you want to continue (y or n): y
Enter a number: 2
Enter a number: 3
Enter a number: 4
Enter a number: 5
Sum is 14
Do you want to continue (y or n): y
Enter a number: 1
Enter a number: 2
Enter a number: 3
Enter a number: 5
Do you want to continue (y or n): n

write aprogram that prompts the user to enter fourn umbers. the numbers should be added and if the sum of the numbers is evenly divisible by 2 then display the

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site