Computer Science, asked by koushal53, 8 months ago

Write a java program to find the sum of all natural numbers from 1 to n using for loop

Answers

Answered by shivanikhanna
7

Answer:

public class SumOfNaturalNumber1

{

public static void main(String[] args)

{

int i, num = 10, sum = 0;

//executes until the condition returns true

for(i = 1; i <= num; ++i)

{

//adding the value of i into sum variable

sum = sum + i;

}

//prints the sum

Answered by expresspanda987
0

Answer:

Hope it helps you!!

Explanation:

SumOfNaturalNumbers

{

public static void main(string args[])

{

int i, num=10,sum=0;

for (i = 1; i<=num;++i)

{

sum=sum+i ;

}

System.out.println("Sum of first natural numbers is ="+sum);

}

}

Similar questions