Write a Java program that reads a set of integers, and then prints the sum of the even and odd integers using loop


Introduction

In this demo I have used NetBeans IDE 8.2 for debugging purpose. But you can use any java programming language compiler as per your availability..


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

  1. import java.util.Scanner;
  2.  
  3. public class JavaLoopExcercise
  4. {
  5. public static void main(String[] args)
  6. {
  7. Scanner console = new Scanner(System.in);
  8.  
  9. int number;
  10. char choice;
  11. int even = 0;
  12. int odd = 0;
  13.  
  14. do
  15. {
  16. System.out.print("Enter any number ");
  17. number = console.nextInt();
  18.  
  19. if( number % 2 == 0)
  20. {
  21. even += number;
  22. }
  23. else
  24. {
  25. odd += number;
  26. }
  27.  
  28. System.out.print("Do you want to continue y/n? ");
  29. choice = console.next().charAt(0);
  30.  
  31. }while(choice=='y' || choice == 'Y');
  32.  
  33. System.out.println("Sum of even numbers: " + even);
  34. System.out.println("Sum of odd numbers: " + odd);
  35. }
  36. }

Result

java techstudy.org

     





Related Topics




Popular Posts