Draw this pattern in one loop | SoloLearn: Learn to code for FREE!

0

Draw this pattern in one loop

* ** *** **** *****

10/18/2017 2:08:56 PM

RAHUL KUMAR

8 Answers

New Answer

+3

Does this work for you? #include<bits/stdc++.h> using namespace std; void printPattern(int n) { int curr_star = 0; for (int line_no = 1; line_no <= n;) { if (curr_star < line_no) { cout << "* "; curr_star++; continue; } else { cout << "\n"; line_no++; curr_star = 0; } } } int main() { printPattern(7); return 0; } https://code.sololearn.com/czqse90iUD1N/?ref=app

+2

Not in C. Python FTW! for i in range(1, 6): print("*" * i)

+2

I am totally agree with niawahta. Use only one loop and you can use conditions.

+2

+1

but I want ans in c

+1

But I don't think it's possible.

+1

for(unsigned i = 0; i < 6; ++i){ for(unsigned j = 0; j <= i; ++j) putchar('*'); putchar('\n'); } Everything is possible in C :D

+1

@Baptiste E. Prunier The question says one loop. I know it's quite easy with a nested for loop, that's pretty common.