ICSE Computer Science Java Programs | IT Developer
IT Developer

Java Programs - Solved 2023 ICSE Computer Science Paper



Share with a Friend

Solved 2023 ICSE Computer Science Paper

Class 10 - ICSE Computer Science Solved Papers

Menu Driven Program - Overload Method - ICSE 2023 Computer Science

Question 5

Define a class to overload the function print() as follows:

void print() to print the following format


1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5

void print(int n) to check whether the number is a lead number. A lead number is the one whose sum of even digits are equal to sum of odd digits.

e.g. 3669


odd digits sum = 3 + 9 = 12
even digits sum = 6 + 6 = 12
3669 is a lead number.

import java.util.Scanner; class Overload{ public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("1. Pattern"); System.out.println("2. Lead number"); System.out.print("Enter your choice: "); int choice = Integer.parseInt(in.nextLine()); switch(choice){ case 1: print(); break; case 2: System.out.print("Enter the number: "); int n = Integer.parseInt(in.nextLine()); print(n); break; default: System.out.println("Invalid choice!"); } } public static void print(){ for(int i = 1; i <= 5; i++){ for(int j = 1; j <= 5; j++) System.out.print(i + " "); System.out.println(); } } public static void print(int n){ int even = 0; int odd = 0; while(n != 0){ int d = n % 10; if(d % 2 == 0) even += d; else odd += d; n /= 10; } if(even == odd) System.out.println("Lead number!"); else System.out.println("Not a lead number."); } }

Output

 
 OUTPUT 1: 
1. Pattern
2. Lead number
Enter your choice: 1
1 1 1 1 1 
2 2 2 2 2 
3 3 3 3 3 
4 4 4 4 4 
5 5 5 5 5 

 OUTPUT 2: 
1. Pattern
2. Lead number
Enter your choice: 2
Enter the number: 3669
Lead number!