What is wrong with my code for calculating factorials?

Here’s the code :

#include <stdio.h>
int main (void)
    int x, y, z;
    z = 1;
    scanf ("%i", &x);
    for (x; x > 0; --x)
 	    scanf ("%i", &y);
	    for (y = 1; y; ++y)
	 	    z = z * y;
	    printf ("%i\n", &z);

    return 0;

Please don’t give me the solution. I am new to programming and I haven’t learn’t anything except loops and if else statements.

@gautam94: I think you want to find the factorial for the given x numbers.

  1. in y loop for(;y>1;y–) z=z*y;

  2. z=1 each time in the x loop

  3. print just ‘z’ , & is used to get the address of an operator.

welcome to programming :slight_smile:

1 Like

http://ideone.com/TtLpi6 Weird output.

This was the output when I ran it on my PC(GCC 4.6.2)


@gautam94: Correction in the above code whose ideone link you have posted:

Well, you’re almost there. You don’t have to use “&” operator while printing. The “weird” output is basically printing the address reference of that particular variable.
In short, use:

<pre>  printf ("%i\n", z); 

If you use:

<pre>  printf ("%i\n", &z); 

then it will print the address of z. and yeah your code produces correct output after this midification :slight_smile:


@sunny_patel: Why you want to close the question?


@betlista i just realized you commented after i deleted mine. Sorry about that.
Well, i saw the link. Sorry sir, i in no way meant that question is bad. But i thought-that while scrolling the discuss homepage say someone feels like answering some question. If it is closed he knows that the question is answered or invalid, & for a person who himself is looking for answers then he will also know which question to see(as there are multiple searches for a single keyword).
So, basically i just felt that it would keep the whole and forum process cleaner :slight_smile: Not to insult anyone in anyway:)

Thanks. That was a typo.

@sunny_patel: Thanks for taking care for forum. Questions with accepted answer are marker green in list (similarly as questions without answer are red). Probably if you want, you can instead of closing questions, find questions that are answered but author didn’t accept any answer and choose the best one and accept it (if you have privileges already). But, I’m not doing this, because I think that users have to learn how to use this forum :wink: