- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSumOfNumbers.java
More file actions
Latest commit
43 lines (35 loc) · 1.29 KB
/
Copy pathSumOfNumbers.java
File metadata and controls
43 lines (35 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//SumOfNumbers.java
importjava.util.Scanner; // Need for the scanner class
/**
This program askes for a positive non-zero integer.
Once an integer is provided, it will calculate the sum
of that number with each integer from 1 to 50 and display
the results.
*/
publicclassSumOfNumbers
{
publicstaticvoidmain(String[] args)
{
intuserNumber; // A number entered by the userNumber
intcount; // The number of iterations to control the loop
intsum; // A sentinal sum
// Create a new Scanner
Scannerkeyboard = newScanner(System.in);
// Ask the user for a number
System.out.println("This program will sum a number you enter " +
"to the integers 1 through 50:");
System.out.println("Enter a positive number: ");
userNumber = keyboard.nextInt();
// Print an interface
System.out.println();
System.out.println("------------------------");
System.out.println(" Number + Integer = Sum ");
System.out.println("------------------------");
// Calculate and display the sum the userNumber with integers 1 to 50
for (count = 1; count <= 50; count++)
{
sum = userNumber + count;
System.out.printf("%-9d%-9d%-9d\n", userNumber, count, sum);
}
}
}