Anyone pls tell me how to print patterns in c++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Anyone pls tell me how to print patterns in c++

Like *. a. * *. a b * * *. a. b. c till 'n' lines Explanation pls

28th Nov 2017, 1:44 PM
Fire Feathers
Fire Feathers - avatar
18 Answers
+ 2
Answered by Gaurav buddy i think u shoild try urself first just notice if u enter n , that means u need n rows ... make a loop for n rows now every row , see what u have to do ... u see that u need spaces first... make a loop for that ... now what u see is u need to print * ... make a loop for that //here is the rough view , u need to figure it out yourself ... if u want to make possible patterns by urself ☺ for (){ for (){} for (){} }
30th Nov 2017, 2:08 PM
Fire Feathers
Fire Feathers - avatar
+ 9
for i in range(x): print(' '*(x-i),'* '*i) Try this.. Works like a charm😀😀 Even i was trying this for a lot of time😂
28th Nov 2017, 2:05 PM
Yash✳️
Yash✳️ - avatar
+ 8
Idk in c++.. But you'll get the logic.. This will print a simple pattern in python: x=int(input()) for i in range(x): print('*' * i) This will multiply '*' i times for each iteration.. For eg.. When i is 0.. 0 times '*' (nothing) When i is 1.. 1 times '*' When i is 2.. Two times '*'.. And so on.. Until i is x.. Try this logic in c++
28th Nov 2017, 1:53 PM
Yash✳️
Yash✳️ - avatar
+ 8
What?
28th Nov 2017, 2:10 PM
Yash✳️
Yash✳️ - avatar
+ 7
c++ is not each processed line wise it will take 3 loops to print the above patterns
28th Nov 2017, 2:11 PM
Fire Feathers
Fire Feathers - avatar
+ 7
@Yash thanks btw
28th Nov 2017, 2:11 PM
Fire Feathers
Fire Feathers - avatar
+ 6
but what about the spaces Ur code will give output as * ** ***
28th Nov 2017, 1:55 PM
Fire Feathers
Fire Feathers - avatar
+ 5
See pattern in my question
28th Nov 2017, 1:55 PM
Fire Feathers
Fire Feathers - avatar
+ 5
c++
28th Nov 2017, 2:09 PM
Fire Feathers
Fire Feathers - avatar
+ 3
Use for loops. for (int i=1;i<=n;i++){ cout<<(n-i)*" "<<i*((" "+"*")
29th Nov 2017, 11:23 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 3
in python it's easy
29th Nov 2017, 1:30 PM
Fire Feathers
Fire Feathers - avatar
+ 2
buddy only in c++
29th Nov 2017, 1:30 PM
Fire Feathers
Fire Feathers - avatar
+ 2
Thanks Verma
30th Nov 2017, 3:05 PM
Fire Feathers
Fire Feathers - avatar
+ 1
actually 3 loops are needed and some logic to set spaces 1st loop goes till n and then inside that loop another for loop is set for spaces and after that for the another loop for printing
30th Nov 2017, 2:43 PM
Sarthak Verma
Sarthak Verma - avatar
+ 1
Verma make it pls
30th Nov 2017, 2:44 PM
Fire Feathers
Fire Feathers - avatar
+ 1
Eg. for(i=0;i<n;i++) {for(j=0;j<n-i;j++)// this loop is for spaces(focus on the condition) {cout<<" "; } for(k=0;k<=i;k++)//this loop is for printing {cout<<"*"; } } caution: I have made this roughly so it may give imperfect output but see the logic
30th Nov 2017, 2:49 PM
Sarthak Verma
Sarthak Verma - avatar
+ 1
int main() {int i,j,k,n; cin>>n; for(i=0;i<n;i++) {for(j=0;j<n-i;j++)// this loop is for spaces(focus on the condition) {cout<<" "; } for(k=0;k<=i;k++)//this loop is for printing {cout<<"* "; }cout<<endl; } //caution: I have made this roughly so it may give imperfect output but see the logic return 0; } this is complete correct code
30th Nov 2017, 2:54 PM
Sarthak Verma
Sarthak Verma - avatar
+ 1
int main() {int i,j,k,n; char ch; cin>>n; for(i=0;i<n;i++) {ch='a'; for(j=0;j<n-i;j++)// this loop is for spaces(focus on the condition) {cout<<" "; } for(k=0;k<=i;k++,ch++)//this loop is for printing {cout<<ch<<" "; }cout<<endl; } return 0; } This is for the a b c d loop
30th Nov 2017, 2:57 PM
Sarthak Verma
Sarthak Verma - avatar