Computer Science, asked by Ishita6006, 7 months ago

Please Solve This problem ​

Attachments:

Answers

Answered by anindyaadhikari13
14

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.

Attachments:
Answered by BrainlyProgrammer
4

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&lt;=7;i++)

{

for (int j=1;j&lt;=i;j++)

{

System.out.print("1"); //As we have to print only one no.

}

System.out.println();

}

}

}

______

Variable Description:-

  1. i----Loop Variable
  2. j----Loop Variable

______

How this code works?

  1. The program runs i loop from 1 to 7
  2. In iteration, j loop runs from 1 to i
  3. Since we need to print only one number...we print "1".
  4. Then,j gets updated with +1
  5. if the condition (j<=i) becomes true....Same procedure will occur as mentioned from (3) to (4)
  6. if the condition becomes false, j loop terminates
  7. i gets updated with +1
  8. if the condition (i<=7) becomes true... same procedure will occur from (2) to (7)
  9. if the condition becomes false, the loop terminates.

____

Output Attached.

Attachments:
Similar questions