Click here to Skip to main content
15,940,921 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Consider a scenario with 'T' test cases. Hence, for each test case 'ti', let there be any positive number 'n'. Since, factorial of a number 'n' can be computed through the following formula:

n! = n × (n − 1) × ... × 3 × 2 × 1

Find a value, named as 'result', by adding all the digits received through a factorial of a number 'n'.

Input Format

The First line contains T, the number of test cases, followed by integer 'n'.




Output Format

Sum of the digits of a computed factorial value.

What I have tried:

unsigned int factorial(unsigned int n)
    int res = 1, i;
    for (i = 2; i <= n; i++)
        res *= i;
    return res;
int main()
    int num = 5;
        "Factorial of %d is %d", num, factorial(num));
Updated 3-Jan-23 14:22pm
CPallini 3-Jan-23 9:46am    
Are you sure about the requirements?
You know, 1000000! goes like 10^5565706
Rick York 3-Jan-23 22:07pm    
There are several optimizations you can do with this. Here is one hint : you do not have to actually compute the factorial since the result you want is the sum of the digits, not the actual factorial value. Think about how factors of ten work - 10, 20, 30, etc...

Please give me the correct code

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
Share this answer
Please give me the correct code

This problem is typical from the challenges sites, this means that you failed to solve the problem.
These kind of problems are meant to be complicated, and not for beginners.
By the way, with these problems, simple minded solutions (brute force) are never the solution. You need to study the problem until you fully understand the inner working.

By the way, your code do not try to solve the problem. Read carefully the problem and try again.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900