Assignment #88 Adding Values with a For Loop
Code
/// Name: Matthew Lorence
/// Period: 5
/// Program Name: AddingValues
/// File Name: AddingValues.java
import java.util.Scanner;
public class AddingValues
{
public static void main (String[] args)
{
Scanner keyboard = new Scanner (System.in);
System.out.print("Number: ");
int n = keyboard.nextInt();
int y = 0;
for ( int x = 1; x<=n; x = x + 1 )
{
System.out.print(x + " ");
y = y + x;
}
System.out.println("\nThe sum is " + y + ".");
}
}
Picture of the output