Untitled

mail@pastecode.io avatar
unknown
c_cpp
3 years ago
1.0 kB
3
Indexable
Never
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

long long factorial(long long a){
    a = (long long)a;
    long long factorial_of_a = 1;
    while(a != 0){
        factorial_of_a *= a;
        a--;
    }
    return factorial_of_a;
}

int main(void)
{
    int num;
    char num_input[100]={0};

//    printf("Please input a positive integer: ");
//    scanf("%s", num_input);
//
//    num = atoi(num_input);

//    num_input = 'q';

    while(1){
        printf("Please input a positive integer: ");
        scanf("%s", num_input);
        if (strcmp(num_input, "#") == 0){
          break;
        }
        num = atoi(num_input);
        while(num < 0){
            printf("You have input a non-positive integer, please input a positive integer: ");
            scanf("%s", num_input);
            num = atoi(num_input);
            }
            printf("The factorial of %d is %lld.\n", atoi(num_input), factorial(num));
            puts("");
        }
    printf("Finished.\n");
    return 0;
    }