In this example we will see how to get sum of input numbers and then compare it to 100. First we will request from the user to write how many numbers need to be added. Then we make a for cycle in which we will get every number from the user input and add it to the sum numberSum. At the end we will check if numberSum is bigger than 100 with if statement.
package org.example; import java.util.Scanner; public class Main { public static void main(String[] args) { System.out.println("How many numbers we will add?"); Scanner scanner = new Scanner(System.in); int numberCount = scanner.nextInt(); System.out.println("Please write " + numberCount + " numbers "); int numberSum = 0; for (int i = 0; i < numberCount; i++) { int oneNumber = scanner.nextInt(); numberSum = numberSum + oneNumber; } if (numberSum > 100) { System.out.println("Sum is " + numberSum + " is bigger than 100"); } else { System.out.println("Sum is " + numberSum + " is smaller than 100"); } } }
How many numbers we will add?
4
Please write 4 numbers
1
99
2
3
Sum is 105 is bigger than 100