ICSE Computer Science Java Programs | IT Developer
IT Developer

Java Programs - Solved 2010 ICSE Computer Science Paper



Share with a Friend

Solved 2010 ICSE Computer Science Paper

Class 10 - ICSE Computer Science Solved Papers

Menu Driven - Prime Number / Automorphic Number Program - ICSE 2010 Computer Science

Write a menu-driven program to accept a number and check and display whether it is a prime number or not, or an automorphic number or not. Use switch-case statement.
(a) Prime number: A number is said to be a prime number if it is divisible only by 1 and itself and not by any other number.
Example: 3, 5, 7, 11, 13, etc.
(b) Automorphic number: An automorphic number is the number which is contained in the last digit(s) of its square.
Example: 25 is an automorphic number as its square is 625 and 25 is present as the last two digits.

import java.util.Scanner; class Menu{ public static void main(String args[]){ Scanner in = new Scanner(System.in); System.out.println("1. Prime number"); System.out.println("2. Automorphic number"); System.out.print("Enter your choice: "); int choice = Integer.parseInt(in.nextLine()); switch(choice){ case 1: System.out.print("N = "); int n = Integer.parseInt(in.nextLine()); int f = 0; for(int i = 2; i <= n / 2; i++){ if(n % i == 0){ f = 1; break; } } if(f == 0 && n > 1) System.out.println("Prime number!"); else System.out.println("Not a prime number."); break; case 2: System.out.print("N = "); n = Integer.parseInt(in.nextLine()); long s = n * n; int digits = 0; for(int i = n; i != 0; i /= 10) digits++; int divisor = (int)Math.pow(10, digits); if(s % divisor == n) System.out.println("Automorphic number!"); else System.out.println("Not an Automorphic number."); break; default: System.out.println("Invalid choice!"); } } }

Output

 
 OUTPUT 1: 
1. Prime number
2. Automorphic number
Enter your choice: 1
N = 17
Prime number! 

 OUTPUT 2: 
1. Prime number
2. Automorphic number
Enter your choice: 2
N = 625
Automorphic number!