0
Print this pattern.. let's see which one is efficient..
where no of lines is given as input. ex. lines=3 * * *** *** ***** *****
3 odpowiedzi
+ 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.
+ 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;
}
0
using goto statement is not considered to be efficient.. and your codes time complexity is more than the other