Friday, June 18, 2021

program in Java to display n terms of natural numbers and their sum (Java) (189)

package com.company;
import java.util.Scanner;
public class Program26 {
public static void main(String[] args) {
Scanner put = new Scanner(System.in);
System.out.println("please enter the number of terms:");
int n = put.nextInt();
int sum=0;
for(int i=1;i<=n;i++)
{
System.out.println(i);
sum=sum+i;
}
System.out.print("total sum is:");
System.out.println(sum);
}
}

No comments:

Post a Comment