-2

I have added this logic to create right angle and i expect this output

10  
9  4 
8  5  3
7  6  2  1

but i'm getting

1
3   2
4   5  6
10  9  8  7

how can i fix this this?

and this is my code

#include<stdio.h>
int main()
{
   int n, r, c;
   int a=1, b;

   printf("Enter number of rows: ");
   scanf("%d",&n);

   for(r=1; r<=n; r++)
   {
     b=a+r-1;
     for(c=1; c<=r; c++, a++)
     {
       if(r%2==1) printf("%5d",a);
       else printf("%5d",b--);
     }

     printf("\n");
   }

   return 0;
}
  • Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine at which point your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Andreas Wenzel Apr 07 '22 at 16:50
  • yes but still not getting – Dhanesh Joshi Apr 07 '22 at 16:53
  • Use meaningful variable names, otherwise people are left to speculate what the variables are by their code. That said, think about your very first number. How could it possibly be `10` given the code above, if you're stepping through it line-by-line? – Rogue Apr 07 '22 at 16:54
  • its comming from the last first line and that use to be in left side – Dhanesh Joshi Apr 07 '22 at 16:58
  • Should the first number (the upper left one) always be the greater and the first column (the left one) always be descending? – Bob__ Apr 07 '22 at 22:55

2 Answers2

1

Here is my solution:

In order to calculate the individual numbers, I first make a function get_highest_value_in_column, which calculates the highest value in a given column. I then calculate the individual numbers based on the position of the number in the column, taking into account whether the column is an even column (which has descending numbers) or an odd column (which has ascending numbers).

#include <stdio.h>

int get_highest_value_in_column( int column )
{
    return column * ( column + 1 ) / 2;
}

int main( void )
{
    int n;

    //set variable "n" based on user input
    printf( "Enter number of rows: " );
    scanf( "%d", &n );

    //print one row per loop iteration
    for ( int i = 0; i < n; i++ )
    {
        //print one number per loop iteration
        for ( int j = 0; j <= i; j++ )
        {
            //add a space between numbers
            if ( j != 0 )
                putchar( ' ' );

            //find value of highest number in column
            int num = get_highest_value_in_column( n - j );

            //adjust number depending on position in column
            if ( j % 2 == 0 )
                //even row
                num -= i - j;
            else
                //odd row
                num -= n - 1 - i;

            //print the calculated number            
            printf( "%3d", num );
        }

        //print newline character to end the row
        putchar( '\n' );
    }
}

This program has the following output:

Enter number of rows: 4
 10
  9   4
  8   5   3
  7   6   2   1
Enter number of rows: 5
 15
 14   7
 13   8   6
 12   9   5   2
 11  10   4   3   1
Enter number of rows: 6
 21
 20  11
 19  12  10
 18  13   9   4
 17  14   8   5   3
 16  15   7   6   2   1
Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
-3

Yes This is correct answer

#include <stdio.h>

int get_highest_value_in_column( int n )
{
    return n* ( n+1 ) / 2;
}

int main( void )
{
    int n;
    printf( "Enter number of rows: " );
    scanf( "%d", &n );
    for ( int i = 0; i < n; i++ )
    {
        for ( int j = 0; j <= i; j++ )
        {
            if ( j != 0 )
                printf(" ");
             int num = get_highest_value_in_column( n - j );

            if ( j % 2 == 0 )
                
                num -= i - j;
            else
                
                num -= n - 1 - i;

            
            printf( "%3d", num );
        }

        printf("\n");
    }
}
  • 1
    A *good* answer, while generally having the necessary code, also needs to have an explanation of *why* this code or code changes solve the problem. A code dump with no explanation is generally not helpful to help someone understand how to fix the problem. – crashmstr Apr 13 '22 at 11:36