Results 1 to 6 of 6

Thread: n factorial program in C

  1. #1
    Join Date
    Sep 2010
    Posts
    11

    n factorial program in C

    I've writtin a program in C to calculate the n!(n factorial) but I am obviously doing somthing wrong because I keep getting errors and I think it has something to do with the z=n; and if statements. Please help...


    #include<stdio.h>
    main()
    {

    int n; /*n is the number entered by the user*/
    int i; /*i is the counter*/

    scanf("%d",&n);

    printf("Enter a positive integer:\n\n");

    z=n; /*z is the result after the first loop is ran*/

    for(i=n; i>0; i--)
    {
    if(n=0)
    z=1;

    z=z*i;
    }

    printf("The factorial of %d is:%d",n,z);

    }

  2. #2
    Join Date
    Nov 2009
    Posts
    518

    Re: n factorial program in C

    Calculate factorial by using recursion
    Code:
    #include
    #include
    
    int ft(int x)
    {
    if(x==0)
    return 1;
    else
    return x*ft(x-1);
    }
    
    void main()
    {
    int n;
    clrscr();
    printf("\n Enter a number :");
    scanf("%d",&n);
    printf("\n Factorial value=%d",ft(n));
    getch();
    }

  3. #3
    Join Date
    Jan 2010
    Posts
    119

    Re: n factorial program in C

    Here is an simple program that will help you

    Code:
    #include <stdio.h> 
    #define VALUE 6 
    int y,x; 
    void main()  
    {    
        x=1;    
        for (y=1; y<=VALUE; y++)      
            x=x*y;    
        printf("The factorial of %d is %d\n",VALUE,x);  
    }

  4. #4
    Join Date
    Sep 2010
    Posts
    11

    Re: n factorial program in C

    Is there a way to fix the first one? I need to use a for loop to complete the problem. The if/else is for the condition zero (0).

  5. #5
    Join Date
    Jan 2010
    Posts
    119

    Re: n factorial program in C

    If you are looking for the logic of for loop then here it is

    Code:
    for (y=1; y<=VALUE; y++)      
            x=x*y;
    just implement the same in the language you are using.

  6. #6
    Join Date
    Sep 2010
    Posts
    11

    Re: n factorial program in C

    Thanks, that helped. I figured it out

Similar Threads

  1. How to create factorial of numbers using sub procedure in vbscript
    By Gurseerat in forum Software Development
    Replies: 1
    Last Post: 04-02-2012, 12:21 PM
  2. c# program for factorial
    By roodiii in forum Software Development
    Replies: 5
    Last Post: 19-12-2009, 02:42 PM
  3. How to write java program to find factorial of number?
    By Balamohan in forum Software Development
    Replies: 5
    Last Post: 28-11-2009, 10:14 PM
  4. What is the Program to find-out factorial of given number?
    By Sheenas in forum Software Development
    Replies: 5
    Last Post: 27-11-2009, 01:42 PM
  5. How to calculate factorial of a number in C++
    By AZUL in forum Software Development
    Replies: 2
    Last Post: 26-08-2009, 06:08 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,715,191,592.68538 seconds with 17 queries