C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-29-2009, 08:28 PM   #1
Registered User
 
Join Date: Oct 2009
Posts: 2
c programming exercise

i need to write the code for this problem:
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();
}
i ran the code on borlands turbo c++ 3.0 and in the output only the right
side of the axis came. i am unable to figure out the mistake in the code.
thanx in advance.
Pulock2009 is offline   Reply With Quote
Old 10-29-2009, 08:33 PM   #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   Reply With Quote
Old 10-30-2009, 12:32 AM   #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');
	}
}
edit:
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   Reply With Quote
Old 10-30-2009, 02:48 AM   #4
Webhead
 
Spidey's Avatar
 
Join Date: Jul 2009
Posts: 278
Quote:
i ran the code on borlands turbo c++ 3.0 and in the output only the right
side of the axis came. i am unable to figure out the mistake in the code.
thanx in advance.
it worked fine for me on visual studio except for this line -

Code:
printf('\n");
which should be

Code:
printf("\n");
__________________
Spidey out!
Spidey is offline   Reply With Quote
Reply

Tags
code, pyramid of numbers

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 05:52 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22