write a program in java to print natural numbers from 1 to 50
Answers
Answer:
First, we used the Java For loop to iterate from 1 to maximum value (Here, number = 6).
User entered value: number = 6
For Loop First Iteration: for(i = 1; i <= 6; i++)
Condition is True. So, i Value printed (i.e., 1)
Second Iteration: for(i = 2; 2 <= 6; 2++)
Condition is True. It prints 2
Third Iteration: for(i = 3; 3 <= 6; 3++)
Condition is True. It prints 3
Fourth Iteration: for(i = 4; 4 <= 6; 4++)
Condition is True. 4 printed.
Fifth Iteration: for(i = 5; 5 <= 6; 5++)
Condition is True. It prints 5
Sixth Iteration: for(i = 6; 6 <= 6; 6++)
Condition is True. 6 printed
7th Iteration: for(i = 7; 7 <= 6; 7++)
Condition (7 <= 6) is False. So, the Java compiler exits from the For Loop
Java Program to Print Natural Numbers from 1 to N using While loop
This Java program to return natural numbers from 1 to N is the same as the above example, but we are using the While Loop.
Explanation:
please mark me the brainliest answer of
★
public class Main {
public static void main(String [] args) {
for(int i = 1; i < 50; i++) {
System.out.println(i);
}
}
}
★
❖ The program uses a for loop to iterate through numbers 1 to 49, and for each number, it prints the value of the variable i using the System.out.println() method.
❖ The for loop starts with an initialization statement int i = 1, which sets the value of the loop variable i to 1.
❖ The loop then continues as long as the condition i < 50 is true, which means that the loop will iterate 49 times (for i = 1 to i = 49).
❖ During each iteration of the loop, the code inside the loop's block is executed, which simply prints the current value of i to the console.
❖ Therefore, when this program is executed, it will output the numbers 1 to 49 to the console.