Write C program to find factorial of any number


Introduction

Write C program to find factorial of any number


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

Calculating power of a number using while loop


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>

int main()
{
    int num,i;
    long long fact=1;

    // Reading a number from user
    printf("Enter any number to calculate factorial: ");
    scanf("%d",&num);
    fact = 1;
    i = 1;

      //Run loop from 1 to number entered by user
    while(i <= num)
    {
        fact = fact * i;
        i++;
    }
     printf("Factorial of %d = %lld", num, fact);
 return 0;
}

Calculating power of a number using for loop



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>

int main()
{
    int i, num;
    long long fact=1;

    // Reading a number from user
    printf("Enter any number to calculate factorial: ");
    scanf("%d", &num);

    //Run loop from 1 to number entered by user
    for(i=1; i<=num; i++)
    {
        fact = fact * i;
    }

    printf("Factorial of %d = %lld", num, fact);

    return 0;
}

Result

C language

     





Related Topics




Popular Posts