Search This Blog

UI/UX Design | Web Development | Mobile Application Development

Whether you need UI/ UX design, web development services, or mobile app development services, Qaabil Digitals will be more than happy to help you. We work with top software developers, designers, and project managers. We tailor our services to suit your exact needs. We closely look at your company, listen to your story, and bring about the best ideas and solutions that can help you drive solid results. We don’t just produce work that works. We produce work that matters. Get a quote now Or Talk to us

Thursday 12 April 2012

How to use If else in switch statement in java to perform different arithmatic operations BY (Shifa Anjum)

import java.util.Scanner;


public class ConditionalSwitch {
   
    public static void main(String[] args) {
       
        Scanner in = new Scanner (System.in);
        int a,b,c=1;
       
        System.out.println("First number must be 1 to 5 and second from 1 to 100");
       
        System.out.print("Enter first number = ");
        a=in.nextInt();
        System.out.print("Enter second number = ");
        b=in.nextInt();
       
        switch(a)
        {
        case 1:
            if (b<100 && b>1)
            {
                c = a * b;
                System.out.println("Product of both numbers = "+c);
            }
            else
                System.out.println("Second number must be from 1 to 100");
            break;
        case 2:
            if (b<100 && b>1)
            {
                c = a + b;
                System.out.println("Sum of both numbers = "+c);
            }
            else
                System.out.println("Second number must be from 1 to 100");
            break;
        case 3:
            if (b<100 && b>1)
            {
                c = a - b;
                System.out.println(""+a+" - "+b+" = "+c);
            }
            else
                System.out.println("Second number must be from 1 to 100");
            break;
        case 4:
            if (b<100 && b>1)
            {
                c = a / b;
                System.out.println(""+a+" / "+b+" = "+c);
            }
            else
                System.out.println("Second number must be from 1 to 100");
            break;
        case 5:
            if (b<100 && b>1)
            {
                c = b % a;
                System.out.println("Remainder of "+b+" / "+a+" = "+c);
            }
            else
                System.out.println("Second number must be from 1 to 100");
            break;
        default:
                System.out.println("First number must be from 1 to 5");
        }
    }

}
Share