Java program to find sum of all natural nubmers from 1 to 100
public class Main {
public static void main(String[] args) {
// 1+2+3+4+5+.............. +100 ==> 5050
int num = 100;
int sum = 0;
//1st method using for loop
for(int i=1; i <= num; i++) {
sum = sum+i;
}
System.out.println("Sum of Natural Number is: " + sum);
System.out.println("------------------------------------------");
//2nd method using while loop
int sumNumber = 0;
int k = 1;
while(k <= num) {
sumNumber = sumNumber + k;
k++;
}
System.out.println("Sum of Natural Number is: " + sumNumber);
}
}