Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How to let the program Refuses

+1 vote
asked Oct 23, 2019 by anonymous

Refuses the entry if the user enters a float and keeps asking the user to enter an integer until the user responds properly and enters an integer.

i make this program but i don't know how to make the right way if there is better way will be great

include<stdio.h>

#include<stdlib.h>

int main()

{

    int a;

    int b;

    int f=1;

   

    printf("Enter integer: ");

    scanf("%d\n",&a);

   

    while(scanf("%d",&a)==0)

    {

        printf("Try again (Please enter an integer)\n");

    }

   

    for (b=1;b<=a;b++)

    {

        f*=b;

    }

   

    printf("The factorial of %d = %d\n",a,f);

    return 0;

}

1 Answer

0 votes
answered Oct 25, 2019 by gameforcer (2,990 points)

It's because you used a variable with the type of integer:

int a;

Instead you should do

float a;

 or if you want to have even better precision:

double a;

Besides that you need to change the expected type of variable in your input and output functions.

http://www.cplusplus.com/reference/cstdio/printf/ - if you look in the table at the top you can see that you used a symbol for signed integer type - %d. Similarly you should use %f for float and %lf for double.

Example:

double a;

scanf("%lf\n",&a);

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...