We want to generate a function that computes the series starting from 0 and ending until the given number following the sequence:
0 1 3 6 10 15 21 28 36 45 55 .... which is created by 0, 0+1, 0+1+2, 0+1+2+3, 0+1+2+3+4, 0+1+2+3+4+5, 0+1+2+3+4+5+6, 0+1+2+3+4+5+6+7 etc.. Input: LastNumber Output: result Example: Input: 6 Output: 21 Input: -15 Output: 0Tags JAVA languages
Subscribe Our Newsletter
import java.io.*;
ReplyDeleteimport java.util.Scanner;
public class TestClass {
public static void main(String[] args) {
int sum=0;
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(int i=0;i<=n;i++)
{
sum=sum+i;}
System.out.println(sum);
}
}