Skip to main content

Switch Statement in Java

Switch Statement in Java:-

1) Switch Statement
It is used to solve option based program.it provides multiple options using multiple case statements.
If we want to develop a choice based program then we mostly prefer switch statements in Java.
Syntax of Switch:-
switch(option)
{
   case optionvalue:
     statement;
     break;
  ....
  default:
   statement:
   break;
}
at runtime option==optionvalue  then that statement will be executed.
/* WAP to choose social media platform according to initial char? *                                                                                                                              
import java.util.Scanner;
class Socialmedia
{
    public static void main(String args[])
    {
      char ch;
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter you choice of social media to enter initial char");
      ch = sc.next().charAt(0);
      switch(ch)    // ch=='f'
      {
           case 'f':
           case 'F':
           System.out.println("You have choosen Facebook");
           break;
           case 'i':
           case 'I':
           System.out.println("You have choosen Instagram");
           break;
           case 't':
           case 'T':
           System.out.println("You have choosen Twitter");
           break;
           case 'w':
           case 'W':
           System.out.println("You have choosen Whatsapp");
           break;
           default:
           System.out.println("Wrong option");
           break; 
      }   
     } 
}
Q) WAP to check vowel and consonant using a switch?
import java.util.Scanner;
class CheckVC
{
   public static void main(String args[])
   {
     Scanner sc = new Scanner(System.in);
     System.out.println("enter char");
     char ch=sc.next().charAt(0);
     switch(ch)
     {
              case 'a':
              case 'A':
              case 'e':
              case 'E':
              case 'i':
              case 'I':
              case 'o':
              case 'O':
              case 'u':
              case 'U':
              System.out.println("Vowel");
              break;
              default:
              System.out.println("Consonent");
              break; 
     }
  }
}
................................................................................................................................................
WAP to check even|odd using switch?
int num=5;
switch(num%2)
{
   case 0:
   System.out.
println("even");
   break;
   default:
   System.out.println("odd");
   break;
}
WAP to check the greater number using Switch?
class Greaterswitch
{
    public static void main(String args[])
    {
         int a=10,b=20;
         switch(a/b)
         {
             case 0:
             System.out.println("b is greater");
             break;
             default:
             System.out.println("a is greater");
             break; 
             }
         }
}.........................................................................................................
2) Nested Switch Statement:-
We can write more than one switch statement using the nested sequence in Java.Nested Switch Statement:-
switch(option)
{
    case optionvalue:
       switch(option)
       {
             case optionvalue:
                    Statement;
                     break;
            default:
                 Statement;
                 break;   
        }
   break;
  default:
     switch(option)
   {
         case optionvalue:
            statement;
             break;
        default:
             statement;
             break;
    }
 break;
}
}
WAP to check divisibility that number is divisible by 3 and 5 both?
import java.util.Scanner;
class CheckVC
{
   public static void main(String args[])
   {
      int num=3;
      switch(num%3)
      {
                case 0:
                 switch(num%5)
                  {
                       case 0:
                        System.out.println("divisible by both");
                        break;
                       default:
                       System.out.println("only divisible by 3");
                       break;
                  }
                 break;
               default:
                  switch(num%5)
                  {
                       case 0:
                        System.out.println("divisible by five");
                        break;
                       default:
                       System.out.println("not divisible by any");
                       break;
                  }
                break;
     }    
  }
}
.......................................................................................................

WAP to check the greater number using a switch?
The solution to this program:-
class Greater
{
  public static void main(String args[])
  {
      int a=20,b=100;
      switch(a/b)
      {
          case 0:
           System.out.println("b is greater");
           break;
          default:
           System.out.println("a is greater");
           break;   
      }          
   }
}
WAP to check the greatest number using a switch?

class Greater
{
  public static void main(String args[])
  {
      int a=9,b=30,c=7;
      switch(a/b)
      {
          case 0:
             switch(b/c)
             {
               case 0:
                System.out.println("c is greater");
                break;
               default:
                 System.out.println("b is greater");
                 break;   
          }
          break;
          default:
          switch(a/c)
             {
               case 0:
                System.out.println("c is greater");
                break;
               default:
                 System.out.println("a is greater");
                 break;   
          }
          break;
      }          
   }
}
Program to calculate the greatest using the conditional operator?
class SwitchGreater
{
    public static void main(String args[])
    {
          int a=10,b=2000,c=300;
          Object o = a>b;  //false 
          switch(o.toString().charAt(0))  //f
          {
                case 't':
                Object o1 = a>c;
                switch(o1.toString().charAt(0))
                {
                   case 't':
                     System.out.println("A is greatest");
                     break;
                  default:
                     System.out.println("C is greatest");
                     break;   
                } 
                break;
                default:
                Object o2 = b>c;  //true
                switch(o2.toString().charAt(0))  //t
                {
                   case 't':
                     System.out.println("b is greatest");
                     break;
                  default:
                     System.out.println("C is greatest");
                     break;   
                } 
               break;
           }
    }
}
Assignment of Switch:-
Q)WAP to display "yes", "no" and "cancel" when user assign 'y','n', and 'c'?
Q) WAP to check leap year using a switch?
Solution of this program?
import java.util.Scanner;
class CheckLeapYearSwitch
{
public static void main(String args[])
{
      Scanner sc = new Scanner(System.in);
      int year;
      System.out.println("Enter year");
      year = sc.nextInt();
     switch(year%400)
       {
             case 0:
                System.out.println("Leap year");
                break;
             default:
                switch(year%4 )
                 {
                    case 0:
                        switch(year%100)
                         {
                            case 0:
                                System.out.println("Not a leap year");
                                break;
                            default:
                               System.out.println("leap year");
                               break;
                        }
                    break;
                  default:
                     System.out.println("Not a leap year");
                     break;
                }
              break;  
         }

    }
}
Q) WAP  to check entered char is numeric, alphabets, or special using switch? 
import java.util.Scanner;
class SwitchExample
{
   public static void main(String args[])
   {
        char ch;
       
        Scanner sc = new Scanner(System.in);
        System.out.println("enter char");
        ch=sc.next().charAt(0);
         Object o=ch>=48 && ch<=57;
         Object o1 = ch>=65 && ch<=91 || ch>=97 && ch<=123;
         switch(o.toString())
        {
           case "true":
           System.out.println("digit");
           break;
           default:
           switch(o1.toString())
           {
           case "true":
           System.out.println("alphabets");
           break;
           default:
           System.out.println("Special"); 
           }
           break;
        }      
   }
}

Comments

  1. class Leapyearswitch
    {
    public static void main(String args[])
    {
    int num=2012;
    switch(num%4)
    {
    case 0:
    System.out.println("Leap year");
    break;
    default:
    System.out.println("not a leap year");
    break;
    }
    }

    }

    ReplyDelete
  2. class Leapyearswitch
    {
    public static void main(String args[])
    {
    int num=2012;
    switch(num%4)
    {
    case 0:
    System.out.println("Leap year");
    break;
    default:
    System.out.println("not a leap year");
    break;
    }
    }

    }

    ReplyDelete
  3. import java.util.Scanner;
    class evenodd
    {
    public static void main(String[]args)
    {
    Scanner sc=new Scanner(System.in);
    System.out.println("enter the value");

    int i=sc.nextInt();

    switch(i%2)
    {
    case 0:
    System.out.println("even");
    break;
    default:
    System.out.println("odd");
    break;
    }

    }
    }

    ReplyDelete
  4. import java.util.Scanner;
    class CheckVC
    {
    public static void main(String[]arg)
    {
    Scanner sc=new Scanner(System.in);
    System.out.println("enter the value");
    char x=sc.next().charAt(0);
    switch(x)
    {
    case 'a':
    case 'A':
    case 'e':
    case 'E':
    case 'i':
    case 'I':
    case 'o':
    case 'O':
    case 'u':
    case 'U':
    System.out.println("Vowel");
    break;
    default:
    System.out.println("consonent value");

    }
    }
    }

    ReplyDelete
  5. import java.util.Scanner;
    class Greatnoo
    {
    public static void main(String[]arg)
    {
    Scanner sc=new Scanner(System.in);
    System.out.println("enter the value of a");
    int a=sc.nextInt();
    System.out.println("enter the value of b");
    int b=sc.nextInt();
    switch(a/b)
    {
    case 0:
    System.out.println("b is greater than a");
    break;
    default:
    System.out.println("a is greater than b");
    break;
    }
    }
    }

    ReplyDelete
  6. import java.util.Scanner;
    class CheckDIV
    {
    public static void main(String[]arg)
    {
    Scanner sc=new Scanner(System.in);
    int a=3;
    switch(a%3)
    {
    case 0:
    switch(a%5)
    {
    case 0:
    System.out.println("divisible by both");
    break;
    default:
    System.out.println("only div by 3");
    break;
    }
    break;
    default:
    switch(a%5)
    {
    case 0:
    System.out.println("div by 5");
    break;
    default:
    System.out.println("not div by any");
    break;
    }
    break;
    }
    }
    }

    ReplyDelete
  7. class Greatnoswitch
    {
    public static void main(String[]args)
    {
    int a=9, b=30, c=7;

    switch(a/b)
    {
    case 0:
    switch(b/c)
    {
    case 0:
    System.out.println("c is greater");
    break;
    default:
    System.out.println("b is greater");
    break;
    }
    break;
    default:
    switch(a/c)
    {
    case 0:
    System.out.println("c is greater");
    break;
    default:
    System.out.println("a is greater");
    break;
    }
    break;
    }
    }
    }

    ReplyDelete
  8. import java.util.Scanner;
    class yesnocancle
    {
    public static void main(String[]arg)
    {
    Scanner sc=new Scanner(System.in);
    System.out.println("enter the value");
    char ch=sc.next().charAt(0);

    switch(ch)
    {
    case 'y':
    System.out.println("yes");
    break;
    case 'n':
    System.out.println("no");
    break;
    case 'c':
    System.out.println("cancle");
    break;
    default:
    System.out.println("something incorrect");
    break;
    }
    }
    }

    ReplyDelete
  9. import java.util.Scanner;
    class leapyear
    {
    public static void main(String[]arg)
    {
    Scanner sc=new Scanner(System.in);
    System.out.println("enter the year");
    int year=sc.nextInt();

    switch(year%4)
    {
    case 0:
    System.out.println("leap year");
    break;
    default:
    System.out.println("this is not a leap year,..");
    }
    }
    }

    ReplyDelete
  10. import java.util.Scanner;
    class EO
    {
    public static void main(String[]arg)
    {
    Scanner sc=new Scanner(System.in);
    System.out.println("enter number...");
    int num=sc.nextInt();

    switch(num%2)
    {
    case 0:
    System.out.println("even");
    break;
    default:
    System.out.println("odd");
    break;
    }
    }
    }

    ReplyDelete
  11. // Akshay


    class Year
    {

    public static void main(String args[])
    {
    int num=2021;
    switch(num%4)
    {
    case 0:
    switch(num%100)
    {
    case 0:
    System.out.println("leap");
    break;
    default:
    System.out.println("non leap");
    break;

    }
    break;
    default:
    switch(400)
    {
    case 0:
    System.out.println("leap");
    break;
    default:
    System.out.println("non leap");
    break;

    }
    break;

    }





    }

    }

    ReplyDelete
  12. import java.util.*;
    class Switch
    {
    public static void main(String arg[])
    {
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter char");
    char ch=sc.next().charAt(0);
    switch(ch)
    {
    case 'a':System.out.println("vowel");
    break;
    case 'e':System.out.println("vowel");
    break;
    case 'i':System.out.println("vowel");
    break;
    case 'o':System.out.println("vowel");
    break;
    case 'u':System.out.println("vowel");
    break;

    default:System.out.println("consonent");


    }
    }
    }

    ReplyDelete
  13. import java.util.*;
    class EO
    {
    public static void main(String arg[])
    {
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter number");
    int x=sc.nextInt();
    switch(x%2)
    {
    case 0:System.out.println("Even");
    break;

    default:
    System.out.println("odd");
    }
    }
    }

    ReplyDelete
  14. import java.util.*;
    class Y
    {
    public static void main(String arg[])
    {

    Scanner sc=new Scanner(System.in);
    System.out.println("Enter ");
    char ch=sc.next().charAt(0);
    switch(ch)
    {
    case 'y':System.out.println("yes ");
    break;
    case 'n': System.out.println("no");
    break;
    case 'c':System.out.println("cancel");
    }

    }
    }

    ReplyDelete
  15. import java.util.*;
    class Leap
    {
    public static void main(String arg[])
    {
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter char");
    int x=sc.nextInt();
    switch(x%400)
    {
    case 0:System.out.println("leap year");

    switch(x%100!)
    {
    System.out.println("not leap");
    break;
    default :

    System.out.println("leap year");
    }
    }



    }
    }

    ReplyDelete

  16. /*WAP to display "yes", "no" and "cancel" when user assign 'y','n', and 'c'?*/
    import java.util.Scanner;
    class EnterChoice
    {

    public static void main(String args[])
    {
    char ch;
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter you choice ");
    ch = sc.next().charAt(0);
    switch(ch)
    {
    case 'y':
    case 'Y':
    System.out.println("yes");
    break;
    case 'n':
    case 'N':
    System.out.println("no");
    break;
    case 'c':
    case 'C':
    System.out.println("cancle");
    break;

    default:
    System.out.println("invalid choice");
    break;

    }


    }


    }

    ReplyDelete
  17. import java.util.*;
    class LeapYear
    {
    public static void main(String arg[])
    {
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter char");
    int num =sc.nextInt();

    switch(num%4)
    {

    case 0:
    switch(num%100)
    {
    case 0:
    System.out.println("not leap");
    break;
    default:
    System.out.println(" leap");
    break;


    }
    break;
    default:
    switch(400)
    {
    case 0:
    System.out.println(" leap");
    break;
    default:
    System.out.println("not leap");
    break;


    }
    break;
    }


    }
    }

    ReplyDelete

  18. import java.util.Scanner;
    class EnterPin1
    {
    public static void main(String args[])
    {
    Scanner sc = new Scanner(System.in);
    int num = 1000;
    int pin =0;
    int a = sc.nextInt();
    switch(a%num)
    {
    case 0 :
    System.out.println("Enter amount");
    break;
    default:
    System.out.println("1.you eneterd invalid pincode please try Again");
    int number = sc.nextInt();
    switch(a%num){
    case 0 :
    System.out.println("Pincode successfully entered");
    System.out.println("Enter amount");
    int amount = sc.nextInt();
    break;
    default:
    System.out.println("2.you eneterd invalid pincode please try Again");
    int b = sc.nextInt();

    }
    switch(a%num)
    {
    case 0 :
    System.out.println("Pincode successfully entered");
    System.out.println("Enter amount");
    int amount = sc.nextInt();
    break;
    default:
    System.out.println("3.you eneterd invalid pincode please try Again after 30 min");
    }
    }
    }
    }

    ReplyDelete
  19. import java.util.Scanner;
    class B_Banking
    {
    public static void main(String args[])
    {
    Scanner sc = new Scanner(System.in);
    int pincode = sc.nextInt();
    int i = 1;
    int count = 1 ;int balance = 5000 ;
    while(i<=pincode)
    {
    pincode = pincode / 10;
    count++;
    i++;
    }
    //System.out.println(count);

    switch(count%6)
    {
    case 0 :
    System.out.println("Press C for Credit");
    System.out.println("Press D for Debit");
    System.out.println("Press B Check Balance");
    System.out.println("Press R for Repeat");
    System.out.println("Press E for Exit");
    char ch = sc.next().charAt(0);


    switch(ch)
    {
    case 'C':
    case 'c':
    System.out.println("Credit");
    System.out.println("Enter Amount");

    break;

    case 'D':
    case 'd':
    System.out.println("Debit");
    System.out.println("Enter Amount");

    break;

    case 'B':
    case 'b':
    System.out.println("Check Balance");
    System.out.println("Balance = " +balance);
    break;

    case 'R':
    case 'r':
    System.out.println("Repeat");
    break;

    case 'E':
    case 'e':
    System.out.println("Exit");
    break;

    default:
    System.out.println("Invalid key");
    break;
    }


    break;
    default: System.out.println("Invalid"); break;
    }

    }
    }

    ReplyDelete
  20. //WAP to display "yes", "no" and "cancel" when user assign 'y','n', and 'c'?
    import java.util.Scanner;
    class Yesno
    {
    public static void main(String...args)
    {

    char ch;
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter char");
    ch=sc.next().charAt(0);
    switch(ch)
    {
    case 'y':
    System.out.println("Yes");
    break;
    case 'n':
    System.out.println("No");
    break;
    case 'c':
    System.out.println("Cancel");
    break;
    default:
    System.out.println("Wrong Choice");
    }
    }
    }

    ReplyDelete
  21. //WAP to check leap year using a switch?
    import java.util.Scanner;
    class Leap
    {
    public static void main(String args[])
    {
    int year, remainder;
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter Year: ");
    year=sc.nextInt();

    switch(remainder=(year%4==0)&&((year%400==0)||(year%100!=0)))
    {
    case 1:
    System.out.println("Leap Year.");
    break;
    case 0:
    System.out.println("Not Leap Year.");
    break;

    default:
    System.out.println("Invalid.");
    break;

    }
    }
    }


    ReplyDelete
  22. Khushboo Sendre


    Q)WAP to display "yes", "no" and "cancel" when user assign 'y','n', and 'c'?
    import java.util.Scanner;
    class Yesnocancel
    {
    public static void main(String args[])
    {
    int ch;
    Scanner sc = new Scanner(System.in);
    System.out.println("enter the choice");
    ch = sc.next().charAt(0);

    switch(ch)
    {
    case'y':
    case'Y':
    System.out.println(" Your choice is Yes");
    break;
    case'n':
    case'N':
    System.out.println("Your choice is No");
    break;
    case'c':
    case'C':
    System.out.println("Cancel");
    break;
    default:
    System.out.println("wrong");
    break;
    }
    }
    }



    Q) WAP to check leap year using a switch?
    import java.util.Scanner;
    class Leapyear4feb
    {
    public static void main(String args[])
    {
    int n;
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the year");
    n = sc.nextInt();

    switch(n%4)
    {
    case 0:
    switch(n%100)
    {
    case 0:
    System.out.println("Year is leap year");
    break;
    default:
    System.out.pritnln("Year is not a leap year");
    break;
    }
    break;
    default:
    switch(n%400)
    {
    case 0:
    System.out.println("Year is leap year");
    break;
    default:
    System.out.println("Year is not a leap year");
    break;
    }
    break;

    }
    }
    }

    ReplyDelete
  23. // PUSHPENDRA SISODIYA

    import java.util.Scanner;
    class Switch
    {
    public static void main(String args[])
    {
    int num;
    Scanner sc=new Scanner(System.in);
    System.out.println("enter number");
    num=sc.nextInt();
    switch(num%2)
    {
    case 0:
    System.out.println("even");
    break;
    case 1:
    System.out.println("odd");
    break;
    default:

    }



    }


    }

    ReplyDelete
  24. //PUSHPENDRA SISODIYA


    import java.util.Scanner;
    class Greater
    {
    public static void main(String args[])
    {
    int a,b,c,ch;
    Scanner sc=new Scanner(System.in);
    System.out.println("enter value of a");
    a=sc.nextInt();
    System.out.println("enter value of b");
    b=sc.nextInt();
    System.out.println("enter value of c");
    c=sc.nextInt();
    System.out.println("enter any choice");
    ch=sc.nextInt();
    switch(ch)
    {
    case 1:
    {
    if(a>b)
    {
    if(a>c)
    System.out.println("a is greater");
    else
    System.out.println("c is greater");
    }
    else if(b>c)
    System.out.println("b is greater");
    else
    System.out.println("c is greater");
    }
    break;
    default:
    }

    }


    }

    ReplyDelete

  25. //PUSHPENDRA SISODIYA

    import java.util.Scanner;
    class Divisible
    {
    public static void main(String args[])
    {
    int num;
    Scanner sc=new Scanner(System.in);
    System.out.println("enter any number");
    num=sc.nextInt();
    switch(num%3)
    {
    case 0:
    switch(num%5)
    {
    case 0:
    System.out.println("divisible by both");
    break;
    case 1:
    System.out.println("divisible by 3");
    }

    break;
    default:


    }


    }

    }

    ReplyDelete
  26. //PUSHPENDRA SISODIYA


    import java.util.Scanner;
    class CheckSwitch
    {
    public static void main(String args[])
    {
    char ch;
    Scanner sc=new Scanner(System.in);
    System.out.println("enter character");
    ch=sc.next().charAt(0);
    switch(ch)
    {
    case 'y':
    System.out.println("yes");
    break;
    case 'n':
    System.out.println("no");
    break;

    case 'c':
    System.out.println("cancle");
    break;
    default:
    System.out.println("other words");
    break;

    }

    }





    }

    ReplyDelete
  27. // Q) WAP to check the greater number using a switch?

    class SwitchGreater
    {
    public static void main(String args[])
    {
    int a=20,b=10;
    switch(a/b)
    {
    case 0:
    System.out.println("b is greater");
    break;
    default:
    System.out.println("a is greater");
    break;
    }
    }
    }

    ReplyDelete
  28. //WAP to check divisibility that number is divisible by 3 and 5 both?

    class SwitchDivisible
    {
    public static void main(String[] args) {

    int num=31;
    switch(num%3)
    {
    case 0:
    switch(num%5)
    {
    case 0:
    System.out.println(num+" is Divisible by 3 and 5");
    break;

    default:
    System.out.println(num+"Divisible by 3 only");
    break;
    }
    break;

    default:
    switch(num%5)
    {
    case 0:
    System.out.println(num+"is Divisible by 5 only");
    break;

    default:
    System.out.println(num+" is not Divisible by 3 and 5")
    break;
    }
    break;




    }
    }
    }

    ReplyDelete
  29. //Q) WAP to check entered char is numeric, alphabets, or special using switch?

    import java.util.Scanner;
    class SwitchNumeric
    {
    public static void main(String[] args) {

    Scanner sc=new Scanner(System.in);
    System.out.println("enter Character");
    char ch= sc.next().charAt(0);
    Object s=(ch>48 && ch<=57);
    Object s1=(ch>=64 &&ch<=90)|| (ch>=97 &&ch<=122);
    switch(s.toString())
    {
    case "true":
    System.out.println("numeric");
    break;

    default:
    switch(s1.toString())
    {
    case "true":
    System.out.println("characher");
    break;
    default:
    System.out.println("special symbol");
    break;
    }
    break;
    }

    }
    }

    ReplyDelete
  30. // Q) WAP to check the greatest number using a switch?

    class SwitchGreatest
    {
    public static void main(String[] args) {

    int a=10,b=50,c=30;
    switch(a/b)
    {
    case 0:
    switch(b/c)
    {
    case 0:
    System.out.println("c is gretest");
    break;

    default:
    System.out.println("b is greatest");
    break;
    }
    break;

    default:
    switch(a/c)
    {
    case 0:
    System.out.println("c is greatest");
    break;
    default:
    System.out.println("a is greatest");
    break;
    }
    break;
    }
    }
    }

    ReplyDelete
  31. //Q)WAP to display "yes", "no" and "cancel" when user assign 'y','n', and 'c'?

    class SwitchYes
    {
    public static void main(String[] args) {

    char ch='h';
    switch(ch)
    {
    case 'Y':
    case 'y':
    System.out.println("yes");
    break;

    case 'N':
    case 'n':
    System.out.println("No");
    break;

    case 'c':
    case 'C':
    System.out.println("Cancel");
    break;

    default:
    System.out.println("wrong input");
    break;
    }

    }
    }

    ReplyDelete
  32. //Q) WAP to check leap year using a switch?

    class SwitchLeap
    {
    public static void main(String[] args) {

    int year=1900;
    Object o=(year%4==0 && year%100!=0) ||(year%400==0);
    switch(o.toString())
    {
    case "true":
    System.out.println("leap year ");
    break;

    default:
    System.out.println("Not leap year");
    break;
    }

    }
    }

    ReplyDelete
  33. WAP to check leap year using a switch
    Program:

    import java.util.Scanner;
    public class LeapYear {

    public static void main(String[] args) {
    int year;
    Scanner scan=new Scanner(System.in);
    System.out.print("Enter year- ");
    year=scan.nextInt();
    switch(year%400) {
    case 0:
    System.out.print("Leapyear");
    break;
    default:
    switch (year%4) {
    case 0:
    switch(year%100) {
    case 0:
    System.out.print("not a Leapyear");
    break;
    default:
    System.out.println(year+" is leap year.");
    break;
    }
    }
    }
    }
    }

    ReplyDelete
  34. WAP to display "yes", "no" and "cancel" when user assign 'y','n', and 'c'
    Program:

    import java.util.Scanner;
    public class Main {

    public static void main(String[] args) {
    char choice;
    Scanner scan=new Scanner(System.in);
    System.out.print("Enter acharacter (y,n,c)- ");
    choice=scan.next().charAt(0);
    switch(choice) {
    case 'y':
    System.out.println("Yes");
    break;
    case 'n':
    System.out.println("No");
    break;
    case 'c':
    System.out.println("Cancel");
    break;
    default:
    System.out.println("Invalid Input!");

    }
    }

    }

    ReplyDelete
  35. //WAP to display "yes", "no" and "cancel" when user assign 'y','n', and 'c'?
    import java.util.Scanner;
    class Switchdisplay
    {
    public static void main(String args[])
    {
    Scanner sc=new Scanner(System.in);
    System.out.println("enter value");
    char ch=sc.next().charAt(0);
    switch(ch)
    {
    case 'y':
    System.out.println("yes");
    break;
    case 'n':
    System.out.println("no");
    break;
    case 'c':
    System.out.println("cancel");
    break;
    default:
    System.out.println("try again");
    break;
    }
    }
    }

    ReplyDelete
  36. Program to calculate the greatest using the conditional operator?
    Program:

    import java.util.Scanner;
    public class ConditionalOperator {

    public static void main(String[] args) {
    int a,b,c;
    Scanner scan=new Scanner(System.in);
    System.out.print("Enter three numbers - ");
    a=scan.nextInt();
    b=scan.nextInt();
    c=scan.nextInt();
    Object obj=a>b;
    switch(obj.toString().charAt(0)) {
    case 't':
    Object obj1=a>c;
    switch(obj1.toString().charAt(0)) {
    case 't':
    System.out.println(a+" is greatest");
    break;
    default:
    System.out.println(c+" is greatest");
    break;
    }
    break;
    default:
    Object obj2=b>c;
    switch(obj2.toString().charAt(0)) {
    case 't':
    System.out.println(b+" is greatest");
    break;
    default:
    System.out.println(c+" is greatest");
    break;
    }
    break;
    }
    }

    }


    ReplyDelete
  37. WAP to check entered char is numeric, alphabets, or special using switch
    Program:

    import java.util.Scanner;
    public class Main {

    public static void main(String[] args) {
    char c;
    Scanner scan =new Scanner(System.in);
    System.out.print("Enter a character - ");
    c=scan.next().charAt(0);
    Object obj=(c>=67&&c<=92)||(c>=97&&c<=122);
    switch(obj.toString().charAt(0)) {
    case 't':
    System.out.print(c+" is a alphabet");
    break;
    default:
    Object obj1=(c>=48&&c<=57);
    switch(obj1.toString().charAt(0)) {
    case 't':
    System.out.print(c+" is a digit");
    break;
    default:
    System.out.print(c+" is a special character");
    }
    }

    }

    }

    ReplyDelete
  38. // WAP to check leap year using a switch?
    class Leap_using_switch
    {
    public static void main(String abc[])
    {
    int year=2021;
    switch(year%400)
    {
    case 0:
    System.out.println("leap year");
    break;
    }
    switch(year%4)
    {
    case 0:
    switch(year%100)
    {
    case 0:
    System.out.println(" not a leap year");
    break ;
    default:
    System.out.println("leap year");
    }
    }
    }
    }

    ReplyDelete
  39. // WAP to check leap year using a switch?
    class Leap_using_switch
    {
    public static void main(String abc[])
    {
    int year=2021;
    switch(year%400)
    {
    case 0:
    System.out.println("leap year");
    break;

    default:
    switch(year%4)
    {
    case 0:
    switch(year%100)
    {
    case 1:
    System.out.println(" leap year");
    break ;
    }
    default:
    System.out.println("not a leap year");

    }
    }
    }
    }

    ReplyDelete
  40. //WAP to display "yes", "no" and "cancel" when user assign 'y','n', and 'c'?
    import java.util.Scanner;
    class Displayycn
    {
    public static void main(String args[])
    {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter Any Character");
    char ch = sc.next().charAt(0);
    switch(ch)
    {
    case'y':
    case'Y':
    System.out.println("Yes");
    break;
    case'n':
    case'N':
    System.out.println("No");
    break;
    case'c':
    case'C':
    System.out.println("Cancel");
    break;
    default:
    System.out.println("Invalid input");
    }
    }
    }

    ReplyDelete

  41. Ankita Verma

    import java.util.Scanner;
    class Yesnc
    {
    public static void main(String args[])
    {

    char ch;
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter your choice");
    ch=sc.next().charAt(0);
    switch(ch)
    {
    case'y':
    case'Y':
    System.out.println("Yes");
    break;
    case'n':
    case'N':
    System.out.println("NO");
    break;
    case'c':
    case'C':
    System.out.println("Cancel");
    break;
    default:
    System.out.println("Other");
    break;
    }
    }
    }

    ReplyDelete


  42. Ankita Verma

    import java.util.Scanner;
    class Leapyear1
    {
    public static void main(String args[])
    {
    int n;
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter th choice");
    n=sc.nextInt();

    switch(n%400)
    {
    case 0:
    System.out.println("leap year");
    break;
    default:
    System.out.println(" not leap year");
    break;
    }
    }
    }

    ReplyDelete

  43. Ankita Verma

    import java.util.Scanner;
    class Alpha
    {
    public static void main(String args[])
    {
    char ch;
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter th char");
    ch=sc.next().charAt(0);

    Object o1=ch>=48 && ch<=57;
    Object o2=ch>=65 && ch<=91||ch>=97 && ch<=123;
    switch(o1.toString())
    {
    case"true" :
    System.out.println("digit");
    break;
    default:
    switch(o2.toString())
    {
    case"true":
    System.out.println("alphabet");
    break;
    default:
    System.out.println("special");
    }
    break;
    }
    }
    }

    ReplyDelete
  44. package switchcase;
    import java.util.Scanner;
    public class Checkyesnocancel {

    public static void main(String[] args) {
    char ch;
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter char");
    ch=sc.next().charAt(0);
    switch(ch)
    {
    case'y':
    System.out.println("YES");
    break;
    case'n':
    System.out.println("NO");
    break;
    case'c':
    System.out.println("CANCEL");
    break;
    default:
    System.out.println("INVALID CHARACTER");
    }
    sc.close();

    }

    }

    ReplyDelete
  45. package switchcase;
    import java.util.Scanner;
    public class EvenOdd {

    public static void main(String[] args) {
    int n;
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter number");
    n=sc.nextInt();

    switch(n%2)
    {
    case 0:
    System.out.println("Even Number");
    break;
    default:
    System.out.println("Odd Number");


    }
    sc.close();
    }

    }

    ReplyDelete
  46. package switchcase;
    import java.util.Scanner;
    public class Divisibility {

    public static void main(String[] args) {
    int n;
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter the number ");
    n=sc.nextInt();

    switch(n%3)
    {
    case 0:
    switch(n%5)
    {
    case 0:
    System.out.println("Number is divisible by both 3 and 5 ");
    break;

    default:
    System.out.println("Number only divisible by 3 ");
    break;
    }

    break;
    default:
    switch(n%5)
    {
    case 0:
    System.out.println("Number is only Divisible by 5 ");
    break;
    default:
    System.out.println("Number is not divisible by both 3 and 5");
    break;
    }
    break;
    }
    sc.close();

    }
    }

    ReplyDelete
  47. package switchcase;
    import java.util.Scanner;
    public class GreatestnoConditional {

    public static void main(String[] args) {
    int a,b,c;
    Scanner sc= new Scanner(System.in);
    System.out.println("Enter 1st number");
    a=sc.nextInt();
    System.out.println("Enter 2nd number");
    b=sc.nextInt();
    System.out.println("Enter 3rd number");
    c=sc.nextInt();

    Object o = (a>b);
    switch(o.toString().charAt(0))
    {
    case't':

    Object o1 = (a>c);
    switch(o1.toString().charAt(0))
    {
    case 't':
    System.out.println("greatest number= "+a);
    break;

    default:
    System.out.println("greatest number= "+c);
    break;
    }
    break;
    default:
    Object o2 =(b>c);
    switch(o2.toString().charAt(0))
    {
    case 't':
    System.out.println("greatest number=" +b);
    break;
    default:
    System.out.println("greatest number="+c);
    break;

    }
    break;

    }
    sc.close();
    }

    }

    ReplyDelete
  48. package loops;
    import java.util.Scanner;
    public class Factorial {

    public static void main(String[] args) {
    int n,i,f=1;
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter the number");
    n=sc.nextInt();

    i=1;

    while(i<=n)
    {
    f=(f*i);
    i++;


    }

    System.out.println(n+"!"+"="+f);
    sc.close();
    }

    }

    ReplyDelete
  49. Ankita Verma
    import java.util.Scanner;

    class Switch
    {
    public static void main(String[]args)
    {
    char ch;
    Scanner sc=new Scanner(System.in);
    System.out.println("enter your choice");
    ch=sc.next().charAt(0);
    switch(ch)
    {
    case 'y':
    case 'Y':
    System.out.println("Yes");
    break;

    case 'n':
    case 'N':
    System.out.println("No");
    break;
    case 'c':
    case 'C':
    System.out.println("Cancel");
    break;
    default:
    System.out.println("wrong answer");
    break;
    }
    }
    }

    ReplyDelete
  50. //yogesh seroke
    //even odd
    import java.util.Scanner;
    class evenodd
    {
    public static void main(String[]args)
    {
    Scanner sc=new Scanner(System.in);
    System.out.println("enter the value");

    int i=sc.nextInt();

    switch(i%2)
    {
    case 0:
    System.out.println("even");
    break;
    default:
    System.out.println("odd");
    break;
    }

    }
    }

    ReplyDelete
  51. //yogesh seroke
    //v and c
    import java.util.Scanner;
    class CheckVC
    {
    public static void main(String[]arg)
    {
    Scanner sc=new Scanner(System.in);
    System.out.println("enter the value");
    char x=sc.next().charAt(0);
    switch(x)
    {
    case 'a':
    case 'A':
    case 'e':
    case 'E':
    case 'i':
    case 'I':
    case 'o':
    case 'O':
    case 'u':
    case 'U':
    System.out.println("Vowel");
    break;
    default:
    System.out.println("consonent value");

    }
    }
    }

    ReplyDelete
  52. //yogesh seroke
    //greatest num
    import java.util.Scanner;
    class Greatnoo
    {
    public static void main(String[]arg)
    {
    Scanner sc=new Scanner(System.in);
    System.out.println("enter the value of a");
    int a=sc.nextInt();
    System.out.println("enter the value of b");
    int b=sc.nextInt();
    switch(a/b)
    {
    case 0:
    System.out.println("b is greater than a");
    break;
    default:
    System.out.println("a is greater than b");
    break;
    }
    }
    }

    ReplyDelete
  53. //yogesh seroke
    //divisible by 3 and 5
    import java.util.Scanner;
    class CheckDIV
    {
    public static void main(String[]arg)
    {
    Scanner sc=new Scanner(System.in);
    int a=3;
    switch(a%3)
    {
    case 0:
    switch(a%5)
    {
    case 0:
    System.out.println("divisible by both");
    break;
    default:
    System.out.println("only div by 3");
    break;
    }
    break;
    default:
    switch(a%5)
    {
    case 0:
    System.out.println("div by 5");
    break;
    default:
    System.out.println("not div by any");
    break;
    }
    break;
    }
    }
    }

    ReplyDelete
  54. //yogesh seroke
    //greatest from three num
    class Greatnoswitch
    {
    public static void main(String[]args)
    {
    int a=9, b=30, c=7;

    switch(a/b)
    {
    case 0:
    switch(b/c)
    {
    case 0:
    System.out.println("c is greater");
    break;
    default:
    System.out.println("b is greater");
    break;
    }
    break;
    default:
    switch(a/c)
    {
    case 0:
    System.out.println("c is greater");
    break;
    default:
    System.out.println("a is greater");
    break;
    }
    break;
    }
    }
    }

    ReplyDelete
  55. //yogesh seroke
    //yes no cancle
    import java.util.Scanner;
    class yesnocancle
    {
    public static void main(String[]arg)
    {
    Scanner sc=new Scanner(System.in);
    System.out.println("enter the value");
    char ch=sc.next().charAt(0);

    switch(ch)
    {
    case 'y':
    System.out.println("yes");
    break;
    case 'n':
    System.out.println("no");
    break;
    case 'c':
    System.out.println("cancle");
    break;
    default:
    System.out.println("something incorrect");
    break;
    }
    }
    }

    ReplyDelete
  56. //yogesh seroke
    //leapyear
    import java.util.Scanner;
    class leapyear
    {
    public static void main(String[]arg)
    {
    Scanner sc=new Scanner(System.in);
    System.out.println("enter the year");
    int year=sc.nextInt();

    switch(year%4)
    {
    case 0:
    System.out.println("leap year");
    break;
    default:
    System.out.println("this is not a leap year,..");
    }
    }
    }

    ReplyDelete
  57. //yogesh seroke
    //even odd
    import java.util.Scanner;
    class EO
    {
    public static void main(String[]arg)
    {
    Scanner sc=new Scanner(System.in);
    System.out.println("enter number...");
    int num=sc.nextInt();

    switch(num%2)
    {
    case 0:
    System.out.println("even");
    break;
    default:
    System.out.println("odd");
    break;
    }
    }
    }

    ReplyDelete
  58. WAP to check Greatest among three different numbers?

    class SwitchExample
    {
    public static void main(String args[])
    {
    int a=60,b=230,c=11;
    //System.out.println(""+ (a>b));
    Object o = a>b;
    switch(o.toString())
    {
    case "true":
    o = a>c;
    switch(o.toString())
    {
    case "true":
    System.out.println("a is greatest");
    break;
    default:
    System.out.println("c is greatest");
    break;
    }
    break;

    default:
    o = b>c;
    switch(o.toString())
    {
    case "true":
    System.out.println("b is greatest");
    break;
    default:
    System.out.println("c is greatest");
    break;
    }
    break;

    }


    }

    }

    ReplyDelete
  59. Sandeep kelwa

    The solution to this program:-
    class Greater
    {
    public static void main(String args[])
    {
    int a=20,b=100;
    switch(a/b)
    {
    case 0:
    System.out.println("b is greater");
    break;
    default:
    System.out.println("a is greater");
    break;

    }



    }

    }

    ReplyDelete
  60. import java.util.Scanner;
    class Calculator
    {
    public static void main(String args[])
    {
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter No. 1=");
    int no1=sc.nextInt();
    System.out.println("Enter No. 2=");
    int no2=sc.nextInt();
    System.out.println("Selecte Symbol(+,-,*,/)");
    String sym=sc.next();
    int res;
    switch(sym)
    {
    case "+" : res=no1+no2;
    System.out.println("Addition is :"+res);
    break;
    case "-" : res=no1-no2;
    System.out.println("Subtraction is :"+res);
    break;
    case "*" : res=no1*no2;
    System.out.println("Multiplication is :"+res);
    break;
    case "/" : res=no1/no2;
    System.out.println("Divisionis :"+res);
    break;
    default : System.out.println("Invalid Symbole");
    break;
    }
    }
    }

    ReplyDelete
  61. import java.util.Scanner;
    class EvenOdd
    {
    public static void main(String args[])
    {
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter your Number :");
    int no=sc.nextInt();
    switch(no%2)
    {
    case 0:
    System.out.println(" This no is Even");
    break;
    default:
    System.out.println("This no. is Odd");
    break;
    }
    }
    }

    ReplyDelete
  62. import java.util.Scanner;
    class Display11
    {
    public static void main(String args[])
    {
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter your Charactore");
    char ch=sc.next().charAt(0);
    switch(ch)
    {
    case 'y':
    case 'Y':
    System.out.println("Yes");
    break;

    case 'n':
    case 'N':
    System.out.println("No");
    break;

    case 'c':
    case 'C':
    System.out.println("Cancel");
    break;

    default:
    System.out.println("wrong answer");
    break;
    }
    }
    }

    ReplyDelete

Post a Comment

POST Answer of Questions and ASK to Doubt

Popular posts from this blog

Uncontrolled form input in React-JS

  Uncontrolled form input in React-JS? If we want to take input from users without any separate event handling then we can uncontrolled the data binding technique. The uncontrolled input is similar to the traditional HTML form inputs. The DOM itself handles the form data. Here, the HTML elements maintain their own state that will be updated when the input value changes. To write an uncontrolled component, you need to use a ref to get form values from the DOM. In other words, there is no need to write an event handler for every state update. You can use a ref to access the input field value of the form from the DOM. Example of Uncontrolled Form Input:- import React from "react" ; export class Info extends React . Component {     constructor ( props )     {         super ( props );         this . fun = this . fun . bind ( this ); //event method binding         this . input = React . createRef ();...

JSP Page design using Internal CSS

  JSP is used to design the user interface of an application, CSS is used to provide set of properties. Jsp provide proper page template to create user interface of dynamic web application. We can write CSS using three different ways 1)  inline CSS:-   we will write CSS tag under HTML elements <div style="width:200px; height:100px; background-color:green;"></div> 2)  Internal CSS:-  we will write CSS under <style> block. <style type="text/css"> #abc { width:200px;  height:100px;  background-color:green; } </style> <div id="abc"></div> 3) External CSS:-  we will write CSS to create a separate file and link it into HTML Web pages. create a separate file and named it style.css #abc { width:200px;  height:100px;  background-color:green; } go into Jsp page and link style.css <link href="style.css"  type="text/css" rel="stylesheet"   /> <div id="abc"> </div> Exam...

JDBC using JSP and Servlet

JDBC means Java Database Connectivity ,It is intermediates from Application to database. JDBC has different type of divers and provides to communicate from database server. JDBC contain four different type of approach to communicate with Database Type 1:- JDBC-ODBC Driver Type2:- JDBC Vendor specific Type3 :- JDBC Network Specific Type4:- JDBC Client-Server based Driver  or JAVA thin driver:- Mostly we prefer Type 4 type of Driver to communicate with database server. Step for JDBC:- 1  Create Database using MYSQL ,ORACLE ,MS-SQL or any other database 2   Create Table using database server 3   Create Form according to database table 4  Submit Form and get form data into servlet 5  write JDBC Code:-     5.1)   import package    import java.sql.*     5.2)  Add JDBC Driver according to database ide tools     5.3)  call driver in program         ...