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

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) Program Question 2: Write a program that solves for c in the Pythagorean Theorem:...
(IN C) Program Question 2: Write a program that solves for c in the Pythagorean Theorem: a2 + b2 = c2 The user will enter values for a and b, and you will calculate c. All of this code will go in just one source file.
Write a program in C that declares the following array: int. array[] = { 1, 2,...
Write a program in C that declares the following array: int. array[] = { 1, 2, 4, 8, 16, 32 } Then write some code that accepts a number between 0 and 5 from the user and stores it in a variable called "index". Write some code that retrieves the item specified by the index, like this: int item = array[index]; Then write code that outputs the corresponding array entry based on the number the user entered. Example output: The...
Write a C program/code that prompts the user for a minimum min and a maximum max....
Write a C program/code that prompts the user for a minimum min and a maximum max. Then use these values to print the squares of all even numbers between the min and max variables. For example if the user enters 6 as the minimum and 200 as the maximum, the program/code should print the following. Enter limit on minimum square: 6 Enter limit on maximum square: 200 36 64 100 144 196
Write a C or C++ program A6p2.c(pp) that accepts one command line argument which is an integer n between 2 and 4 inclusi...
Write a C or C++ program A6p2.c(pp) that accepts one command line argument which is an integer n between 2 and 4 inclusive. Generate 60 random integers between 1 and 49 inclusive and store them in a 5 by 12 two dimensional integer array (e.g.,int a[5][12];). Use pthread to create n threads to square all 60 array elements. You should divide this update task among the n threads as evenly as possible. Print the array both before and after the...
write a program to make scientific calculator in java
Problem StatementWrite a program that uses the java Math library and implement the functionality of a scientific calculator. Your program should have the following components:1. A main menu listing all the functionality of the calculator.2. Your program should use the switch structure to switch between various options of the calculator. Your Program should also have the provision to handle invalidoption selection by the user.3. Your calculator SHOULD HAVE the following functionalities. You can add any other additional features. PLEASE MAKE...
Write a program that reads an integer, a list of words, and a character.
13.14 LAB: Contains the characterWrite a program that reads an integer, a list of words, and a character. The integer signifies how many words are in the list. The output of the program is every word in the list that contains the character at least once. Assume at least one word in the list will contain the given character.Ex: If the input is:4 hello zoo sleep drizzle zthen the output is:zoo drizzleIn c++ 
Write a program/code that prompts the user for a minimum min and a maximum max. Then...
Write a program/code that prompts the user for a minimum min and a maximum max. Then use these values to print the squares of all even numbers between the min and max variables. (WRITTEN IN C) For example if the user enters 6 as the minimum and 200 as the maximum, the program/code should print the following. Enter limit on minimum square: 6 Enter limit on maximum square: 200 36 64 100 144 196
​Write a recursive method, vowels, that returns the number of vowels in a string. Also, write a program to test your method.
Write a recursive method, vowels, that returns the number of vowels in a string. Also, write a program to test your method.(JAVA Code)
Use the switch structure to write a MATLAB program to compute the amount of money that accumulates
Use the switch structure to write a MATLAB program to compute the amount of money that accumulates in a savings account in one year. The program should accept the following input: the initial amount of money deposited in the account; the frequency of interest compounding (monthly, quarterly, semiannually, or annually); and the interest rate. Run your program for a $1000 initial deposit for each case; use a 5 percent interest rate. Compare the amounts of money that accumulate for each...