how do i make sure the user put a whole number and not a decimal in c?

I am making a code that calculate the surface area of a polygon. the user must, among others, enter the number of sides to the polygon which cannot be a decimal. I need it to fit into a condition of "if":

if (side_a < 0 || radius < 0 ||  diameter < side_a || num_pol<3 ||...

the code must mean: "if num_pol is decimal"

also I must calculate the area of a polygon which has a circle in it. the formula is: radius^2 * number of sides * tangent(180/num of sides). so I wrote in C:

pow(radius,2)*num_pol*tan(P/(2*num_pol)) //P is pi//

is it correct? thank you.

2 answers

  • answered 2018-04-14 14:30 Maxime Chéramy

    how do i make sure the user put a whole number and not a decimal in c?

    Use an integer type such as int.

    As for the formula, the best way to know is to test your program with various examples. This advice works for all your programs: test your code before asking someone else to review it.

  • answered 2018-04-14 14:30 Abhishek Keshri

    You can use: ceilf(side) == side && side > 0

    Here is the verification code:

    #include <stdio.h>
    #include<math.h>
    
    int main(void) {
        // your code goes here
        double side;
        side = 3.0;
        if(ceilf(side) == side && side > 0)
            printf("true");
        else
            printf("false");
        side = 3.1;
        if(ceilf(side) == side && side > 0)
            printf("true");
        else
            printf("false");
        side = 3.6;
        if(ceilf(side) == side && side > 0)
            printf("true");
        else
            printf("false");
        side = -3.6;
        if(ceilf(side) == side && side > 0)
            printf("true");
        else
            printf("false");
        return 0;
    }