Factorial calc program not running!

This is a discussion on Factorial calc program not running! within the C Programming forums, part of the General Programming Boards category; Hello, I am very new to C, I was making an experiment after reading the C tutorial on the site ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    8

    Factorial calc program not running!

    Hello, I am very new to C, I was making an experiment after reading the C tutorial on the site up until the "Loops" section. I've made a program which I can compile but which doesn't return any result. The code is as follows:

    Code:
    #include <stdio.h>
    
    
    int main(){
        int num1;
        int fact = 1;
        int x;
        printf ("Insert the number whose factorial you want to know: \n");
        scanf ("%d", &num1);
        for ( x=1; x <= num1; x++ ){
            fact = fact*x;
            scanf("%d", &fact);
        }
        printf ("The factorial of %d is %d \n", num1, fact );
        return(0);
    }
    I'd appreciate if someone could have a look at it and possibly tell me what I'm doing wrong?

    Thank you very much in advance!
    Last edited by tfigueiro; 10-24-2011 at 06:30 PM. Reason: text translation pt-en

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
        for ( x=1; x <= num1; x++ ){
            fact = fact*x;
            scanf("%d", &fact);
        }
    Why are you scanning for fact in that loop? scanf means "ask the user for specifically formatted input".


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Why are you scanf'ing fact inside the loop?
    Code:
    while(!asleep) {
       sheep++;
    }

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    8
    Thank you very much for looking into my question! I thought scanf meant something like "update value of". Now my program does work properly and more important, I won't forget what exactly scanf does!

    Tiago Figueiró

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. factorial program
    By emblem4ever in forum C Programming
    Replies: 4
    Last Post: 03-29-2010, 04:44 AM
  2. Help: Program to calc space and money for apartments
    By MightyM991 in forum C Programming
    Replies: 4
    Last Post: 09-22-2005, 09:11 AM
  3. Tax Calc program
    By EvBladeRunnervE in forum C++ Programming
    Replies: 3
    Last Post: 02-13-2003, 02:02 PM
  4. My calc program?
    By Munkey01 in forum C++ Programming
    Replies: 14
    Last Post: 12-29-2002, 10:26 AM
  5. Calc Program I made Tell me what u think
    By smog890 in forum C Programming
    Replies: 12
    Last Post: 06-09-2002, 10:56 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21