import java.util.Scanner;
// Sum of first N natural Numbers , Naymul Islam CSE 18
public class sumofN {
public static void main(String args[]) {
System.out.print("Enter Natural Number: ");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int sum = 0;
int i = 1;
while (i <= n) {
sum += i;
i++;
}
// System.out.println(sum);
System.out.println("Sum of first " + n + " natural Numbers: " + sum);
}
}
import java.util.Scanner;
// Sum of first N natural Numbers
public class sumofN {
public static void main(String args[]) {
System.out.print("Enter Natural Number: ");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int sum = 0;
int i = 1;
while (i <= n) {
sum += i;
i++;
}
// System.out.println(sum);
System.out.println("Sum of first " + n + " natural Numbers: " + sum);
}
}
No comments:
Post a Comment