Home > Back-end >  how to display the table in c?
how to display the table in c?

Time:09-28

   #include<stdio.h>
#include<conio.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

CodePudding user response:

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

CodePudding user response:

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

#include<stdio.h>
#include<conio.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;
}
  • Related