Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am writing one factorial C program in which user is not allowed to enter negative and alphabet character but number from 0 is allowed. The program will ask for input until enter key is pressed.

The program works fine when I enter any number or press enter key but when I enter any character it goes into infinite loop giving output as "factorial of 1 is 1". Why is this happening?

Also when I enter ASCII value of alphabet character(e.g. 109) I am getting output as "alphabet is not allowed" but the expectation is factorial value of that number. I know this is because I am using same variable to check for alphabet and number. Then how do I check for alphabet and number separately? as input value is stored in one variable.

In output I am getting factorial upto 33 number only. If I enter 34 or any number greater than 34 then I am getting output as "factorial of [entered number] is 0". Why the factorial value is 0 for number greater than 33?

Kindly guide me where I am making mistake.

What I have tried:

C
#include <stdio.h>
#include <stdlib.h>

int display();
void fact_fun(int num_fact);

int main()
{
    int num = 0;
    char next;

    next=display();

    while( next != '\n')
    {
        num = next;
        fact_fun(num);
        next=display();
    }

    return 0;
}

int display()
{
    int input;
    printf("\nEnter number to find factorial or press ENTER to exit :");
    scanf("%d",&input);

    return input;
}

void fact_fun(int num_fact)
{
    int fact=1;
    if((num_fact>='a' && num_fact<='z') || (num_fact>='A' && num_fact<='Z'))
    {
        printf("Alphabet is not allowed");
    }
    else if(num_fact < 0)
    {
        printf("Negative number is not allowed");
    }
    else if(num_fact == 0)
    {
        printf("Factorial of %d is 1",num_fact);
    }
    else
    {
        for(int i = 1; i<= num_fact; i++)
        {
            fact = fact*i;
        }
        printf("Factorial of %d is %d",num_fact,fact);
    }
}
Posted
Updated 23-Sep-18 3:49am

The problem is that you don't do any error checking: C library function - scanf()[^] returns a negative number if a "bad value" is entered and you do not check for this, you just assume that the value in input is always correct and valid.

Modify your display function to check the response, and repeat until the user types something right. And change its name! It doesn't "display" it prompts for input and gets a value from the user.

But that's not your only problem: because you use scanf with a numeric code - "%d" - it will never return '\n', so that won;t work too well either.

What I'd suggest is that you use scanf to read a line of characters from the user, and then check that for "empty" - if it's empty, then the user pressed ENTER without a number and you are done. If it isn't, then use C library function - atoi()[^] to try and convert it to an integer. You can then throw out your A-Z checking and just get on with the factorial.

Do be aware that factorials get very large, very quickly: they will exceed an 32 bit integer long before n reaches 33!
 
Share this answer
 
You need to check the result of your scanf call to make sure that the user typed a valid number. See scanf, _scanf_l, wscanf, _wscanf_l[^] for the return value from a scanf call. Note also that your display function will never return the '\n' character, as it only accepts integer input. You need to use a special value to indicate the end.

This code is redundant:
C++
if((num_fact>='a' && num_fact<='z') || (num_fact>='A' && num_fact<='Z'))
{
    printf("Alphabet is not allowed");
}

You already have an integer value (checked in the display function), so comparing it to an arbitrary range of numbers does not mean what you think.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900