Saturday 5 March 2022

NPTEL - WEEK 3 ASSIGNMENT 3 QUESTION 03

 QUESTION :

Write a C program to list all the factorial numbers less than or equal
to an input number n.

A number N is called a factorial number if it is the factorial of a
positive integer. For example, the first few factorial numbers are

   1, 2, 6, 24, 120, ...

*Note* - We do not list the factorial of 0.

Input
-----
A positive integer, say n.

Output
------
All factorial numbers less than or equal to n.


SOLUTION :

#include<stdio.h>

int main()
{
    int n, i, j, fact = 1;

    scanf("%d", &n);

    for(i=1; i<n; i++)
    {
            fact = 1;
            for(j=1; j<=i; j++)
            {
                fact *= j;
            }
            if(fact <= n)
            {
               printf("%d ", fact);
            }
            else
            {
               break;
            }
    }
    return 0;
}


No comments:

Post a Comment