DEV Community

Neelakandan R
Neelakandan R

Posted on

while loop

Print number like this order:

*1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25 *

package Afterfeb4; public class while1 { public static void main(String[] args) { int no = 1; int upperLimit = 5; while (no <= 25) { while (no <= upperLimit) { System.out.print(no + " "); no = no + 1; } System.out.println(); upperLimit = upperLimit + 5; } } } 
Enter fullscreen mode Exit fullscreen mode
package Afterfeb4; public class while2 { public static void main(String[] args) { int no =1; while(no<=25) { System.out.print(no+" "); if(no%5==0) { System.out.println(); } no++; } } } 
Enter fullscreen mode Exit fullscreen mode

count of digits for a given number:

package Afterfeb4; public class while3 { public static void main(String[] args) { int no = 19675; int i = 0; while (no > 0) { System.out.println(no % 10);//Reminder no = no / 10;//upper value i++; } System.out.println("total count = " + i); } } 
Enter fullscreen mode Exit fullscreen mode

Output:
5
7
6
9
1
total count = 5

*Changing order of no:
1 2 3 4 5
5 4 3 2 1
1 2 3 4 5
5 4 3 2 1
1 2 3 4 5
*

package Afterfeb4; public class Numberparttern1 { static int j; public static void main(String[] args) { int count = 1; while (count <= 5) { if (count % 2 != 0) // check count no is odd number ;example count=1,3,5 { int j = 1; while (j <= 5) { System.out.print(j + " "); j++; } } else // (count%2==0)check count no even number ;example count=2,4 { j = 5; while (j >= 1) { System.out.print(j + " "); j--; } } 
Enter fullscreen mode Exit fullscreen mode

Output:
1 2 3 4 5
5 4 3 2 1
1 2 3 4 5
5 4 3 2 1
1 2 3 4 5

package Afterfeb4; public class Numberpattern2 { static int j = 1; public static void main(String[] args) { int count = 1; while (count <= 5) { if (count == 1) while (j <= 5) { { System.out.print(j); j++; if (j % 6 == 00) System.out.println(); } } else if (count == 2) { j = 10; while (j >= 6) { System.out.print(j); j--; } System.out.println(); } else if (count == 3) { j = 11; while (j <= 15) { System.out.print(j); j++; } System.out.println(); } else if (count == 4) { j = 20; while (j >= 16) { System.out.print(j); j--; } System.out.println(); } else if (count == 5) { j = 21; while (j <= 25) { System.out.print(j); j++; } } count++; } } } 
Enter fullscreen mode Exit fullscreen mode

Output:

12345
109876
1112131415
2019181716
2122232425

Top comments (0)