Computer Science, asked by h2557joanna, 4 months ago

Write a program to print following pattern without using any nested
loop.
#
# #
# # #
# # # #
# # # # #

Answers

Answered by BrainlyProgrammer
3

Question:-

  • To print the following pattern

#

# #

# # #

# # # #

# # # # #

  • Condition:-Not to use nested loop

____________

Answer:

This is done using String.

import java.util.*;

class Code

{

public static void main (String ar [])

{

String str="# ";

for(I=1;I<=5;I++)

{

System.out.print(str);

str+="# ";

}

}

}

________

Output:-

#

# #

# # #

# # # #

# # # # #

__________

Explanation:

We know, In java,'+' sign is used for addition and combination of two or more string

So in the given above code:-

  • Loop is running from 1 to 5 because there are 5 lines(no. of rows)
  • initially, str is "#"...So it will print only one #.
  • After each iteration str is updated with one more '#'
  • So after each iteration, it will print updated value of str

Answered by Afreenakbar
0

Answer:

Code-

n = 5

for i in range(1, n+1):

print("# " * i)

Explanation:

Here's an example program in Python that prints the pattern without using any nested loops:

Code:-

n = 5

for i in range(1, n+1):

print("# " * i)

Output:

#

# #

# # #

# # # #

# # # # #

Explanation:

  1. We first set the value of n to 5, which is the number of rows we want to print.
  2. We use a for loop that iterates from 1 to n (inclusive).
  3. For each iteration of the loop, we use the print() function to output a string consisting of the "#" character followed by a space, repeated i times using the string multiplication operator *. This creates the required pattern of "#" symbols with spaces in between.
  4. Since we are not using any nested loops, the time complexity of this program is O(n).

Similar Questions:

https://brainly.in/question/2436178

https://brainly.in/question/2907302

#SPJ3

Similar questions