Monday 28 January 2019

c examples 3

#tutorial18#
Loop examples 
// Program to find factorial of a number
// For a positive integer n, factorial = 1*2*3...n

#include <stdio.h>
int main()
{
    int number;
    long long factorial;

    printf("Enter an integer: ");
    scanf("%d",&number);

    factorial = 1;

    // loop terminates when number is less than or equal to 0
    while (number > 0)
    {
        factorial *= number;  // factorial = factorial*number;
        --number;
    }

    printf("Factorial= %lld", factorial);

    return 0;
}
 
Output

Enter an integer: 5
Factorial = 120

No comments:

Post a Comment

Note: only a member of this blog may post a comment.