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 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)
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...
Python 3 Calculate factorial Create a function that uses a loop to calculate the factorial of...
Python 3 Calculate factorial Create a function that uses a loop to calculate the factorial of a number. * function name: get_fact * parameters: num (int) * returns: int * operation: Must use a loop here. Essentially calculate and return the factorial of whatever number is provided. but: - If num is less than 1 or greater than 20, print "num is out of range" and exit the function - If num is not an int, print "invalid num parameter"...
C++ Task 1 Write a program that allocates an array large enough to hold 5 student...
C++ Task 1 Write a program that allocates an array large enough to hold 5 student test scores. Once all the scores are entered, the array should be passed to a function that sorts them in ascending order. Another function should be called that calculates the average score. The program should display the sorted list of scores and averages with appropriate headings. Input Validation: Do not accept negative numbers for test scores. Task 2 Modify the program so the lowest...
C++ Task 1 Write a program that allocates an array large enough to hold 5 student...
C++ Task 1 Write a program that allocates an array large enough to hold 5 student test scores. Once all the scores are entered, the array should be passed to a function that sorts them in ascending order. Another function should be called that calculates the average score. The program should display the sorted list of scores and averages with appropriate headings. Input Validation: Do not accept negative numbers for test scores. Task 2 Modify the program so the lowest...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT