c programming exercise

This is a discussion on c programming exercise within the C Programming forums, part of the General Programming Boards category; i need to write the code for this problem: Q:write a c program to print a pyramid of digits as ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    26

    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.

  2. #2
    Registered User
    Join Date
    Oct 2009
    Posts
    26
    the pyramid should have been like this:
    1
    2 3 2
    3 4 5 4 3
    4 5 6 7 6 5 4

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    10
    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 01:42 AM.

  4. #4
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    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!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. programming exercise
    By mashour06 in forum C Programming
    Replies: 1
    Last Post: 06-01-2009, 07:22 AM
  2. Line of data input method
    By larry_2k4 in forum C Programming
    Replies: 2
    Last Post: 04-29-2009, 12:34 AM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. any recommended exercise book for C++?
    By gogo in forum C++ Programming
    Replies: 5
    Last Post: 11-07-2001, 04:44 PM

Tags for this Thread


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