73.6k views
5 votes
Create an application that prompts the user for a number and then displays the numbers 1 through the number entered, each on a separate line. Below the numbers, the sum is displayed.

2 Answers

3 votes

Answer:

import java.util.Scanner;

public class NumberSum {

public static void main(String[] args) {

// TODO Auto-generated method stub

int num, sum=0,num1=0;

Scanner input=new Scanner(System.in);

System.out.println("Enter a number");

num=input.nextInt();

input.close();

do{

System.out.println(num1);

num1+=1;

sum+=num1;

}while(num1<=num);

System.out.println(sum);

}

}

User Kieveli
by
3.8k points
7 votes

Answer:

In Python:

start = num(input("Enter a number: "))

sum = 0

for x in range(1, start+1):

sum += x

print (x)

print ("Final Sum: " + sum)

User Sanchit Bhatnagar
by
4.0k points