Java Program on do while Loop

Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java

Program 1

import java.util.*;
class TestDowhile
{
    public static void main(String args[])
    {
              int n,choice;
            Scanner scan=new Scanner(System.in);
          do
          {
            System.out.println("---------------Menu----------------");
            System.out.println("1.Factorial");
            System.out.println("2.Reverse of number");
            System.out.println("3.Palimdrome Number");
            System.out.println("4.Exit");
            System.out.println("--------------------------------------");
           choice=scan.nextInt();
           switch(choice)
           {
            case 1:
                {
                          int f=1;
                          System.out.println("Enter a number");
                          n=scan.nextInt();
                           while(n!=0)
                           {
                              f=f*n;
                              n--;
                           }
                           System.out.println("factorial is " + f); 
                           break;
                }

                case 2:
                {
                          int s=0,r;
                          System.out.println("Enter a number");
                          n=scan.nextInt();
                           while(n!=0)
                           {
                               r=n%10;
                               s=s*10+r;
                               n=n/10;
                           }
                           System.out.println("Reverse is " +s); 
                           break;
                }
                   case 3:
                {
                          int s=0,r,m;
                          System.out.println("Enter a number");
                          n=scan.nextInt();
                          m=n;
                           while(n!=0)
                           {
                               r=n%10;
                               s=s*10+r;
                               n=n/10;
                           }
                         if(m==s)
                          System.out.println("Number is palindrom");
                          else
                          System.out.println("Number is not palindrom");
                           break;
                }
                case 4:break;
                default: System.out.println("Invalid choice");
           }
         }while(choice!=4);    
    }
}

 

If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google

courses

TechVidvan Team

TechVidvan Team provides high-quality content & courses on AI, ML, Data Science, Data Engineering, Data Analytics, programming, Python, DSA, Android, Flutter, full stack web dev, MERN, and many latest technology.

Leave a Reply

Your email address will not be published. Required fields are marked *