icse-promo

Question

Using the switch statement, write a menu driven program: 
(i) To check and display whether a number input by the user is
a composite number or not (A number is said to be a composite, if it
has one or more then one factors excluding 1 and the number itself).
Example: 4, 6, 8, 9…
(ii) To find the smallest digit of an integer that is input:
Sample input: 6524
Sample output: Smallest digit is 2
For an incorrect choice, an appropriate error message should be displayed.

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					import java.util.Scanner;

public class program9
{

    public static void main(String[] args)
    {
        int number=0;
        Scanner sc = new Scanner(System.in);

        System.out.println("Press 1 to  Check composite number");
        System.out.println("Press 2 to  Find smallest digit of a number");
        System.out.print("Enter your choice: ");
        int choice = sc.nextInt();
        switch (choice)
        {
            case 1:
            System.out.print("Enter a number: ");
            number = sc.nextInt();
            int i=0;
            boolean IsComposite=false;
            for (i = 2; i < number; i++)
            {
                if (number % i == 0)
                {

                    IsComposite=true;
                    break;
                }
            }

            if(IsComposite==true)
            {
                System.out.println("It is a composite number");
            }
            else
            {
                System.out.println("It is not a composite number");
            }
            break;
            case 2:
            System.out.print("Enter a number: ");
            number = sc.nextInt();
            int smallest = 9,rem=0;
            while (number > 0)
            {
                rem = number % 10;
                if (rem < smallest)
                {
                    smallest = rem;
                }
                number = number / 10;
            }

            System.out.println("Smallest digit is " + smallest);
            break;
            default:
            System.out.println("Incorrect choice");
            break;
        }
    }
}

				
			

Coding Store

Leave a Reply

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