• Register
Welcome to Kodlogs, programming questions and answer website.
0 votes
20 views
write a program that asks the user for a number n and prints the sum of the numbers 1 to n
by (720 points)  

1 Answer

0 votes

SOLUTION:
import java.util.Scanner;
public class Num{

public static void main(String[] args){

int sum=0;

Scanner sc=new Scanner(System.in);

System.out.println("Enter the number:");

int n=sc.nextInt();

for(int i=1;i<=n;i++){

sum=sum+n;

}

System.out.println("The sum from 1 to"+n+"is:"+sum);

}

}

Example:

input:5
output: The sum from 1 to 5 is:15

by (140 points)  
...