-2
#include <stdio.h>
#include <math.h>

int main(void)
{
    // input value 
    
        int num, lower, upper;
        double squareroot;
        int square;
        int cube;

        printf("enter your number:\n");
        scanf_s("%d", &num);

        do
        {
            printf("the lower value limit is ");
            scanf_s("%d", &lower);
        } while (lower < 0 || lower > 50);

        do
        {
            printf("the upper value limit is ");
            scanf_s("%d", &upper);
        } while (upper < 0 || upper > 50);

        // the formular to find the squareroot, square, cube
        squareroot = sqrt(num);
        square = num * num;
        cube = num * num * num;

        //a for loop
        for (num = 0; num <= upper; num++) {
            
            printf("*base number*  ||  *square root*  ||  *square*  ||  *cube*\n");
            printf("*%d*           ||   *%f*      ||   *%ld*      ||  *%ld*\n",
                lower, squareroot, square, cube);
            
        }
    
    return 0;
}

i try to make a table to display the base number, square root, square, and cube and set a limit for the table. for example, if I input the lower number is 1 and the upper number is 5 then the table will stop at 5 then display the square root, square, and cube

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
aster dis
  • 13
  • 6
  • 1
    What is the purpose of the outer loop? What do you think the value of `lower` is in the initial `n < lower` condition? – Oka Sep 28 '22 at 00:43
  • lower is my input value – aster dis Sep 28 '22 at 00:52
  • @asterdis: However, you are reading the value of `lower` before input is written to it, which does not make sense. – Andreas Wenzel Sep 28 '22 at 00:56
  • ok, what change should i make – aster dis Sep 28 '22 at 00:59
  • 1
    @asterdis writing to variables before reading them – cryptxum Sep 28 '22 at 01:02
  • do { printf("the lower value limit is "); scanf_s("%d", &upper); } while (upper < 0 || upper > 50); like this – aster dis Sep 28 '22 at 01:05
  • @asterdis: Yes, that is better, although you seem to be confusing `lower` with `upper`. Note that this won't work if the user enters something like `abc`, though. In that case, your program may get stuck in an infinite loop. I have discussed this problem with `scanf` in [my answer to your previous question](https://stackoverflow.com/a/73860730/12149471). – Andreas Wenzel Sep 28 '22 at 01:09
  • ok got it, but want the actual output to come out is not my expect – aster dis Sep 28 '22 at 01:13
  • 3
    `squareroot = sqrt(lower); square = lower * lower; cube = lower * lower * lower;` should be inside the loop with the printf and should use the loop index instead of `lower`. The loop index should NOT be `lower` because that should be the initial value of the loop index. https://onlinegdb.com/klG8McTpX – Jerry Jeremiah Sep 28 '22 at 01:20
  • It's unfortunate that you've spent time trying to get the "layout" of the table to look nice when the program logic has not been solved and checked first. Layout of output should be the last worry. The prettiest table - that is incorrect - is pretty useless (and the extra code obscures what should be easy to see and correct.) – Fe2O3 Sep 28 '22 at 03:15

3 Answers3

2

At least this problem:

Mismatched specifiers/type

int square;
int cube;
...
printf("*%d*           ||   *%f*      ||   *%ld*      ||  *%ld*\n",
  lower, squareroot, square, cube);
        

Use "%d" with int, not "%ld".

Move assignments

Following assignments need to be inside the loop.

    for (num = 0; num <= upper; num++) {
      square = num * num;
      cube = num * num * num;

Save time. Enable all compiler warnings

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
1

Try the Below Code Make all Changes I have added Comments to Clarify why I made The

#include <stdio.h>
#include <math.h>

int main(void)
{
    // input value 
    int lower, upper;
    double squareroot;
    int square;
    int cube;

    //Read the Limits First
    printf("Enter the Lower Limit: ");
    scanf("%d", &lower);
    printf("Enter the Upper Limit: ");
    scanf("%d", &upper);
    
    //Instead of Declaring an Entire Loop You Can Just Use an If Statement this Reduces Code Complexity
    //You can also Set Your Limits
    if(upper > 0 && upper < 50 && lower > 0 && lower < 50)
    {
        //THen Enter Actual Code
        //Also Dont Set Lower to 0 It will Change Your Actual Value Instead Take another Loop Var
        for (int i = lower; i <= upper; i++) 
        {
            //Then Perform all Functions on i
            squareroot = sqrt(i);
            square = i * i;
            cube = i * i * i;
            printf("*base number*  ||  *square root*  ||  *square*  ||  *cube*\n");
            printf("*%d*           ||   *%f*      ||   *%d*      ||  *%d*\n", i, squareroot, square, cube);
        }
    }
    return 0;
}
Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/248427/discussion-on-answer-by-karthikanurag-how-to-display-the-table-in-c). – Samuel Liew Sep 29 '22 at 01:27
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 02 '22 at 14:06
1

Several issues:

  • You go to the trouble of asking for lower, but you don't use it to control your loop - your loop should be
    for( int num = lower; num <= upper; num++) 
    { 
      ... 
    }
    Put another way, if you always intend for your loop to start from zero, then you don't need lower at all.
  • You need compute the square root, square, and cube of num for each iteration of the loop. Instead, you're doing it once before the loop and just printing those same values over and over again;
  • You only need to print your table header once, outside the body of the loop;

You can compute your square root, square, and cube all as part of the printf statement:

printf( "%d %f %d %d\n", num, sqrt((double) num), num * num, num * num * num );

Field width specifiers are your friends - you can tell printf exactly how wide you want each column to be. Example:

printf( "%6d%12.2f%12d%12d", num, sqrt((double) num), num * num, num * num * num );

This means the column for num is 6 characters wide, the column for square root is 12 characters wide, with 3 characters reserved for the decimal point and two following digits, and the columns for square and cube are 12 characters wide. Example:

printf( "%6s%12s%12s%12s\n", "base", "root", "square", "cube" );
printf( "%6s%12s%12s%12s\n", "----", "----", "------", "----" );

for (int i = lower; i <= upper; i++ )
  printf( "%6d%12.2f%12d%12d\n", i, sqrt( (double) i ), i*i, i*i*i );

Which gives output like this (lower == 1, upper == 10):

  base        root      square        cube
  ----        ----      ------        ----
     1        1.00           1           1
     2        1.41           4           8
     3        1.73           9          27
     4        2.00          16          64
     5        2.24          25         125
     6        2.45          36         216
     7        2.65          49         343
     8        2.83          64         512
     9        3.00          81         729
    10        3.16         100        1000

Full example:

#include <stdio.h>
#include <math.h>

int main( void )
{
  int lower = 0, upper = 0;
  
  printf( "Gimme a lower value: " );
  while ( scanf( "%d", &lower ) != 1 || (lower < 0 || lower > 50 ))
  {
    /**
     * Clear any non-numeric characters from the input stream
     */
    while ( getchar() != '\n' ) 
      ; // empty loop
    printf( "Nope, try again: " );
  }

  printf( "Gimme an upper value: " );
  while ( scanf( "%d", &upper ) != 1 || (upper < lower || upper > 50 ))
  {
    while( getchar() != '\n' )
      ; // empty loop
    printf( "Nope, try again: " );
  }

  printf( "%6s%12s%12s%12s\n", "base", "root", "square", "cube" );
  printf( "%6s%12s%12s%12s\n", "----", "----", "------", "----" );

  for (int i = lower; i <= upper; i++ )
    printf( "%6d%12.2f%12d%12d\n", i, sqrt( (double) i ), i*i, i*i*i );

  return 0;
}

I've written the input section such that it will reject inputs like foo or a123. It will not properly handle inputs like 12w4, but that would make the example more complicated than it needs to be (you're not asking about input validation, you're asking about computation and formatting). Example run:

$ ./table
Gimme a lower value: foo
Nope, try again: a123
Nope, try again: 123
Nope, try again: 1
Gimme an upper value: 100
Nope, try again: 50
  base        root      square        cube
  ----        ----      ------        ----
     1        1.00           1           1
     2        1.41           4           8
     3        1.73           9          27
     4        2.00          16          64
     5        2.24          25         125
     6        2.45          36         216
     7        2.65          49         343
     8        2.83          64         512
     9        3.00          81         729
    10        3.16         100        1000
    11        3.32         121        1331
    12        3.46         144        1728
    13        3.61         169        2197
    14        3.74         196        2744
    15        3.87         225        3375
    16        4.00         256        4096
    17        4.12         289        4913
    18        4.24         324        5832
    19        4.36         361        6859
    20        4.47         400        8000
    21        4.58         441        9261
    22        4.69         484       10648
    23        4.80         529       12167
    24        4.90         576       13824
    25        5.00         625       15625
    26        5.10         676       17576
    27        5.20         729       19683
    28        5.29         784       21952
    29        5.39         841       24389
    30        5.48         900       27000
    31        5.57         961       29791
    32        5.66        1024       32768
    33        5.74        1089       35937
    34        5.83        1156       39304
    35        5.92        1225       42875
    36        6.00        1296       46656
    37        6.08        1369       50653
    38        6.16        1444       54872
    39        6.24        1521       59319
    40        6.32        1600       64000
    41        6.40        1681       68921
    42        6.48        1764       74088
    43        6.56        1849       79507
    44        6.63        1936       85184
    45        6.71        2025       91125
    46        6.78        2116       97336
    47        6.86        2209      103823
    48        6.93        2304      110592
    49        7.00        2401      117649
    50        7.07        2500      125000
John Bode
  • 119,563
  • 19
  • 122
  • 198