Question

In: Computer Science

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).  

Solutions

Expert Solution

Program:

#include<iostream>

using namespace std;

#define size 5737 // It produce upto 5737 digits

void factorial(int n); // Function prototype

int fact(int i, int array[], int arraySize); // Function prototype

int main()

{

  int n; // variable declaration

  cout<<"Enter a number(upto 2000): ";

  cin>>n; // Accept n value

  cout<<"Factorial of "<<n<<" is:"<<endl;

  factorial(n); // calling function to find the factorial

  return 0;

}

void factorial(int n) // calling function to find factorial

{

  int array[size];

  array[0] = 1;

  int arraySize = 1;

  

  for (int i=2; i<=n; i++)

    arraySize = fact(i, array, arraySize); // recusive calling to find output size

  

  for (int j=arraySize-1; j>=0; j--)

    cout << array[j]; // print factorial using array

  cout<<endl;

}

int fact(int i, int array[], int arraySize) // calling function to find output size

{

  int check = 0;

  for (int k=0; k< arraySize; k++)

  {

int m = array[k] * i + check;

    array[k] = m % 10;

    check = m/10;

  }

  while (check)

  {

    array[arraySize] = check%10;

    check = check/10;

    arraySize++;

  }

  return arraySize;

}

Output:


Related Solutions

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 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. 
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]
In C++ Write a program that dynamically allocates a built-in array large enough to hold a...
In C++ Write a program that dynamically allocates a built-in array large enough to hold a user-defined number of test scores. (Ask the user how many grades will be entered and use a dynamic array to store the numbers.) Once all the scores are entered, the array should be passed to a function that calculates the average score. The program should display the scores and average. Use pointer notation rather than array notation whenever possible. (Input Validation: Do not accept...
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 a C++ program that uses array to store the salaries of 10 employees working in...
Write a C++ program that uses array to store the salaries of 10 employees working in a small firm. The program should take average of the salaries and the max and min salaries being paid to the employees
Program in C: Write a program in C that reorders the elements in an array in...
Program in C: Write a program in C that reorders the elements in an array in ascending order from least to greatest. The array is {1,4,3,2,6,5,9,8,7,10}. You must use a swap function and a main function in the code. (Hint: Use void swap and swap)
On a raspberry pi, write an assembler program that will calculate the factorial of an integer...
On a raspberry pi, write an assembler program that will calculate the factorial of an integer value inputted by the user. the program should detect if an overflow occurs. If no overflow occurred, then it should print out the value of the factorial. Otherwise print out a message indicating that an overflow occurred.
Language: c++ works in visual basic Write a program that uses an array of nested structs...
Language: c++ works in visual basic Write a program that uses an array of nested structs to store the addresses for your store’s customers.  Each customer has a name and two addresses: home address and business address.  Each address has a street, city, state, and zip code. Requirements: 1. Data structure a. Define an Address struct with street, city, state and zip fields b. Define a Customer struct with lastNm and firstNm fields, plus homeAddr and busAddr fields...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT