Find the Sum of Natural Numbers Java
PROGRAM TO FIND THE SUM OF N NATURAL NUMBERS
public class SumNatural {
public static void main(String[] args) {
int num = 50, i = 1, sum = 0;
while(i <= num)
{
sum += i;
i++;
}
System.out.println("Sum = " + (num*(num+1)) / 2);
System.out.println("Sum = " + sum);
}
}
OUTPUT
Sum = 1275
Sum = 1275
Comments
Post a Comment