List of loop programming exercises
Write a Java program to print all natural numbers from 1 to n. - using while loop
Sol :-
Static
class loop1
{
public static void main(String args[])
{
int i=1, j=10;
while(i<=10)
{
System.out.println(i);
i++;
}
}
}
Output :-
Dynamic :-
import java.util.*;
class loop1
{
public static void main(String args[])
{
int i=1;
Scanner in = new Scanner(System.in);
System.out.println("Enter the Number :- ");
int j = in.nextInt();
while(i<=j)
{
System.out.println(i);
i++;
}
}
}
Output :-