Question

i need this in c program please Computing Factorials Lab #2 Name your program lab2.c Start...

i need this in c program please

Computing Factorials

Lab #2

Name your program lab2.c

Start by asking the user to enter a number

between 1 and 15

Check if the user has complied.

Keep asking the user for a number between 1

and 15 until the user complies.

Then ask if recursion is desired or not.

If recursion is not desired call a function to calculate

N! (where N is the number entered)

If recursion is required call a recursive function to

calculate N! (where N is the number entered)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:


#include <stdio.h>

// recursive function to calculate factorial
long recurivefact(int n) {
if (n>=1)
return n*recurivefact(n-1);
else
return 1;
}

// iterative function to calculate to factorial
long fact(int n){
int fact=1,i;
for (i = 1; i <= n; i++) {
fact =fact*i;
}
return fact;
}

int main()
{
int n;
char ch,choice='N';
// if choice is 'N' or 'n' keep running
while(choice=='N' || choice=='n'){\
// take user input of N
printf("Enter a number between 1 and 15:\n");
scanf("%d",&n);
  
// ask if user need the answer using recursive function or iterative function
printf("recursion is desired or not (Y/N):\n");
scanf(" %c", &ch);
// if ch is 'Y' or 'y' call recursive function else iterative function
if(ch=='Y' || ch=='y')
printf("%ld\n",recurivefact(n));
elsel
printf("%ld\n",fact(n));
// ask the user if he want to continue compiling(running) or not
printf("Do you wish stop compiling (Y/N):\n");
scanf(" %c",&choice);
}

return 0;
}

SCREENSHOTS:

OUTPUT:

Please upvote if you like my answer and comment below if you have any queries.

Add a comment
Know the answer?
Add Answer to:
i need this in c program please Computing Factorials Lab #2 Name your program lab2.c Start...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Need help with code using pointer only in C Ask the user to enter a number...

    Need help with code using pointer only in C Ask the user to enter a number between 1 and 10 •Check if the user has complied. •Keep asking the user for a number between 1 and 10 until the user complies. •Then calculate N! (no recursion)

  • C++ PROGRAM ONLY! For this lab you need to write a program that will read in...

    C++ PROGRAM ONLY! For this lab you need to write a program that will read in two values from a user and output the greatest common divisor (using a recursive implementation of the Euclidean algorithm) to a file. In Lab #3, you implemented this program using an iterative method. Greatest Common Divisor In mathematics, the greatest common divisor (GCD) of two or more integers (when at least one of of them is zero then the larger value is the GCD....

  • Write a working C program that will do the following: 1. Include your name, Lab Test...

    Write a working C program that will do the following: 1. Include your name, Lab Test number, and student id as comments in the first lines of the program 2. User is first prompted to enter an integer number between 1 and 20, to be stored as variable named number. If the number is outside the range 1-20, the program will end. 3. If the number is within the allowed range, variable number is passed to function multF(). The multF()...

  • Write a working C program that will do the following: 1. Include your name, Lab Test...

    Write a working C program that will do the following: 1. Include your name, Lab Test number, and studentId as comments in the first lines of the program 2. User is first prompted to enter an integer number between 1 and 20, to be stored as variable named number. If the number is outside the range 1-20, the program will end. 3. If the number is within the allowed range, variable number is passed to function multF(). The multF() function...

  • Write a working C program that will do the following: 1.Include your name, Lab Test number,...

    Write a working C program that will do the following: 1.Include your name, Lab Test number, and studentId as comments in the first lines of theprogram 2.User is first prompted to enter an integer number between 1 and 20, to be stored asvariable named number. If the number is outside the range 1-20, the program will end. 3.If the number is within the allowed range, variable number is passed to function multF().The multF() function computes and returns the result of:...

  • CS0007 Good day, I am in an entry-level Java class and need assistance completing an assignment....

    CS0007 Good day, I am in an entry-level Java class and need assistance completing an assignment. Below is the assignment criteria and the source code from the previous assignment to build from.  Any help would be appreciated, thank you in advance. Source Code from the previous assignment to build from shown below: Here we go! Let's start with me giving you some startup code: import java.io.*; import java.util.*; public class Project1 { // Random number generator. We will be using this...

  • Write a java program that demonstrates recursion. This program can be in a java GUI frame...

    Write a java program that demonstrates recursion. This program can be in a java GUI frame or as a console application. On this one it is up to you. (It would probably work better as a console program for this particular program.) The input for this program is a number of boxes. Ask the user for this with a textbox or a prompt on the command line. Demonstrate recursion with a function called LoadTruck() that takes a number of boxes...

  • Please solve the program in C++(Recursive) Write a function called factorialIterative(). This function should take a...

    Please solve the program in C++(Recursive) Write a function called factorialIterative(). This function should take a single BIG integer (bigint) as its parameter n, and it will compute the factorial n! of the value and return it as its result (as a bigint). Write this functions using a loop (do not use recursion). 2. Write the factorial function again, but using recursion. Call this function factorialRecursive(). The function takes the same parameters and returns the same result as described in...

  • C++ Hey, I really need help with this lab. I don't understand the function part at...

    C++ Hey, I really need help with this lab. I don't understand the function part at all. It'll be great way for me to understand. Than you Functions and loops: Write a program that will ask the user which type of triangle they would like to draw on the characters. The choices are screen with left-top-big left-bottom-big right-top-big right-bottom-big Get the user's choice, ask how big it should be, and then draw the shape. After each successful drawing, ask the...

  • LANGUAGE IS C++ Lab Ch14 Recursion In this lab, you are provided with startup code which...

    LANGUAGE IS C++ Lab Ch14 Recursion In this lab, you are provided with startup code which has six working functions that use looping (for, while, or do loops) to repeat the same set of statements multiple times. You will create six equivalent functions that use recursion instead of looping. Although looping and recursion can be interchanged, for many problems, recursion is easier and more elegant. Like loops, recursion must ALWAYS contain a condition; otherwise, you have an infinite recursion (or...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT