Home / Programs / 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. 3669odd digits sum = 3 + 9 = 12even digits sum = 6 + 6 = 123669 is a lead number.
Programming Example

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.

👁 76 Views
💻 Practical Program
📘 Step by Step Learning
Study this program carefully to understand the logic, output, and explanation in a structured way.

Information & Algorithm

Given Input:


Expected Output:


Program Code

import java.util.Scanner;

public class RansariMethodOverload {
    
    // Method to print the pattern
    public void print() {
        for (int i = 1; i <= 5; i++) {
            for (int j = 1; j <= 4; j++) {
                System.out.print(i + " ");
            }
            System.out.println();
        }
    }
    
    // Method to check for a lead number
    public void print(int n) {
        int d, evenSum = 0, oddSum = 0;
        
        while (n != 0) {
            d = n % 10;
            if (d % 2 == 0)
                evenSum += d; // Sum of even digits
            else
                oddSum += d;  // Sum of odd digits
            
            n /= 10;
        }
        
        // Check if it's a lead number
        if (evenSum == oddSum)
            System.out.println(n + " is a Lead number");
        else
            System.out.println(n + " is Not a lead number");
    }
    
    public static void main(String args[]) {
        RansariMethodOverload obj = new RansariMethodOverload(); // Corrected class name
        Scanner in = new Scanner(System.in);
        
        System.out.println("Pattern:");
        obj.print();
        
        System.out.print("Enter a number: ");
        int num = in.nextInt();
        obj.print(num);

        in.close(); // Closing scanner to prevent resource leak
    }
}

Output

Pattern:
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5

Enter a number: 3669
3669 is a Lead number

Explanation

The program demonstrates method overloading in Java with two print methods:

  1. print(): Prints a 5x4 pattern where each row contains the row number repeated 4 times.
  2. print(int n): Checks if a number is a lead number, where the sum of even digits equals the sum of odd digits.

In the main method:

  • It first prints the pattern.
  • Then, it checks if the user-entered number is a lead number.

The program efficiently handles digit extraction and compares the sums of even and odd digits to determine if it's a lead number.

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.