Assignment = Adding Values With a For Loop

Code

  /// Name: Sam Wilson
  /// Period: 6
  /// Program Name: Adding Values With a For Loop
  /// File Name: addingfor.java
  /// Date Finished: 3/1/16
 
  
import java.util.InputMismatchException;
import java.util.Scanner;

public class addingfor
{

	public static void main(String[] args)
	{
		Scanner keyboard = new Scanner(System.in);
		int num = 0;
		int total = 0;

		try {
			System.out.print("Number: ");
			num = keyboard.nextInt();
		} catch (InputMismatchException var5) {
			System.out.println("WRONG INPUT!!!");
		}

		System.out.println();

		for(int i = 0; i < num; ++i) {
			System.out.print(i + 1 + " ");
			total += i + 1;
		}

		System.out.println();
		System.out.println("The sum is " + total);
	}
}

    

Picture of the output

Assignment 31