star patterns using loops

Write a C++ program that prints the star patterns using nested for loops. It should have a rectangular, triangle and pyramid pattern.

#include<iostream>
using namespace std;
int main(){
	int n = 5;
	cout<<"Pattern 1 :"<<endl;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			cout<<"*";
		}
		cout<<endl;
	}
	cout<<"Pattern 2 :"<<endl;
	for(int i=0;i<n;i++){
		for(int j=i;j<n;j++){
			cout<<"*";
		}
		cout<<endl;
	}
	cout<<"Pattern 3 :"<<endl;
	for(int i=0;i<n;i++){
		for(int j=0;j<=i;j++){
			cout<<"*";
		}
		cout<<endl;
	}
	cout<<"Pattern 4 :"<<endl;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			if((i+j)<(n-1))cout<<" ";
			else cout<<"*";
		}
		cout<<endl;
	}
	cout<<"Pattern 5 :"<<endl;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			if((i+j)<(n-1))cout<<" ";
			else cout<<"* ";
		}
		cout<<endl;
	}
	return 0;
}
output of the above program. star patterns
output of the above program. star patterns

Leave a Reply