Question

In: Computer Science

Write down the C++ Program To Find Factorial.

Write a function, which accepts an integer value as an argument, finds the factorial of that integer value, and then returns the factorial value to the main program. Write a main program that will call the function by passing an integer value and print the factorial value returned by the function. 

Solutions

Expert Solution

1. First we create a function called findFactorial .

void findFactorial(int number) { }

2. Now we implement logic in out findFactorial function.

 


void findFactorial(int number)
{
int factorial = 1;
if (number == 0)
{
cout << "Please enter some number";
}
else
{
for (int i = 1; i <= number; i++)
{
factorial = factorial * i;
}
}
cout << "Factorial of " << number << " is: " << factorial;
}
2.1: The main logic is to create a variable named factorial and give a value 1 .
2.2: Now using the if statement we check if the user input value is zero or not
2.3: After that, we show a simple message and use for loop for the main logic
2.4: in for loop we declare variable i and give it value 1 and we say that i is less than user given number and increment the i .
2.5: then we multiply this by factorial variable with i and store in factorial variable and return the variable factorial

 

3. Now we call in the main function.
int main()
{
int inputInteger;
cout << "Enter an Integer: ";
cin >> inputInteger;
findFactorial(inputInteger);
getch();
}

 

 

 

And the whole program looks like this:

 

#include
#include
using namespace std;


void findFactorial(int);
int main()
{
int inputInteger;
cout << "Enter an Integer: ";
cin >> inputInteger;
findFactorial(inputInteger);
getch();
}
void findFactorial(int number)
{
int factorial = 1;
if (number == 0)
{
cout << "Please enter some number";
}
else
{
for (int i = 1; i <= number; i++)
{
factorial = factorial * i;
}
}
cout << "Factorial of " << number << " is: " << factorial;
}

 


#include <iostream>

#include <conio.h>

using namespace std;

void findFactorial(int);

int main()

{

    int inputInteger;

    cout << "Enter an Integer: ";

    cin >> inputInteger;

    findFactorial(inputInteger);

    getch();

}

void findFactorial(int number)

{

    int factorial = 1;

    if (number == 0)

    {

        cout << "Please enter some number";

    }

    else

    {

        for (int i = 1; i <= number; i++)

        {

            factorial = factorial * i;

        }

    }

    cout << "Factorial of " << number << " is: " << factorial;

}

Related Solutions

1. Write a program in C++ to find the factorial of a number. Prompt the user...
1. Write a program in C++ to find the factorial of a number. Prompt the user for a number and compute the factorial by using the following expression. Use for loops to write your solution code. Factorial of n = n! = 1×2×3×...×n; where n is the user input. Sample Output: Find the factorial of a number: ------------------------------------ Input a number to find the factorial: 5 The factorial of the given number is: 120 2. Code problem 1 using While...
Write a factorial C++ program where n=10,000,000
Write a factorial C++ program where n=10,000,000
Write a factorial C++ program where n=10,000,000
Write a factorial C++ program where n=10,000,000
Write down the C++ Program
 Write a function, which accept three integer values as arguments find the largest of three and then return the largest value to main program. Write a main program which will call the function by passing three integer values and print the value returned by the function.?
In C++, write a program that uses array to calculate the factorial of a reasonable large...
In C++, write a program that uses array to calculate the factorial of a reasonable large number (say, up to 2,000).
In C++. Write a program that uses array to calculate the factorial of a reasonable large...
In C++. Write a program that uses array to calculate the factorial of a reasonable large number (say, up to 2,000).  
JAVA PROGRAM 1. Write a program to find the factorial value of any non-negative number entered...
JAVA PROGRAM 1. Write a program to find the factorial value of any non-negative number entered through the keyboard.(method) (Factorial of n: n! = 1*2*3*…*n, 0! = 1.) 2. Write a program that prompts the user to input a positive integer. It should then output a message indicating whether the number is a prime number. (method) Please post a screenshot of the codes. Thanks!
Write a c program to calculate the factorial of a number using recursion    [8] Question five...
Write a c program to calculate the factorial of a number using recursion    [8] Question five Write a stack algorithm to POP an item                                                         [6] What does FRONT and REAR signify in a queue?                                                                 [6] Write an algorithm for a dequeue operation                                                                       [8]
write pseudocode not c program If- else programming exercises 1.    Write a C program to find...
write pseudocode not c program If- else programming exercises 1.    Write a C program to find maximum between two numbers. 2.    Write a C program to find maximum between three numbers. 3.    Write a C program to check whether a number is negative, positive or zero. 4.    Write a C program to check whether a number is divisible by 5 and 11 or not. 5.    Write a C program to check whether a number is even or odd. 6.    Write...
write C++ program using functions (separate function for each bottom) Write a program to find if...
write C++ program using functions (separate function for each bottom) Write a program to find if a number is large word for two given bottom base - bottom1 and bottom2. You can predict that a number, when converted to any given base shall not exceed 10 digits. . the program should ask from user to enter a number that it should ask to enter the base ranging from 2 to 16 after that it should check if the number is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT