In: Computer Science
Write a JAVA program that allow a user to enter 2 number, starting point and end point. For example a user me enter 1 and
100. Your program will
Add numbers from 1 to 100, add all the even number, and add all the odd numbers
Output:
The sum of number 1 to 100 is:
The sum of even number 1 to 100 is:
The sum of odd number 1 to 100 is:
import java.util.Scanner;
public class AddNumbers {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter starting number: ");
int start = in.nextInt();
System.out.print("Enter ending number: ");
int end = in.nextInt();
int sum = 0, sumOdds = 0, sumEvens = 0;
for (int i = start; i <= end; i++) {
sum += i;
if (i % 2 == 0) {
sumEvens += i;
} else {
sumOdds += i;
}
}
System.out.println("The sum of number " + start + " to " + end + " is: " + sum);
System.out.println("The sum of even number " + start + " to " + end + " is: " + sumEvens);
System.out.println("The sum of odd number " + start + " to " + end + " is: " + sumOdds);
}
}
