Write a JAVA program that reads a set of integers, and then prints the sum of the even and odd integers.

 import java.util.Scanner;


public class ReadSetofIntegersPrintsSumofEvenAndOddIntegers {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int number, chioce, evenSum = 0, OddSum = 0;

        do {

            System.out.print("Enter the number ");

            number = sc.nextInt();


            if (number % 2 == 0) {

                evenSum += number;

            } else {

                OddSum += number;

            }

            System.out.println("Do you want to continue? Press 1 for yes or 0 for no");

            chioce = sc.nextInt();

        } while (chioce == 1);

        System.out.println("Sum of even numbers: " + evenSum);

        System.out.println("Sum of odd numbers: " + OddSum);

    }

}

import java.util.Scanner;

public class ReadSetofIntegersPrintsSumofEvenAndOddIntegers {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int number, chioce, evenSum = 0, OddSum = 0;
        do {
            System.out.print("Enter the number ");
            number = sc.nextInt();

            if (number % 2 == 0) {
                evenSum += number;
            } else {
                OddSum += number;
            }
            System.out.println("Do you want to continue? Press 1 for yes or 0 for no");
            chioce = sc.nextInt();
        } while (chioce == 1);
        System.out.println("Sum of even numbers: " + evenSum);
        System.out.println("Sum of odd numbers: " + OddSum);

    }
}



Output:
Enter the number 4 Do you want to continue? Press 1 for yes or 0 for no 1 Enter the number 6 Do you want to continue? Press 1 for yes or 0 for no 1 Enter the number 8 Do you want to continue? Press 1 for yes or 0 for no 1 Enter the number 9 Do you want to continue? Press 1 for yes or 0 for no 0 Sum of even numbers: 18 Sum of odd numbers: 9

No comments:

Post a Comment

Max Sub Array Sum: Kadane's Algorithm

Code of: Max Sub Array Sum: Kadane's Algorithm. public class MaxSubArraySumKadanesALgorithm {     public static void Kadane ( int ...