8
Define a class to overload the method display as follows:
void display( ): To print the following format using nested loop
Show SolutionHide Solution↓
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
void display(int n): To print the square root of each digit of the given number.
Example:
n = 4329
Output – 3.0
1.414213562
1.732050808
2.0
import java.util.Scanner;
public class KboatMethodOverload
{
public void display()
{
for(int i = 1; i <= 5; i++)
{
for(int j = 1; j <= i; j++)
{
System.out.print(j + " ");
}
System.out.println();
}
}
public void display(int n)
{
while( n != 0)
{
int d = n % 10;
System.out.println(Math.sqrt(d));
n = n / 10;
}
}
public static void main(String args[])
{
KboatMethodOverload obj = new KboatMethodOverload();
Scanner in = new Scanner(System.in);
System.out.println("Pattern: ");
obj.display();
System.out.print("Enter a number: ");
int num = in.nextInt();
obj.display(num);
}
}
