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