+ 1
How to print below pattern using for loop?
XXXXX XXXX XXX XX X
10 odpowiedzi
+ 1
check this out:
#include <iostream>
using namespace std;
int main() {
int lines = 6, x = 6;
for(int i=0; i<lines; i++)
{
for(int j=0; j<x; j++)
{
cout<<"X";
}
cout<<endl;
x--;
}
return 0;
}
+ 3
You can use an if-else statement to the second for loop and print X if first initial variable >= second else " "
+ 2
Here's what you're looking for.
https://code.sololearn.com/ctIAO5SefrJ7/?ref=app
+ 2
#include <iostream>
using namespace std;
int main()
{
for(int l=5;l>=1;l--)
{
for(int s=1;s<=5-l;s++)
{
cout<<" ";
}
for(int v=1;v<=l;v++)
{
cout<<"X";
}
cout<<endl;
}
return 0;
}
Outer most for loop is used to display lines and the inner for loop is used to print Spaces from the left side and the most inner for loop is used to print variable X on the screen. And last, statement "cout<<endl;" is used to move on next line.
+ 2
Bhupender Singh
#include<iostream>
using namespace std;
int main() {
int n=5;
for(int i=5;i>0;i--){
int space=n-i;
for(int j=1;j<=space;j++){
cout<<" ";//for printing spaces
}
int star=i;
for(int j=1;j<=star;j++){
cout<<"X";
}
cout<<endl;
}
}
Here is right solution for this pattern
+ 1
Thanks man
But this is not what I'm looking for
+ 1
I have tried this one
X
XX
XXX
XXXX
XXXXX
but i dont know how to add space in the beginning.
0
I will try
0
for i in range(5):
for j in range(5):
if(j<i):
print(" ")
else:
print("x")
print("\n")
In python