Java Online Compiler

do while Loop In java

do while:

'do while' is a keyword in java language which is used to execute one block of statements several number of times based on the condition.
do
 {
 ...
 ...
 }
 while(condition);
 
In the above syntax :
if the condition is true the statemnets of 'do while' block is execute infinite times , if the condition is false the statements of 'do while' block is execute one time.
Display 1 to 10 Number using do-while loop

import java.util.*;
class Factorial
{
public static void main(String args[])
{
int i=1,n;
Scanner s=new Scanner(System.in);
System.out.println("Enter N Value");
n=s.nextInt();
do
{
System.out.println(i);
i++;
}
while(i<=n);

}

}

Display 10 to 1 Number using do-while loop

import java.util.*;
class Factorial
{
public static void main(String args[])
{
int i,n;
Scanner s=new Scanner(System.in);
System.out.println("Enter N Value");
n=s.nextInt();
i=n;
do
{
System.out.println(i);
i--;
}
while(i>=1);

}

}

Factorial Number Using DO While Loop

import java.util.*;
class Factorial
{
public static void main(String args[])
{
int i=1,fact=1,n;
Scanner s=new Scanner(System.in);
System.out.println("Enter N Value");
n=s.nextInt();
do
{
fact=fact*i;
i++;
}
while(i<=n);
System.out.println("Fact is :"+fact);
}

}