Write a program to print first ten natural numbers in java

Write a program to print first ten natural numbers in java. In this program you will learn how to write a program to sum of the natural numbers in java for beginners. In Java programming language, the sum of natural numbers. In the Java programming language, this is an example of a simple set program. The sum of natural numbers program is essentially an addition program that is run in a for loop

public class Main
{
public static void main(String[] args)
{
int number = 10, sum = 0;
for(int i = 1; i <= number; ++i)
{
sum =sum + i;
}
System.out.println("Sum = " + sum);
}
}

 

Output:

Sum = 55

What is for loop and how it works

A for-loop is a control flow statement in computer science that specifies iteration and allows code to be performed repeatedly. When the number of iterations is known before entering the loop, for-loops are commonly employed. The startup of the loop, when we set our counter to a beginning value. Before the loop starts, the initialization statement is executed. The test statement determines whether or not a particular condition is true.

What is natural number in java

Natural numbers are a subset of the number system that encompasses all positive integers from 1 to infinity and are used for counting. It does not include the number zero (0). 1,2,3,4,5,6,7,8,9,… are also known as counting numbers. Natural numbers are a subset of real numbers, which only contain positive integers such as 1, 2, 3, 4,5, and 6, as well as zero, fractions, decimals, and negative numbers.

Print first ten natural in java

Printing the natural numbers in java programming language will make you understand the concept of for loop in java.