Please Solve This problem

Answers
Required Answer:-
Question:
Write a program to read the number n using Scanner class and display the following pattern without using string data type.
1
11
111
1111
11111
111111
1111111
Solution:
This is an easy question. Read this post to get your answer.
This is the program. It's the easiest approach.
import java.util.*;
public class Pattern
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter n - ");
int n = sc.nextInt();
for(int i=1,a=1;i<=n;i++,a=a*10+1)
System.out.println(a);
sc.close();
}
}
This is solved using 1 loop and without using any string. In total, 15 lines of code.
How it's solved?
We will take the number of rows(n) as input. Now, we will display the value of a. After each iteration, value of a is multiplied by 10 and 1 is added to it.
a = 1
a = a * 10 + 1 = 10 + 1 = 11
a = 11
a = a * 10 + 1 = 110 + 1 = 111
In this way, the pattern is displayed.
Output is attached.


Question:-
WAP in JAVA to print the following pattern
1
11
111
1111
11111
111111
1111111
______
Condition:-
- Use Scanner class
- Not to use String data type.
_______
Code:-
package Coder;
import java.util.*;
public class Pattern {
public static void main(String[] args) {
Scanner sc=new Scanner (System.in);
for(int i=1;i<=7;i++)
{
for (int j=1;j<=i;j++)
{
System.out.print("1"); //As we have to print only one no.
}
System.out.println();
}
}
}
______
Variable Description:-
- i----Loop Variable
- j----Loop Variable
______
How this code works?
- The program runs i loop from 1 to 7
- In iteration, j loop runs from 1 to i
- Since we need to print only one number...we print "1".
- Then,j gets updated with +1
- if the condition (j<=i) becomes true....Same procedure will occur as mentioned from (3) to (4)
- if the condition becomes false, j loop terminates
- i gets updated with +1
- if the condition (i<=7) becomes true... same procedure will occur from (2) to (7)
- if the condition becomes false, the loop terminates.
____
•Output Attached.

