Print this pattern.. let's see which one is efficient.. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Print this pattern.. let's see which one is efficient..

where no of lines is given as input. ex. lines=3 * * *** *** ***** *****

10th Nov 2016, 12:05 PM
gowtham
gowtham - avatar
3 Answers
+ 3
#include <iostream> using namespace std; int main() { int n; cin>>n; for(int i=0;i<n;i++){ for(int j=n-i;j>1;j--) cout<<" "; for(int j=0;j<2*i+1;j++) cout<<"*"; for(int k=0;k<2*(n-i)-1;k++) cout<<" "; for(int l=0;l<2*i+1;l++) cout<<"*"; cout<<endl; } } edit: if someone has more efficient code, do post it.
10th Nov 2016, 12:38 PM
kamal joshi
kamal joshi - avatar
+ 1
I don't know it is efficient or not but it also do the same thing #include <iostream> using namespace std; int main() { int i,j,n=0; for(i=0;i<6;i+=2) { n=0; step: for(j=4+n;j>=i;j--) { if(n==0) cout << " "; else cout << " "; } for(j=0;j<i+1;j++) { cout << "* "; } if(n==0) { ++n; goto step; } cout << "\n"; } return 0; }
10th Nov 2016, 4:47 PM
Aditya kumar pandey
Aditya kumar pandey - avatar
0
using goto statement is not considered to be efficient.. and your codes time complexity is more than the other
10th Nov 2016, 6:13 PM
gowtham
gowtham - avatar