Question

Assignment 4 - For example 2, write a non recursive version of this function... using a regular loop.

Lets consider writing a function to find the factorial of an integer, N!. For example 7! equals 7*6*5*4*3*2*1. int myFactori

C++

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

Source Code:

#include <iostream>
using namespace std;
int myFactorial(int n)
{
int fact=1;
for(int i=1;i<=n;i++)
{
fact=fact*i;
}
return fact;
}
int main()
{
int num=0;
cout<<"Enter the number for factorial:";
cin>>num;
cout<<"factorial of "<<num<<" is "<<myFactorial(num)<<endl;
return 0;
}

main.cpp 1 #include <iostream> 2 using namespace std; 3 int myFactorial(int n) int fact=1; for(int i=1;i<=n;i++) fact=fact*i;

Add a comment
Know the answer?
Add Answer to:
C++ Assignment 4 - For example 2, write a non recursive version of this function... using...
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
  • Consider the following recursive definition of a factorial function. int factorial ( int n) {   ...

    Consider the following recursive definition of a factorial function. int factorial ( int n) {    if ( n == 0 || n ==1 )    return 1;       else    return n * factorial (n-1);    } Suppose the function factorial (4) is invoke. Trace through the function call, explicitly show how the factorial function is repeatedly called and what is the value of n in each call. Also show the value returned by each call. Give an...

  • X266: Recursion Programming Exercise: log For function log, write the missing base case condition and the...

    X266: Recursion Programming Exercise: log For function log, write the missing base case condition and the recursive call This function computes the log of n to the base b.As an example: log 8 to the base 2 equals 3 since 8 = 2*2*2. We can find this by dividing 8 by 2 until we reach 1, and we count the number of divisions we make. You should assume that n is exactly b to some integer power. Examples: log(2, 4)...

  • (Recursive Function) Display the triangle pattern without using loop. Write a recursive function named Triangle to...

    (Recursive Function) Display the triangle pattern without using loop. Write a recursive function named Triangle to solve the problem. void Triangle(int); Write a main function to test it. For example, a function call Triangle(6) will displays the following 6 rows of a triangle pattern. 1 1 2 1 1 2 3 2 1 1 2 3 4 3 2 1 1 2 3 4 5 4 3 2 1 1 2 3 4 5 6 5 4 3 2 1...

  • C++ Create three recursive functions that accomplish the following: Recursive Power Function this function will raise...

    C++ Create three recursive functions that accomplish the following: Recursive Power Function this function will raise a number to a power. The function should accept two arguments, the number to be raised and the exponent. Assume that the exponent is a non-negative integer. String Reverser function that accepts a string object as its argument and prints the string in reverse order. Sum of Numbers this function accepts an integer argument and returns the sum of all the integers from 1...

  • PROBLEM: Write a recursive method named SumEvens that takes an integer X and returns the sum...

    PROBLEM: Write a recursive method named SumEvens that takes an integer X and returns the sum of even digits in X. Please comment on every line of code. EXISTING CODE: int numTwos(int n) { int right = n % 10; int remain = n / 10; if(n==0) { return 0 ; } else if(right ==2) { //rightmost digit is 2 return 1 + numTwos(remain); else { return 0 + numTwos(remain); } } int SumDigits(int X) { if (X == 0)...

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

  • IN MATLAB RECURSIVE FUNCTION 1. Sum of Factorials. The Recursive Function: A series that sums up...

    IN MATLAB RECURSIVE FUNCTION 1. Sum of Factorials. The Recursive Function: A series that sums up the factorial of the natural numbers from 1 to N can be expressed as The recursive algorithm: N-1 N-2 N-3 Write independent matlab code to solve the above problem with the following methods: 1. 2. 3. A monolithic program (with no functions) A standard (non-recursive) user defined function (an a program to call it) A recursive function (an a program to call it) Test...

  • C - Language Create a recursive function to print a multi-digit number vertically. For example, 2378...

    C - Language Create a recursive function to print a multi-digit number vertically. For example, 2378 should be printed as 2 3 7 8 Be sure to test your program with numbers of different length. The recursive function should return an int and take an int as a parameter. The function should have a base case where the parameter is 0 and this should return 0 Define a temp variable using the remainder operator where temp is equal to the...

  • Use C++ to write a recursive function that takes in a non-negative integer and returns the...

    Use C++ to write a recursive function that takes in a non-negative integer and returns the count of the occurrences of 7 as a digit, so for example 717 yields 2. Note: Even though it would be easy to solve this problem iteratively, the goal is to get some practice solving problems recursively.

  • *Program is in C* Write a recursive function to compute a^b for integers a and b....

    *Program is in C* Write a recursive function to compute a^b for integers a and b. For the recursive use the following equality a^b = a^b - 1 * a and a^0 = 1. What does the following recursive function do? int mystery(int a, int b) {if (b == 1) return (a); else return (a + mystery(a, b - 1));}

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