Untitled

 avatar
unknown
c_cpp
4 years ago
856 B
6
Indexable
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdio.h>
long long unsigned int fact(int num);
int chkdig(char string[]);

int main(){
    char a[50]={0};
    while(1){
      printf("Enter a number:");
      scanf("%s",a);
      if(strcmp(a, "#") == 0){
        break;
      }
        while(chkdig(a)!=1){
            printf("Enter a valid number:");
            scanf("%s",a);
        }
        int num = atoi(a);
        printf("%lld\n",fact(num));
      }
}

int chkdig(char string[]){
    int num=0;
    for (int i=0;i<strlen(string);i++){
        if (isdigit(string[i])){
            num+=1;
        }
    }
    if (num==strlen(string)){
        return 1;
    }
    else{
        return 0;
    }
}
long long unsigned int fact(int num){
    if (num>=2){
        return num*fact(num-1);
    }
    else{
        return num;
    }
}

Editor is loading...