Draw this pattern in one loop | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Draw this pattern in one loop

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

18th Oct 2017, 2:08 PM
RAHUL KUMAR
RAHUL KUMAR - avatar
8 Answers
+ 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
19th Oct 2017, 2:31 PM
Bob
Bob - avatar
+ 2
Not in C. Python FTW! for i in range(1, 6): print("*" * i)
18th Oct 2017, 2:15 PM
LunarCoffee
LunarCoffee - avatar
+ 2
I am totally agree with niawahta. Use only one loop and you can use conditions.
19th Oct 2017, 12:15 PM
RAHUL KUMAR
RAHUL KUMAR - avatar
+ 2
woa
19th Oct 2017, 3:48 PM
LunarCoffee
LunarCoffee - avatar
+ 1
but I want ans in c
18th Oct 2017, 2:27 PM
RAHUL KUMAR
RAHUL KUMAR - avatar
+ 1
But I don't think it's possible.
18th Oct 2017, 2:53 PM
LunarCoffee
LunarCoffee - avatar
+ 1
for(unsigned i = 0; i < 6; ++i){ for(unsigned j = 0; j <= i; ++j) putchar('*'); putchar('\n'); } Everything is possible in C :D
19th Oct 2017, 7:34 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 1
@Baptiste E. Prunier The question says one loop. I know it's quite easy with a nested for loop, that's pretty common.
19th Oct 2017, 12:05 PM
LunarCoffee
LunarCoffee - avatar