+ 5
Anyone pls tell me how to print patterns in c++
Like *. a. * *. a b * * *. a. b. c till 'n' lines Explanation pls
18 Antworten
+ 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 (){}
}
+ 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😂
+ 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++
+ 8
What?
+ 7
c++ is not each processed line wise
it will take 3 loops to print the above patterns
+ 7
@Yash thanks btw
+ 6
but what about the spaces
Ur code will give output as
*
**
***
+ 5
See pattern in my question
+ 5
c++
+ 3
Use for loops.
for (int i=1;i<=n;i++){
cout<<(n-i)*" "<<i*((" "+"*")
+ 3
in python it's easy
+ 2
buddy only in c++
+ 2
Thanks Verma
+ 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
+ 1
Verma make it pls
+ 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
+ 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
+ 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