AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



corejava-programming: SumNum in java

Write a program to sum up all the positive numbers from n1 to n2 (n1<=n2), both inclusive and store it in a variable n. Your program should print the message "The sum of t numbers is " followed by the sum of given numbers Sample input: 2 10 Sample output: The sum of 9 numbers is 54

corejava x 336
programming x 153
Posted On : 2016-10-21 00:06:15.0
profile Aarti Kumar - anyforum.in Aarti Kumar
5900
up-rate
4
down-rate

Answers


import java.util.Scanner;

public class SumNum {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int n1=input.nextInt();
int n2=input.nextInt();
int n=0;
for(int i=n1;i<=n2;i++){
n=n+i;
}
System.out.println("The sum of "+(n2-n1+1)+" numbers is "+n);
}
}

Posted On : 2016-10-21 22:48:07
Satisfied : 1 Yes  0 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523185319357
Reply This Thread
up-rate
3
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in