Loop

This is a discussion on Loop within the C Programming forums, part of the General Programming Boards category; Hi I want the program to take in a number n, and then print n,n-1,n-2 till 1 the next line, ...

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

    Loop

    Hi
    I want the program to take in a number n, and then print n,n-1,n-2 till 1
    the next line, it should start from n-1,n-2 till 1
    then n-2,n-3 till 1

    eg n=5, then
    54321
    4321
    321
    21
    1

    I tried but it doesnt work as I though.

    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int n,i=0;
    clrscr();
    printf("Enter value of N\n");
    scanf("%d",n);
    while(n!=0)
     {
     for(i=n;i>=1;i--,n--)
     {
     printf("%d",i);
     printf("\n");
     }
    }
    getch();
    }
    any help?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You probably only want n to go down once per line, not one time for every digit printed.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    2
    fixed it

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    void main()
    {
    int n, row,col;
    clrscr();
    printf("enter n\n");
    scanf("%d",&n);
    for(row=n; row>=1; row--)
     {
     for(col=row; col>=1; col--)
     {
     printf("%d ",col);
     }
     printf("\n");
     }
    
    for(row=1; row<=n; row++)
     {
     for(col=1; col<=row; col++)
     {
     printf("%d ",col);
     }
     printf("\n");
    }
    getch();
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Poll event loop
    By rogster001 in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2009, 04:28 AM
  2. need help with a loop
    By Darkw1sh in forum C Programming
    Replies: 19
    Last Post: 09-13-2009, 09:46 PM
  3. funny-looking while loop
    By Aisthesis in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2009, 11:54 PM
  4. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 08:14 AM

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