0

here is the question: Write a program that accepts a positive integer. Use a function to calculate the factorial of
that number.
my codes:

#include <stdio.h>
#include <stdlib.h>
int fun(int z);
int main()
{
    int n,x,fact;

    printf("enter  a number: ");
    scanf("%d",&n);
    fact=fun(n);
    printf("factorial of %d is %ld\n" ,n,fact);

    system("pause");
    return(0);
}
    int fun(int z)
{
    int n,c,  fact=1;
    for(c=1;c<=n;c++)

    return fact*c;
}

i am still having the difficulty when i enter 2 i should have get 2 but instead i get a 1. what is wrong?

4
Contributors
5
Replies
33
Views
3 Years
Discussion Span
Last Post by Assembly Guy
0

Line 19. What is the value of n?

Also, your function fun just plain doesn't work. That loop doesn't loop. The first time you get to line 21, the function finishes.

Edited by Moschops

0
#include <stdio.h>
#include <stdlib.h>
int fun(int z);
int main()
{
    int n,x,fact;

    printf("enter  a number: ");
    scanf("%d",&n);
    fact=fun(n);
    printf("factorial of %d is %ld\n" ,n,fact);

    system("pause");
    return(0);
}
    int fun(int z)
{
    int n,c,  fact=1;
    for(c=1;c<=z;c++)
    {
        fact=fact*c;
}
    return (fact);
}

now it works ;)

0

What's that n for in line 18?

yes,
its good practice that if there is no use of any variable remove these.(i think they consume memory).

so , if n is not useful then don't declare it.

2

so , if n is not useful then don't declare it.

This is why I believe -Wall (and other compilers' equivalents) shouldn't be optional.

Votes + Comments
Indeed!
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.