Question

In: Computer Science

Write a C function to calculate and return the factorial value of any positive integer as...

Write a C function to calculate and return the factorial value of any positive integer as an
argument. Then call this function from main() and print the results for the following input
values:
2, 3,4, 5, 10, 15
What is the maximum value of an integer for which factorial can be calculated correctly on a
machine using your algorithm?

Solutions

Expert Solution

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

long long factorial(int num);
int findMaxValue();
int main() {

int num;
printf("Enter a number :");
scanf("%d",&num);

printf("Factorial of %d is %lld\n",num,factorial(num));
printf("Maximum value of an integer for which factorial can be calculated correctly is %d\n",findMaxValue());
   return 0;
  
}

long long factorial(int num) {
if (num <= 1)
return 1;
else
return num * factorial(num - 1);
}
int findMaxValue()
{
   int res = 2;
long long int fact = 2;
while (1)
{
if (fact < 0)
break;
res++;
fact = fact * res;
}
return res - 1;
}

___________________________

Output:

__________________Thank You


Related Solutions

Create a program that will calculate the factorial of any user entered integer. For any positive integer, the factorial is given as
Create a program that will calculate the factorial of any user entered integer. For any positive integer, the factorial is given as: n! = n·(n-1)·(n-2)·.……·3·2·1. The factorial of 0 is 1. If a number is negative, factorial does not exist and this program should display error message. Sample output dialogues should look like these:  Enter an integer: -5 ERROR!!! Tactorial of a negative number doesn't exist  Enter an integer: 10  Factorial = 3628800
Write a recursive function to calculate and return factorial of a given number 'n'. in C...
Write a recursive function to calculate and return factorial of a given number 'n'. in C progrmaining
C++ 1. Write a function decimalToBinary() that takes in a positive integer as a parameter and...
C++ 1. Write a function decimalToBinary() that takes in a positive integer as a parameter and use as stack to convert the integer to a its corresponding binary representation. Hint: divide the integer by 2. 2. A palindrome is a string of characters (a word, phrase, or sentence) that is the same regardless of whether you read it forward or backward—assuming that you ignore spaces, punctuation, and case. For example, Race car is a palindrome. So is A man, a...
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.
python exercise: a. Write a function sumDigits that takes a positive integer value and returns the...
python exercise: a. Write a function sumDigits that takes a positive integer value and returns the total sum of the digits in the integers from 1 to that number inclusive. b. Write a program to input an integer n and call the above function in part a if n is positive, else give ‘Value must be Positive’ message. sample: Enter a positive integer: 1000000 The sum of the digits in the number from 1 to 1000000 is 27000001 Enter a...
a. Write a function sumDigits that takes a positive integer value and returns the total sum...
a. Write a function sumDigits that takes a positive integer value and returns the total sum of the digits in the integers from 1 to that number inclusive. b. Write a program to input an integer n and call the above function in part a if n is positive, else give ‘Value must be Positive’ message. Sample Runs: Enter a positive integer: 1000000 The sum of the digits in the number from 1 to 1000000 is 27000001 Enter a positive...
Create the function fact that had a positive integer argument and returns its factorial . Recall...
Create the function fact that had a positive integer argument and returns its factorial . Recall that factorial (n) = 1*2*3***n. For example factorial (4)=1*2*3*4=24, (python)
4. Write a function called mean that will return the average value of an integer array....
4. Write a function called mean that will return the average value of an integer array. The integer array and its length must be passed to the function as parameters. 5.Write a function called max that will return the index of the max value of an integer array. The integer array and its length must be passed to the function as parameters. E.g. array = {1,2,3,4,5,6,7,8,9,0} max = 9 index = 8 6.Write a function called variance that calculates and...
Write a Python function next_Sophie_Germain(n), which on input a positive integer n return the smallest Sophie...
Write a Python function next_Sophie_Germain(n), which on input a positive integer n return the smallest Sophie Germain prime that is greater or equal to n.
Write a C function boolean isPrime (int n), that would take a positive integer n as...
Write a C function boolean isPrime (int n), that would take a positive integer n as a parameter and return true or false whether the number is a prime number. You should check the range of n and print proper message (For example, if n is a genitive number, you should print out an error message).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT