ICSE 2013 Computer Application Q9 Solution

ICSE 2013 Computer Application Q9 Solution

Q. Using the switch statement, write a menu driven program:
(i) To check and display whether the 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 than 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

ICSE 2013

Solution
import java.io.*;
/**
 *  class MenuDriven 
 *
  */
public class MenuDriven
{
    public static void main(String args[])throws IOException
    {
        InputStreamReader in=new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(in);
        int n, count, ch, small, a, i;
        // For menu
        System.out.println("Enter 1 for Composite number");
        System.out.println("Enter 2 for Smallest digit search");
        System.out.print("Enter your choice :");
        ch=Integer.parseInt(br.readLine());     // enter menu option
        switch(ch)
        {
            case 1:                                           // for checking composite number
                count=0;
                System.out.print("Enter a number :");
                n= Integer.parseInt(br.readLine());
                for(i=2; i<=n/2; i++)
                {
                    if(n % i == 0)
                     count++;
                }
                if(count>0)
                 System.out.println("Entered number is Composite number");
                else
                 System.out.println("Entered number is Composite number");
                break;
            case 2:                                        // for checking smallest digit from a number
                System.out.print("Enter a number :");
                n= Integer.parseInt(br.readLine());
                small=99;
                while(n!=0)
                {
                    a = n % 10;
                    if(a < small)
                    {
                        small = a;
                    }
                    n = n / 10;
                }
                System.out.println("Smallest number is " + small);
                break;
            default:
                  System.out.println("Wrong choice");
        }
    }
               
}

Output:
Enter 1 for Composite number
Enter 2 for Smallest digit search
Enter your choice :1
Enter a number :27
Entered number is Composite number

Enter 1 for Composite number
Enter 2 for Smallest digit search
Enter your choice :2
Enter a number :78653
Smallest number is 3


SHARE THIS
Previous Post
Next Post