Click here to Skip to main content
15,919,479 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
4 3 2 1 2 3 4
3 2 1 2 3
2 1 2
1

What I have tried:

#include <stdio.h>

int main()
{
    int i, space, rows,col;



    for(rows=4; rows>=1; rows--)
    {
        
         for(col=rows;col>=1;col--)
       {
           printf(" %d",col);
       }

 for(col=2;col<=rows;col++)
       {
           printf(" %d",col);
       }

        printf("\n");
    }

    return 0;
}
Posted
Updated 21-Oct-17 21:52pm
v2
Comments
CHill60 21-Oct-17 9:48am    
What is wrong with your code?
Dinesh Sahoo 22-Oct-17 1:15am    
i need the first element of the 2nd row (3) exactly below the 3 of first rows.
an so on.then in 3rd (2) exactly below the (2) like this at last only 1 is printed under (1) .

And your code - while badly indented - works. When I copy your code into an online IDE and run it I get:
 4 3 2 1 2 3 4                                                           
 3 2 1 2 3                                                               
 2 1 2                                                                   
 1                                                                       
                                                                         
                                                                         
...Program finished with exit code 0
So what am I doing that you are not?
 
Share this answer
 
Learn to indent properly your code, it show its structure and it helps reading and understanding.
C++
#include <stdio.h>

int main()
{
    int i, space, rows,col;
    for(rows=4; rows>=1; rows--)
    {
        for(col=rows;col>=1;col--)
        {
            printf(" %d",col);
        }
        
        for(col=2;col<=rows;col++)
        {
            printf(" %d",col);
        }
        
        printf("\n");
    }

    return 0;
}

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]

Do you have a problem with this code ?
 
Share this answer
 
Is this is the output you are looking for:
4 3 2 1 2 3 4
  3 2 1 2 3
    2 1 2
      1


You need to output spaces for the overhangs, this means you always have to print from 4, even if only an empty field. The code change is here:
int main()
{
    int rows, col;

    for (rows = 4; rows >= 1; rows--)
    {
        for (col = 4; col >= 1; col--)
        {
            if (col <= rows)
            {
                printf(" %d", col);
            }
            else
            {
                printf("  ");
            }
        }
        for (col = 2; col <= 4; col++)
        {
            if (col <= rows)
            {
                printf(" %d", col);
            }
            else
            {
                printf("  ");
            }
        }

        printf("\n");
    }

    return 0;
}
 
Share this answer
 
Comments
Dinesh Sahoo 22-Oct-17 8:38am    
thank you....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900