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 Learning
Learn this program step-by-step with algorithm, source code, output and detailed explanation.

📌 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
    }
}

                        

🖥 Program 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.

📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.