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++, 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).  
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...
Part1: Write a program in C/C++ to find the maximum flow in the given Flow Network...
Part1: Write a program in C/C++ to find the maximum flow in the given Flow Network using (i)Ford -Fulkerson algorithm and (ii) Edmond-Karps algorithm Go through the related text and implement each of these algorithms using the efficient data structure. Show the results of different steps of these algorithms for an instance of the flow network with total number of nodesV=6 (please note down that in a flow network there are two special nodes source and sink) and total number...
Write a C++ program to find the number of pairs of integers in a given array...
Write a C++ program to find the number of pairs of integers in a given array of integers whose sum is equal to a specified number.
IN C++ Write a program to find the number of comparisons using binarySearch and the sequential...
IN C++ Write a program to find the number of comparisons using binarySearch and the sequential search algorithm as follows: Suppose list is an array of 1000 elements. 3 Search list for some items as follows: a. Use the binary search algorithm to search the list. (You may need to modify the algorithm given in this chapter to count the number of comparisons.) b. Use the binary search algorithm to search the list, switching to a sequentialsearch when the size...
IN C++ Write a program to find the number of comparisons using binarySearch and the sequential...
IN C++ Write a program to find the number of comparisons using binarySearch and the sequential search algorithm as follows: Suppose list is an array of 1000 elements. 2 Use any sorting algorithm to sort list.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT