4 Replies - 477 Views - Last Post: 18 November 2012 - 09:36 PM Rate Topic: -----

#1 qpMONKEYMIKEqp  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 25
  • Joined: 16-October 12

Factorial Program in C

Posted 18 November 2012 - 04:20 PM

Hello DreamInCode!

I have been working though JAVA and C programming the past few months and i found this example that looks like a challenge. I have been posting these while i am doing them because allot of you have great input on my programming. Its one thing to do it but another to have others comment on it for improvements and answer pressing issues. People on here respond allot faster than people that i have on my email list.

Thank you for looking at this post and thank you very much for all help Past, Present, and Future!

__________________
Have the user enter a list of numbers and place them into an array (since we have not done dynamic arrays yet, create a static array that can hold at least 25 items and limit the user to that many items).

You will need to write two methods, one to find the factorial of a number and the other to sum the factorials of the array. These methods will invoke the following signatures:

int factorial(int number);

int sumFactorial(int list[], int numberElements);

For each item in the array, you will need to display the number and itís factorial. At the end, display the sum of the factorials.

Example run:

Enter Number Elements: 5

Element 1: 2

Element 2: 3

Element 3: 1

Element 4: 6

Element 5: 7

f(2) = 2

f(3) = 6

f(1) = 1

f(6) = 720

f(7) = 5040

sum of factorials = 5769

Validation: Ensure the user enters a value for the number of elements less than or equal to you array size and accept only positive numbers for the values to be placed in the array. If an invalid entry is made, prompt the user for another entry (donít exit the program).

Is This A Good Question/Topic? 0
  • +

Replies To: Factorial Program in C

#2 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 614
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Factorial Program in C

Posted 18 November 2012 - 05:05 PM

Could you post your code and specific problems you have?
Was This Post Helpful? 0
  • +
  • -

#3 qpMONKEYMIKEqp  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 25
  • Joined: 16-October 12

Re: Factorial Program in C

Posted 18 November 2012 - 08:55 PM

I posted this right as i was coding.

I surprisingly ran though this without issues that i couldn't solve with time and persistance :w00t: . I believe that it is complete. Does this code look like it meets the criteria of the exercise?

#include <stdio.h>
#include <math.h>

int factorial(int number)
{
    int product = 0; // store product
    int i; //increment
    
    if (number == 1) //if number = 1 product is 1
    {
        product = 1; 
    }
    else if (number == 0) //if number = 0 product is 0
    {
        product = 0;
    }
    else // else run the equation
    {
        for (i = number; i > 1; i--) //for loop to run though factorial equation
        {
            if (product == 0) //if its the first run though
            {
                product = (i * (i - 1)); // multiply the number by one less than itself
            }
            else // if its ran though once before
            {
                product = (product * (i - 1)); // multiply the product by one less than the increment
            }//end product ifs
        }//end factorial for loop
    }//end of number ifs
    
    return product;
}//end factorial

int sumFactorial(int listAnswers[], int numberElements)
{
    int sumFact = 0; // store sum
    int i; // increment
    
    for (i = 0; i < numberElements; i++) // for loop to run though all factorials
    {
        if (sumFact == 0) // if it has not ran before
        {
            sumFact = (listAnswers[i] + listAnswers[i+1]); // add first to next
        }
        else //if it has ran once before
        {
            sumFact = (sumFact + listAnswers[i+1]); // add sum to next
        }// end sumFact ifs
    }// end sum for loop
    
    return sumFact;
}//end sumFactorial

int main()
{
    int list[26]; //initialize array for numbers entered
    int listAnswers[26]; //initialize array for factorials
    int listEntry; //create listEntry
    int numberElements = 0; //create user input for number of elements
    char restart = 'y'; //create variable for while loop restart
    int loopCount; //create variable to count number of loops
    int i; //increment variable forLoop
    int elementNum; //current element
    int number; //number for factorial
    int answer; //store current factorial
    int sumFact; //store sum factorial
    
    printf("Welcome to the factorial program!\n\n"); //Welcome text
    
    while (restart == 'y') //while loop for users convenience to restart program without stoping
    {
        loopCount = 0; //make loopCount 0 before every while
        while (numberElements < 1 || numberElements > 25)
        {
            if (loopCount == 0) //if loop has never been ran
            {
                printf("Enter number of elements (between 1 and 25):\n"); //Ask user for input for numberElements
                scanf("%d", &numberElements); //receive user input for numberElements
                ++loopCount; //increase loop counter
            }
            else //if loop has been ran atleast once
            {
                if (numberElements < 1) // if numberElements is les than one
                {
                    printf("Try again your number was less than 1:\n"); //Ask user for input for numberElements
                    scanf("%d", &numberElements); //receive user input for numberElements
                }
                else // if numberElements is greater than 25
                {
                    printf("Try again your number was greater than 25:\n"); //Ask user for input for numberElements
                    scanf("%d", &numberElements); //receive user input for numberElements
                } //end numberElements ifs
            } //end count ifs
        } //end while numberElements
        
        for (i = 0; (i < numberElements); i++) // for loop to enter values into array
        {
            listEntry = -1; // make list Entry negative before every loop
            loopCount = 0; // make loopCount 0 before every while
            elementNum = i + 1; //used to display elements 1,2,3 not 0,1,2
            while (listEntry <0) //while loop to check for errors in listEntry
            {
                if (loopCount == 0) //if never looped
                {
                    printf("Element %d:\n", elementNum); //Ask user for input for listEntry
                    scanf("%d", &listEntry); //receive user input for listEntry
                    ++loopCount; //increase loop counter
                }
                else //if looped atleast once
                {
                    printf("Your number is negative please enter another number:\n"); //Ask user for input for listEntry
                    scanf("%d", &listEntry); //receive user input for listEntry
                } //end loopCount ifs
            } //end listEntry while loop
            
            list[i] = listEntry; //put valid listEntry into array list
        } //end value entry for loop
    
        printf("\n"); // break up parts in output
        for (i = 0; (i < numberElements); i++) // for loop to print factorial answer
        {
            number = list[i]; // make number = element
            answer = factorial(number); //store factorial
            printf("f(%d) = %d\n",list[i],answer); //print factorial answer
            listAnswers[i] = answer; //store factorial in array
        } // end for loop
        
        printf("\n"); // break up parts in output
        sumFact = sumFactorial(listAnswers, numberElements); // store sum
        printf("sum of factorials = %d\n",sumFact); //print sum
        
        printf("\n"); //break up parts in output
        printf("Would you like to restart the program? Yes(y), or No(anyChar)\n\n"); //Ask user for input for restart
        scanf("%s", &restart); //receiving user input for restart
    } //end while restart
    
    return 0;
} //end main


Was This Post Helpful? 0
  • +
  • -

#4 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Factorial Program in C

Posted 18 November 2012 - 09:10 PM

I have a question for you.

Have you learned recursion?

Coz if you have, then doing a factorial program with recursion is probably the simplest of things ever :)

If you want to know what recursion is, I would suggest you go through This link(only the recursion part) to see how it works. :)

regards,
Raghav

This post has been edited by raghav.naganathan: 18 November 2012 - 09:11 PM

Was This Post Helpful? 0
  • +
  • -

#5 qpMONKEYMIKEqp  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 25
  • Joined: 16-October 12

Re: Factorial Program in C

Posted 18 November 2012 - 09:36 PM

View Postraghav.naganathan, on 18 November 2012 - 09:10 PM, said:

I have a question for you.

Have you learned recursion?

Coz if you have, then doing a factorial program with recursion is probably the simplest of things ever :)/>

If you want to know what recursion is, I would suggest you go through This link(only the recursion part) to see how it works. :)/>

regards,
Raghav


I wouldn't say learned, but i have dabbled in it and have used it before. Thank you very much for the input!!!! i have added the implementation of recursion to my code. Looking back that is probably what they wanted me to do in the first place.

here is my changes

//
//  main.c
//  MichaelRauchExam2
//

#include <stdio.h>
#include <math.h>

int factorial(int number)
{
    if (number == 0) // if 0 then answer is 1
        return 1;
    else //enter the recursion statement
    return (number * factorial(number - 1)); 
}//end factorial

int sumFactorial(int listAnswers[], int numberElements)
{
    int sumFact = 0; // store sum
    int i; // increment
    
    for (i = 0; i < numberElements; i++) // for loop to run though all factorials
    {
        if (sumFact == 0) // if it has not ran before
        {
            sumFact = (listAnswers[i] + listAnswers[i+1]); // add first to next
        }
        else //if it has ran once before
        {
            sumFact = (sumFact + listAnswers[i+1]); // add sum to next
        }// end sumFact ifs
    }// end sum for loop
    
    return sumFact;
}//end sumFactorial

int main()
{
    int list[26]; //initialize array for numbers entered
    int listAnswers[26]; //initialize array for factorials
    int listEntry; //create listEntry
    int numberElements = 0; //create user input for number of elements
    char restart = 'y'; //create variable for while loop restart
    int loopCount; //create variable to count number of loops
    int i; //increment variable forLoop
    int elementNum; //current element
    int number; //number for factorial
    int answer; //store current factorial
    int sumFact; //store sum factorial
    
    printf("Welcome to the factorial program!\n\n"); //Welcome text
    
    while (restart == 'y') //while loop for users convenience to restart program without stoping
    {
        loopCount = 0; //make loopCount 0 before every while
        while (numberElements < 1 || numberElements > 25)
        {
            if (loopCount == 0) //if loop has never been ran
            {
                printf("Enter number of elements (between 1 and 25):\n"); //Ask user for input for numberElements
                scanf("%d", &numberElements); //receive user input for numberElements
                ++loopCount; //increase loop counter
            }
            else //if loop has been ran atleast once
            {
                if (numberElements < 1) // if numberElements is les than one
                {
                    printf("Try again your number was less than 1:\n"); //Ask user for input for numberElements
                    scanf("%d", &numberElements); //receive user input for numberElements
                }
                else // if numberElements is greater than 25
                {
                    printf("Try again your number was greater than 25:\n"); //Ask user for input for numberElements
                    scanf("%d", &numberElements); //receive user input for numberElements
                } //end numberElements ifs
            } //end count ifs
        } //end while numberElements
        
        for (i = 0; (i < numberElements); i++) // for loop to enter values into array
        {
            listEntry = -1; // make list Entry negative before every loop
            loopCount = 0; // make loopCount 0 before every while
            elementNum = i + 1; //used to display elements 1,2,3 not 0,1,2
            while (listEntry <0) //while loop to check for errors in listEntry
            {
                if (loopCount == 0) //if never looped
                {
                    printf("Element %d:\n", elementNum); //Ask user for input for listEntry
                    scanf("%d", &listEntry); //receive user input for listEntry
                    ++loopCount; //increase loop counter
                }
                else //if looped atleast once
                {
                    printf("Your number is negative please enter another number:\n"); //Ask user for input for listEntry
                    scanf("%d", &listEntry); //receive user input for listEntry
                } //end loopCount ifs
            } //end listEntry while loop
            
            list[i] = listEntry; //put valid listEntry into array list
        } //end value entry for loop
    
        printf("\n"); // break up parts in output
        for (i = 0; (i < numberElements); i++) // for loop to print factorial answer
        {
            number = list[i]; // make number = element
            answer = factorial(number); //store factorial
            printf("f(%d) = %d\n",list[i],answer); //print factorial answer
            listAnswers[i] = answer; //store factorial in array
        } // end for loop
        
        printf("\n"); // break up parts in output
        sumFact = sumFactorial(listAnswers, numberElements); // store sum
        printf("sum of factorials = %d\n",sumFact); //print sum
        
        printf("\n"); //break up parts in output
        printf("Would you like to restart the program? Yes(y), or No(anyChar)\n\n"); //Ask user for input for restart
        scanf("%s", &restart); //receiving user input for restart
    } //end while restart
    
    return 0;
} //end main


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1