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

Kindle Paperwhite High Resolution Display Tablet | Rs. 9999

  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


Responses




Popular Posts

Facebook

Founded in 2016, Tech Study passionately delivers stylish and dynamic innovative information to programmer and technology lovers. http://www.techstudy.org/.