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.
Java Programming Language (Article) (Program)
69
Given Input:
Expected Output:
Program:
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:
print(): Prints a 5x4 pattern where each row contains the row number repeated 4 times.
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.
This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.
Program:
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:
print(): Prints a 5x4 pattern where each row contains the row number repeated 4 times.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.
This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.