In: Computer Science
Typed Code:
#include <iostream>
using namespace std;
int main()
{
int i = 0 , j =0;
for(int i = 0;i<4;i++){
for(int j = 0;j<=i;j++){ //Logic to print stars
cout<<"*";
}
cout<<"\n";//after every j loop we need new line
}
cout<<"///SECOND PATTERN//\n";
for(int i = 1;i<=4;i++){
for(int j = 1;j<=i;j++){ //in this logic we are printing j ,with
i starting from 1 to 4
cout<<j;
}
cout<<"\n";//after every j loop we need new line
}
cout<<"///THIRD PATTERN//\n";
for(int i = 1;i<=4;i++){ //in this logic we are printing i, with
i starting from 1 to 4
for(int j = 1;j<=i;j++){
cout<<i;
}
cout<<"\n";//after every j loop we need new line
}
cout<<"///FOURTH PATTERN//\n";
int k = 1;
for(int i = 1;i<=4;i++){
for(int j = 1;j<=i;j++){//in this logic we are printing a number
from 1 with incrementing
//, with i starting from 1 to 4
cout<<k++<<" ";
}
cout<<"\n";//after every j loop we need new line
}
cout<<"///FIFTH PATTERN//\n";
for(int i = 1,k = 1;i<=4;i++){//in this logic we are printing a
number from 1 with incrementing
//, with i starting from 1 to 4
for(int j = 1;j<=i;j++){
cout<<k++<<" ";
}
cout<<"\n";//after every j loop we need new line
}
cout<<"///SIXTH PATTERN//\n";
int n = 5;
int m = 2*n; // doubling the number bcz we need to iterate 2*n -1
times
for(int i = 0;i<m;i++){
int x = m/2; //taking half of the number
if(i < x) { //from 1 to less than half of number , it is in
inceasing order
for(int j = 0;j<(2*i + 1);j++){ //here 2i +1 gives odd number of
stars
cout<<"*";
}
} else { // after half of number we need to print decreasing
order
int z = (2*n - (2*i -6)); //logic to print odd number of
stars
for(int j = z ;j>=0;j--){ //printing stars with decreasing
order
cout<<"*";
}
}
cout<<"\n"; //after every j loop we need new line
}
return 0;
}
SCREEN SHOT :
OUTPUT: