DEV Community

Preethi Nandhagopal
Preethi Nandhagopal

Posted on

Program on while loop

1)Print numbers from 1 to 10:
public class Seven{
public static void main (String[] args){
int no=1;
while(no<=10){
System.out.print(no);
no++;
}
}
}

2)To find even and odd numbers from 1 to 20:
public class Eight{
public static void main (String[] args){
int no=1;
while(no<=20){
if(no%2==0){
System.out.println("the even no is " +no);
no++;
}else{
System.out.println("the odd no is " +no);
no++;
}
}
}
}

3)Five tables:
public class Nine{
public static void main (String[] args){
int no=1;
while(no<=10){
System.out.println(no + " * 5 = " + no*5);
no++;
}
}
}

4)Print numbers in reverse 5 to 1:
public class Ten{
public static void main (String[] args){
int no=5;
while(no>=1){
System.out.println(no);
no--;
}
}
}

5)To count the numbers number=1+2+3+4+5=15:
public class Eleven{
public static void main (String[] args){
int count=0;
int no=1;
while(no<=5){
count=count+no;
no++;
}
System.out.println(count);
}
}

6)To print the alphabet from A to Z:
public class Alphabet_w{
public static void main (String[] args){
char ch='A';
while(ch<='Z'){
System.out.print(ch+"");
ch++;
}
}
}

7)To print the alphabet by combination of if condition when character is P print string "Preethi":
public class Alphabet_Condition_w{
public static void main (String[] args){
char ch='A';
while(ch<='Z'){
if(ch=='P'){
System.out.println("Preethi");
}else{
System.out.println(ch);
}
ch++;
}
}
}

Top comments (0)