//only two for loop are used to create pyramid and patterns. Time complexity of the following //program is O(n^2).
#include<stdio.h>
main()
{
int i,j,k=1,m,l;
printf("Enter the height of the pyramid");
scanf("%d",&l);
m=l;
for(i=1;i<=l;i++)
{
for(j=1;j<l+i;j++)
{
if(k==0)
{
if((j%2==0) && j>=m && (j-m)<2*i)
printf("%c",'*');
else
printf(" ");
}
else if(k==1)
{
if(j%2!=0 && j>=m && (j-m)<2*i)
printf("%c",'*');
else
printf(" ");
}
}
printf("\n");
if(k==1)
k=0;
else
k=1;
m=m-1;
}
}
I/O of a program
Enter the height of the pyramid 7
#include<stdio.h>
main()
{
int i,j,k=1,m,l;
printf("Enter the height of the pyramid");
scanf("%d",&l);
m=l;
for(i=1;i<=l;i++)
{
for(j=1;j<l+i;j++)
{
if(k==0)
{
if((j%2==0) && j>=m && (j-m)<2*i)
printf("%c",'*');
else
printf(" ");
}
else if(k==1)
{
if(j%2!=0 && j>=m && (j-m)<2*i)
printf("%c",'*');
else
printf(" ");
}
}
printf("\n");
if(k==1)
k=0;
else
k=1;
m=m-1;
}
}
I/O of a program
Enter the height of the pyramid 7
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
No comments:
Post a Comment