Assignemnt #88, Adding Values with a For Loop

Code

import java.util.Scanner;

public class prog88
{
    public static void main( String[] args )
    {
        Scanner keyboard = new Scanner(System.in);
        
        System.out.print( "Number: " );
        int x = keyboard.nextInt();
        int y = 0;
        
        for ( int n = 1; n <= x; n = n+1 )
        {
            System.out.print( n + " " );
            y = x+y;
            
            if ( n == x )
                System.out.println( " " );
        }
        
        System.out.println( "The sum is " + y + "." );
    }
}
    

Picture of the output

Assignment 88