Assignemnt #88: Adding Values With A For Loop
Code
/// Name: Kelsey Lieberman
/// Period 5
/// Program Name: AddingValuesWithAForLoop
/// File Name: AddingValuesWithAForLoop.java
/// Date Finished: 1/7/2016
import java.util.Scanner;
public class AddingValuesWithAForLoop
{
public static void main(String[] args)
{
Scanner key = new Scanner(System.in);
int num, sum = 0;
System.out.println("Type in a number and I will add all the numbers counting up to that number together!");
System.out.print("Number: ");
num = key.nextInt();
for (int x = 1; x <= num; x++)
{
System.out.print( x + "\t");
sum = (sum + x);
}
System.out.println("\nThe total is: " + sum );
}
}
Picture of the output