while loops in java with examples

0

while loop is a type of loop in programming, it can be used when rounds number of loop is unspecified by the programmer, also it can be used if rounds number is specified.

it can be written in this way:

while(the condition of continue looping)

{

here is the code which will repeat in each round

}

All Contributions

The factorial of n (written n!) is the product of the integers between 1 and n. Thus 4! = 1*2*3*4 = 24.

By definition, 0! = 1.

Factorial is not defined for negative numbers.

Write a program that asks the user for a non-negative integer and computes and prints the factorial of that integer

import java.util.Scanner; 
public class Exercise4 { 
public static void main(String[] args) { 
int number; 
System.out.println("Enter the number: "); 
Scanner scanner = new Scanner(System.in); 
number = scanner.nextInt(); 
long fact = 1; 
if ( number==0) 
{fact = 1; 
System.out.println("Factorial of "+number+" is: "+fact);} 
else if ( number > 0) 
{ 
 int i = 1; 
 while(i<=number) 
 { 
 fact = fact * i; 
 i++; 
 } 
System.out.println("Factorial of "+number+" is: "+fact); 
} 
else if (number<0) 
 System.out.println("ERROR"); 
 } 
}

write java program to print all even numbers between 0 to 100

int t=0;
while(t<=100) {
 if(t%2==0)
         System.out.println(t);
 t++; 
}

using while loops: write java program to print all odd numbers between 0 to 100

int t=0;
while(t<=100) {
 if(t%2==1)
 System.out.println(t);
 t++;
}

write java program to print the numbers from 1 to 100   --  use while loop

int i=1;
while(i<=100)
{
 System.out.println(i);
 i++;
}

write java program to print the numbers from 1 to 100   --  use while loop

int i=1;
while(i<=100)
{
 System.out.println("hello world");
 i++;
}

  

total contributions (5)

New to examplegeek.com?

Join us