Follow This Blog For more... 😊

C Program to accept number and print it's factorial.| Using C language| C Program series 49|

👨‍💻C Program to accept number and print it's factorial.

Code :

    /*
    C Program to accept number and print it's factorial.
    For Example:n=5
   
    5! = 120
   
    */
   
    #include<stdio.h>
    main()
    {
        printf("C Program to accept number and print it's factorial.\n\n");
       
        int i,n;
        float factorial=1;//using float data type for handling very big values.
        //For example int data type cannot handle the answer of 15!.
       
        printf("Enter positive number:");
        scanf("%d",&n);
       
        if(n<0)
        {
            printf("Invalid input (%d)!\nOnly positive value.",n);
            return 0;
        }
       
        for(i=1;i<=n;i++)
        {
            factorial=factorial*i;
        }
        printf("==> %d! = %.0f",n,factorial);
        //%.0f means zero decimals after the decimal point.
    }

The output of Code:





👉Click for more C Program series Questions.[Questions list]

Comments

Popular Posts