Printing Pyramids
program 1: ********* ****_**** ***___*** **_____** *_______* #include<stdio.h> #include<conio.h> void main() { int i,j,x=0,c; for(i=5;i>=1;i--) { for(j=1;j<=i;j++) { printf("*"); } for(c=(2x-1);c>=1;c--) { printf(" "); } for(j=i;j>=1;j--) { if(j!=5) { printf("*"); } } x++; printf("\n"); } getch(); } program 2: 1234554321 1234__4321 123____321 12______21 1________1 #include<stdio.h> #include<conio.h> void main() { int i,j,x=0,c; for(i=5;i>=1;i--) { for(j=1;j<=i;j++) { printf("%d",j); } for(c=2x;c>=1;c--) { printf(" "); } for(j=i;j>=1;j--) { printf("%d",j); } x++; printf("\n"); } getch(); } program 3: 1234554321 1234__4321 123____321 12______21 1________1 #include<stdio.h> #include<conio.h> void main() { int i,j,x=0,c; for(i=5;i>=1;i--) { for(j=1;j<=i;j++) { printf("%d",j); } for(c=2x;c>=1;c--) { printf(" "); } for(j=i;j>=1;j--) { printf("%d",j); } x++; printf("\n"); } getch(); } Program 4: ____1 ___121 __12321 _1234321 123454321 #include<stdio.h> #include<conio.h> void main() { int i, j, x=7,num=1; for(i=1;i<=5;i++) { for(j=1;j<=x;j++) { printf(" "); } for(j=1;j<=i;j++) { printf("%d",num); num++; } num--; for(j=1;j<=(i-1);j++) { num--; printf("%d",num); } num=1; printf("\n"); x--; } } program 5: 12345 1234 123 12 1 #include<stdio.h> #include<conio.h> void main() { int i,j; for(i=1;i<=5;i++) { for(j=1;j<=5;j++) { printf("%d",j); } printf("\n"); } getch(); }
Comments
the out put of last programm is as follows
1
12
123
1234
12345
oops!
in the first for loop i should be intialized to 5!
then you will get the correct o/p
thanks for visiting!
In your program i should be decremented by 1(instead of increment by 1) and i>=1 will be the conditon. Also in the inner for loop the condition should be j<=i
heres a program for displaying a pyramid of stars with 10 rows with “A SPACE BETWEEN THEM” as follows:
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
CODE:
#include
main()
{
int i,j, x=10,f;
f=x;
for(i=1;i<=f;i++)
{
for(j=1;j<=x;j++)
{
printf(" ");
}
for(j=1;j<=i;j++)
{
printf("* ");
}
printf("\n");
x–;
}
}
the output is not correctly visible int he post.
u can execute and see the output.
i m non electrical first year coepian.
i rhought this program can also be included in ur pyramids section.
so,…
actually the formating is incorrect! we are having some problem with the formating!
and by the way it doesn’t matter whether you are from electrical or non-electrical stream!
thanks for the contribution we will definitely include it!
can u provide the program for the following output
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
You want your output in the shape of an arrow or a rectangle ?
Becouse the 2digit no. may cause formatting difference.
rectangle and the numbers must display in sequence like 1,then 2….4..then 5 as in the position shown
#include
int main()
{
int n,c=97,i,j;
printf(“Enter the n factor (odd essentially)in producing n*n matrix”);
scanf(“%d”,&n);
int a[n][n],start,end;
for(i=0;i<n;i++) // To initialize the array to zero….
{
for(j=0;j<n;j++)
a[i][j]=0;
}
for(start=0,end=n-1;start!=end;start++,end–)
{
for(j=start,i=start;j<=end;j++){ a[i][j]=c++; }
j=end;
for(i=start+1;i=start;j–) {a[i][j]=c++;}
j=start;
for(i=end-1;i>=start+1;i–){ a[i][j]=c++;}
}
a[n/2][n/2]=c;
for(i=0;i<n;i++) // Display the array
{
for(j=0;j<n;j++)
printf("%c ",a[i][j]);
printf("\n");
}
return 0;
}
I know Its too late…Your Exams got over and You had the same code to write