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: Sum of n numbers

Write a program to sum up all the positive numbers from n1 to n2, both inclusive and store it in a variable n. Your program should print the message "The sum of n2 - n1 numbers is " followed by the value of the sum (n).

corejava x 352
programming x 168
Posted On : 2016-09-14 18:48:41.0
profile Glad - anyforum.in Glad
1500
up-rate
3
down-rate

Answers


public class SumNSeries {
public static void main(String[] args) {
int n1=1;
int n2=14;
/*as we studied sum of n terms in 11th standard=n*(2*a+(n-1)*d)/2
* a=n1;
* n=n2-n1+1;
* d=1
* */
System.out.println("the sum of n2-n1 numbers is "+(n2-n1+1)*(n1+n2)/2);
}
}

Posted On : 2016-09-15 00:05:20
Satisfied : 2 Yes  12 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523188221600
Reply This Thread
up-rate
0
down-rate



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