Beginner

This is a discussion on Beginner within the C Programming forums, part of the General Programming Boards category; Well guys Im basicly new to C so take it ez with me >_<.. I got a project in which ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    4

    Unhappy Beginner

    Well guys Im basicly new to C so take it ez with me >_<..

    I got a project in which I had to enter a number and the computer should output if it has a factorial or not.

    I dont know why but the problem I have is that when I input a number which has a factorial it says No Factorial,No FactorialNo FactorialNo Factorial then the Answer Correctly.. I wish someone could help me..


    I believe the error in here..
    Code:
    i = 2;
    facttemp = 1;
    while (facttemp<=pfact){
       facttemp= facttemp*i;
       i++;
    if (facttemp==pfact){
       x = i - 1 ;                    
       printf("\n\n\tEl numero tiene factorial, el factorial es %d", x);
       return 0;}
    else 
    printf("No tiene factorial");
    getch();}
    return 0;
    }
    Last edited by gyferlotus; 10-27-2006 at 03:41 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Please add some newlines to that. It's horrible to read.


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

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Every statement inside while loop is exeuted on every iteration, also the if/else

    Till you rich the actual number - if condition is false - and you execute the else block.
    So you get your "No factorial" output on every iteration...

    Move it outside the loop
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    4
    yeah I know that but how will the program know that when to post the
    "No factorial" message.

    The question I have is because if I move it out of the loop,
    it will always print it once

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Are you sure? What happens in your program when the factorial condition is matched?
    Look closely at the if block of the if/else statement.
    Pay attention to return
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    269
    write a function returining int. if the number passed to that function has a factorial then it will return 1 otherwise will return 0. In your main(), call this function and pass the number you want as parameter, then use an if statement to display appropriate message.

    Here is a code skeleton
    Code:
    int has_factoaril(int)
    {
     /* chechk here if it has a factorial */
     /* if it has return 1*/
     /* if it doesn't return 0 */
    }
    
    int main()
    {
    ...
    ...
     if (has_factorial(any_number))
      printf("yes it has\n");
     else
      printf("no, it does not\n");
    ...
    ...
     return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Same old beginner question...
    By Sharmz in forum C Programming
    Replies: 15
    Last Post: 08-04-2008, 11:48 AM
  2. What are some good beginner programs I shouold make?
    By oobootsy1 in forum C# Programming
    Replies: 6
    Last Post: 08-09-2005, 02:02 PM
  3. Books, Beginner, MustHave
    By Zeusbwr in forum C++ Programming
    Replies: 9
    Last Post: 10-25-2004, 05:14 PM
  4. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 10:21 AM

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