Thursday, June 18, 2020

Write a program to print the given below pattern 4

Sample Input:

3

Sample Output:

112
322
334

Sample Input:

5

Sample Output:

11112
32222
33334
54444
55556

SOLUTION:

#include<iostream> 
using namespace std; 
void printPattern(int n) 
{ 
	int j, k = 1; 
	// loop to decide the row number 
	for (int i = 1; i <= n; i++) 
	{ 
		// if row number is odd 
		if (i%2 != 0) 
		{ 
			for (j = 0; j < n-1; j++) 
				cout << k; 
			cout << ++k << endl;  
		} 
		
		// if row number is even 
		else
		{ 
          	cout << ++k;
          	k--;
			for (j = 0; j < n-1; j++ ) 
				cout << k ; 	
          	cout<<endl;
          	k++;
		} 
	} 
} 
int main() 
{ 
	int n ;
  	cin>>n;
	printPattern(n); 
	return 0; 
} 

No comments:

Post a Comment

horizontal ads