![]() |
| | #1 |
| Registered User Join Date: Oct 2009
Posts: 2
| c programming exercise Q:write a c program to print a pyramid of digits as shown below for n number of lines. 1 2 3 2 3 4 5 4 3 4 5 6 7 6 5 4 -- -- -- -- -- -- -- -- Actually i have the code as well.But something is wrong. heres the code: Code: /*program to print digits in pyramidal form*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,l,m,n;
clrscr();
printf("\n how many lines?");
scanf("%d",&n);
/*loop 1 to print n lines*/
for(l=1;l<=n;l++)
{
/*loop 2 to print spaces*/
for(i=1;i<=n-l;i++)
printf(" ");
/*loop to print digits in the left side of axis*/
m=l;
for(j=1;j<=l;j++)
{
printf("%6d",m);
m++;
}
/*loop to print digits rightside of axis*/
m=m-2;
for(k=1;k<l;k++)
{
printf("%6d",m);
m--;
}
printf('\n");
}
getch();
}
side of the axis came. i am unable to figure out the mistake in the code. thanx in advance. |
| Pulock2009 is offline | |
| | #2 |
| Registered User Join Date: Oct 2009
Posts: 2
| the pyramid should have been like this: 1 2 3 2 3 4 5 4 3 4 5 6 7 6 5 4 |
| Pulock2009 is offline | |
| | #3 |
| Registered User Join Date: Oct 2009
Posts: 8
| Code: #include <stdio.h>
int main(void){
int i, j, space, repeats;
repeats = 5;
for(i = 1; i <= repeats; i++){
space = repeats-i;
while(space-- > 0) printf(" ");
for(j = i; j < 2*i-1; j++)
printf("%d", j);
for(j = 2*i-1; j > i-1; j--)
printf("%d", j);
putchar('\n');
}
}
if you want spaces between the numbers you can change : space = repeats-i to space = 2*(repeats-i) and the printf statements to printf("%d ", j); also only works at a max of 5 lines. Last edited by emef; 10-30-2009 at 12:42 AM. |
| emef is offline | |
| | #4 | |
| Webhead Join Date: Jul 2009
Posts: 278
| Quote:
Code: printf('\n");
Code: printf("\n");
__________________ Spidey out! | |
| Spidey is offline | |
![]() |
| Tags |
| code, pyramid of numbers |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| programming exercise | mashour06 | C Programming | 1 | 06-01-2009 06:22 AM |
| Line of data input method | larry_2k4 | C Programming | 2 | 04-28-2009 11:34 PM |
| Tutorial review | Prelude | A Brief History of Cprogramming.com | 11 | 03-22-2004 09:40 PM |
| Request for comments | Prelude | A Brief History of Cprogramming.com | 15 | 01-02-2004 10:33 AM |
| any recommended exercise book for C++? | gogo | C++ Programming | 5 | 11-07-2001 04:44 PM |