/*factirial*/
 
#include<stdio.h>
#include<conio.h>
void main()
{
int n,s=1,i;
clrscr();
printf("enter n ");
for(i=1;i<n;i++)
{
scanf("%d",&n);
}
for(i=1;i<n;i++)
{
s=s*i;
}
printf("factorial is %d",s);
getch();
}
/*factirial*/
 
#include<stdio.h>
#include<conio.h>
void main()
{
int n,s=1,i; /* n = unknown garbage, s = 1, i = unknown garbage */
clrscr();
printf("enter n ");
for(i=1;i<n;i++) /* Initially loop as long as i is less than n. what was in n?. Garbage! anything can happens */
{
scanf("%d",&n); /* user set n to some integer, let's say as an example integer 4 */ 
}
for(i=1;i<n;i++)  /* Initially i= 1, n = 4; 1 < 4? Yes, execute block */ /* Will do 3 times */
{
s=s*i; /*first time s = 1 * 1 = 1 */ /* second time s = 1 * 2 = 2 */ /* third time s = 2 * 3 = 6 */
}
printf("factorial is %d",s); /* factorial is 6 */ /* This line has to be inside the loop to display each value */
getch();
}

Aia, what did you just post? No explanation, no formatting, improper programming practices ( void main() , getch() , clrscr() )
You should model good practices and proper techniques. You seem to have been here long enough to know this... ;)

Aia, what did you just post? No explanation, no formatting, improper programming practices (void main(), getch(), clrscr())
You should model good practices and proper techniques. You seem to have been here long enough to know this... ;)

@WaltP

I just tried to expose the logic of his code using some comments. Just in case he/she needed a little bit more explanation.

Infarction already mentioned about the "void main" declaration.

By the way, main should return a type int

and concerning about the getch(), clrscr() Well, knowing that they are not portable is good, however I'm not the most knowledgable guy
to preach. I don't feel the most qualified to correct those things, even when reading many of your comments has made me not to use them.

By the way, I'm suprise you haven't say anything about

#include<conio.h>.

This is a "no, no" if you want your code to be portable. :confused:

and concerning about the getch(), clrscr() Well, knowing that they are not portable is good, however I'm not the most knowledgable guy
to preach. I don't feel the most qualified to correct those things, even when reading many of your comments has made me not to use them.

By the way, I'm suprise you haven't say anything about
This is a "no, no" if you want your code to be portable. :confused:

Well, the OP needs to change the line getch() to getchar() first. That's why conio.h was included in the first place, and also for clrscr(). They were both used a lot in Borland, although there isn't a simple replacement for clrscr() like there is for pausing the program when execution is completed.

If you really have to clear the screen, consider reading this:
http://www.cprogramming.com/faq/cgi-bin/smartfaq.cgi?answer=1031963460&id=1043284385

Regarding the original topic:

Basically you need to take out the first loop that scanf() is encased in. This will initalize n properly (although you should still do error-checking in the case that an integer isn't entered).

Here is a little different version of your code:

/*
 * factorial number.c
 * for testing purposes only.
 */
#include <stdio.h>

int main(void)
{
    char string[20]; 
    int number;    
    int fact = 1;
    int i;

    fputs("Please enter a number: ", stdout); 
    fflush(stdout);

    /* read the input as a string */
    if(fgets(string, sizeof(string), stdin))  
    {

     /* check that there is a number in the inputed string */
        if(sscanf(string, "%d", &number) == 1)
        {

           /* the loop that will display the result */
            printf("Factorial is ");
            for(i = 1; i < number; i++)
            {
                fact = fact * i;
                printf("%d ", fact);
            }
            putchar('\n'); 
        }
    }
    getchar();
    return(0);
}

sir i am still amaeture could u please helpme in this prg.

I know that feeling, however may I suggest that you read this blog?

Also, I took the liberty of finding these other helpful links for tagging and formatting C/C++.
This one is really great is you want to learn what's wrong with using
some popular functions like scanf() and gets(). Will you read them?.

Just a small minor correction.
Instead of:

for(i = 1; i < number; i++)

It should be :

for(i = 1; i <= number; i++)

Sorry about that.

can u send me the correct prog...
i want to see how it really works

No. Read this

sir i am still amaeture could u please helpme in this prg.

Read this too

this is the first time ..
can u telll me what is my mistake

if i write
for(i=1;i<=4;i++)

may be then it will work.
what do you say

Depends on what you want the program to do...

We are still waiting for an explanation of your problem. Rather than beg for help, explain what your program is doing wrong, and what it should do correctly.

Maybe they're compiling it with some 16-bit fossil compiler, then wondering why the program produces weird results past say 7! or 8!

actually sir i don't hv c in my pc so m not able to check.yell me whether this one is right or not.

#include<stdio.h>
void main()
{
int i,s=1,n;
printf("enter a numner ");
scanf("%d",&n);
for(i=n;i>1;i--)
{
s=s*i;
}
printf("%d",s);
}


i hv some more queries regarding c++.
will u help me by highlighting my errors,when i'll send u the prgs???

actually sir i don't hv c in my pc so m not able to check.yell me whether this one is right or not.

i hv some more queries regarding c++.
will u help me by highlighting my errors,when i'll send u the prgs???

Probably not, since you have completely ignored every posting rule and suggestion we've pointed at and still
1) won't use code tags
2) won't format your code
3) won't tell us what the problem is, leaving us to figure it out
4) asking us to finish your program for you
5) continually using 'leet speak'

> actually sir i don't hv c in my pc so m not able to check.yell me whether this one is right or not.
So get one, and stop using the boards to answer your "does this compile" queries.

With a compiler, you could answer your own question in a few minutes, compared to the days that asking such questions on a forum usually takes (not to mention the nagging).

This question has already been answered. Start a new discussion instead.