An Exercise for all C++ students

All class XII or below students having basic knowledge of C/C++ can try out their luck at this one!

You have to write one (exactly one) line code/statement for finding the factorial of a number.
Remember you have to use only one line code. All declaration should be done in that particular statement. You have to take the number from the user and print the result back on the screen.

Code should be like

void main()
//”one line executable code”

Factorial of a number is the product of all natural numbers till that number.
E.g. 4!=1*2*3*4

I don’t know the syntax of VB, maybe it can be done in that too.

try it……

6 thoughts on “An Exercise for all C++ students

  1. it seems unfortunately that you cant see the answer in comments cuz when you type the lesser than sign blogger thinks youre trying to use HTML….
    so treat the ~ sign as a lesser than….
    and the answer is:

    for(float i=1,j=1,k=(getche()-48);i~=k;i++,j*=i) if(i==k) cout~~”\n”~~j;

  2. Rahul, your solution is correct but its too typical.
    I would like a solution which is something like

    and thats it.

  3. oops read the question wrong >_< . Anyways here goes: for (int k,n = getche()-48,answer = n;(n>1)&&(k=(n==2)?printf(“%d”,answer):1);answer*=(–n));

    Came up with it in a few minutes, I bet there are better ways though.

Leave a Reply

Your email address will not be published. Required fields are marked *