CodeHub Menu
Home Exercise Practice About Download Portfolio Play Games NEW Privacy Policy Terms Contact
Login / Register

overload the function

JAVA November 19, 2025 5 views 0 likes
Define a class to overload the function print as follows:

void print() - to print the following format
Number Pattern Design

Number Pattern Output


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.

Answer


Method Overloading Program in Java

Method Overloading Program in Java


import java.util.Scanner;

public class KboatMethodOverload
{
    public void print()
    {
        for(int i = 1; i <= 5; i++) 
        {
            for(int j = 1; j <= 4; j++)
            {
                System.out.print(i + " ");
            }
        System.out.println();
        }
    }
    
    public void print(int n)
    {
        int d = 0;
        int evenSum = 0;
        int oddSum = 0;
        while( n != 0)
        {
            d = n % 10;
            if (d % 2 == 0)
                evenSum += d;
            else
                oddSum += d;
            n = n / 10;
        }
        
        if(evenSum == oddSum)
            System.out.println("Lead number");
        else
            System.out.println("Not a lead number");
    }
    
    public static void main(String args[]) 
    {
        KboatMethodOverload obj = new KboatMethodOverload();
        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);
    }
}
5 views 0 likes
Total visits: 2,089 • Unique visitors: 1,285