Thread: loop help print this

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    19

    loop help print this

    Code:
    #include<stdio.h>
    
    main()
    
    {
     int n, c, k, space;
     
     printf("Enter the number of rows: ");
     scanf("%d", &n);
     
     space=n;
      
     for(k=n;k>=1;k--)
             {for(c=1; c<=space; c++)
                     printf(" "); space++;
                         for(c=1; c<=k; c++)
                              printf("%d",k);
                             printf("\n");
             }
     return 0;
     }


    sss4
    ss34
    s234
    1234

    s for space....
    can any one help me??

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    19
    preview must be like this:
    sss4
    ss34
    s234
    1234

    s= space

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Well, right away, the number of spaces equals the current row number being printed - 1, if you assign the current row number to the total row number, and then decrement it, in the loop.

    In this code you have a for loop that looks like it's nested inside another for loop, immediately above it, but it's not. Indent your code, and you'll be surprised how easy it is to find some errors, and for others to study it:

    Code:
            for(c=1; c<=space; c++)
                     printf(" "); space++;
                         for(c=1; c<=k; c++)
                              printf("%d",k);
    
    
    //it should look like (but I wouldn't use this logic).
            for(c=1; c<=space; c++)
               printf(" "); space++;
            for(c=1; c<=k; c++)
               printf("%d",k);
    The second example of the code looks substantially different.

    If the total row number to print is 4, you will start printing digits with that value as the current row number (4). If current row number is 3, you will print the current row number -1, or two digits. The starting digit will be the current row number (3).

    Your for loop will thus start with the current row number (which starts with the total row number to be printed), and end with the total row number (4 in this case).

    Give that a shot.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    19
    thanks sir..

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    19
    can you post the right code?? please...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 11-10-2011, 09:24 PM
  2. using user-defined function, do/while loop, to print name
    By jackson6612 in forum C++ Programming
    Replies: 8
    Last Post: 05-08-2011, 06:02 AM
  3. Print matrice with one loop?
    By cowa in forum C Programming
    Replies: 3
    Last Post: 12-25-2009, 08:34 PM
  4. Add a Loop to print the output?
    By big-tony in forum C Programming
    Replies: 23
    Last Post: 05-22-2009, 10:38 AM
  5. Print from 1 to 10 without using a loop
    By Guti14 in forum C++ Programming
    Replies: 11
    Last Post: 12-23-2003, 10:02 PM