Question

Done in C++ using visual studio Instructions to solve all three problems below using function •...

Done in C++ using visual studio

Instructions to solve all three problems below using function

• The value of n should be asked from user in the main() and pass the value of n to an additional function as argument value; Ex: Factorial(n).

• The functions should calculate the values according to the formulas below and return the values.

• Then the main() function should print out those returned values.

• All three parts should have their own separate function, since formulas are different.

a. Write a function to calculate F = n!

b. Write a function to calculate for F whereby n can be equal to any integer and F will be as the following: F = 1/1 + 1/2 + 1/3 + ⋯ + 1/n For example, if n=10, then F = 1/1 + 1/2 + 1/3 + ⋯ + 1/10

c. Write a function to calculate for F whereby n can be equal to any integer and F will be as the following: F = 1/(1!) + 1/(2!) + 1/(3!) + ⋯ + 1/(n!) For example, if n=10, then F = 1/(1!) + 1/(2!) + 1/(3!) + ⋯ + 1/(10!)

Hint: The factorial of a nonnegative integer n is written as n! (Pronounced “n factorial”) and is defined as follows: n! = n*(n-1)*(n-2). …… *1 (for values of n greater than or equal to 1) and n! =1 (for n =0). For example, 5! = 5*4*3*2*1, which is 120.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>

using namespace std;

int factorial(int n) {
    if (n == 0)
        return 1;
    else
        return n * factorial(n-1);
}

int main() {
    int n;
    cout << "Enter a number: ";
    cin >> n;
    cout << "Factorial of " << n << " is " << factorial(n) << endl;
    return 0;
}
Add a comment
Know the answer?
Add Answer to:
Done in C++ using visual studio Instructions to solve all three problems below using function •...
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
  • Done in C++ using visual studio 1. Write a program with one additional function called int[]...

    Done in C++ using visual studio 1. Write a program with one additional function called int[] reverseArray(int array). This function will receive an array from main and then reverse and should return the reversed array to the main to print out. Use a single array and a single loop, you’re are neither allowed to print out just in reverse order nor allowed to use another array variable to store the original array in reverse order. 2. Write a program which...

  • 1. Write a C programme by using visual Studio: a) Write a function with parameters that...

    1. Write a C programme by using visual Studio: a) Write a function with parameters that returen the largest of three integer arguments. So users could call your function (name: max3) to output the maximum of three input values. b) Make a function outside of the main routine. And in the main routine, please call this function and print the harmonic mean. The harmonic mean of two numbers is obtained by taking the inverses of the two numbers, averaging them,...

  • Write the c++ code that will run with visual studio With the square function below as...

    Write the c++ code that will run with visual studio With the square function below as an example, write a max function that has two int parameters and will return back the larger of the two. Also write a main which can be used to test your function. ex ) int square(intnum); // funcprototype int main() {    inta = 5, b; a = square(a);    b = square(4);cout<< square( 6 ) << endl;    ....        // func...

  • Please write below code in C++ using Visual Studio. Write program that uses a class template...

    Please write below code in C++ using Visual Studio. Write program that uses a class template to create a set of items. The program should: 1. add items to the set (there shouldn't be any duplicates) • Example: if your codes is adding three integers, 10, 5, 10, then your program will add only two values 10 and 5 • Hint: Use vectors and vector functions to store the set of items 2. Get the number of items in the...

  • If you’re using Visual Studio Community 2015, as requested, the instructions below should be exact but...

    If you’re using Visual Studio Community 2015, as requested, the instructions below should be exact but minor discrepancies may require you to adjust. If you are attempting this assignment using another version of Visual Studio, you can expect differences in the look, feel, and/or step-by-step instructions below and you’ll have to determine the equivalent actions or operations for your version on your own. INTRODUCTION: In this assignment, you will develop some of the logic for, and then work with, the...

  • Use C++, visual studio format. Please Write a program to solve the quadratic equation and find...

    Use C++, visual studio format. Please Write a program to solve the quadratic equation and find only the real and equal roots. the program should test for three types of roots tell the users the roots type and then calculate the real roots. the user enters the values of a ,b and c. you calculate r1 and r2. c

  • using Microsoft Visual Studio C++ write a compute program that will allow the user to: 1.Get...

    using Microsoft Visual Studio C++ write a compute program that will allow the user to: 1.Get a Factorial (using a loop) 2. perform addition and subtraction 3.allow the user to quit the program

  • C# - Using Visual Studio 2017/2019 The Fibonacci sequence is a numerical sequence that follows a...

    C# - Using Visual Studio 2017/2019 The Fibonacci sequence is a numerical sequence that follows a simple pattern: 1,1, 2, 3, 5, 8, 13, 21, 34, 55, By definition, the first two numbers are 1 and 1, and each subsequent number is the sum of the previous two. For your program, you will ask the user to enter a number, n, and then calculate and display the values of the Fibonacci sequence to the nth position. ==sample output== Enter an...

  • C++ ONLY THANKS! USE VISUAL STUDIO COMPILER ONLY! Part 1: Exercise 1: Problem description Write a...

    C++ ONLY THANKS! USE VISUAL STUDIO COMPILER ONLY! Part 1: Exercise 1: Problem description Write a three function program (your main function and two other functions). The main function of the program should create an array that can store 10 integers. The main function will then pass the array to a second function that gets the integers from the user and stores them in the array. Then your main will call a third function that displays the elements in the...

  • Needs to be done in visual studio. I keep having problems with making my own function...

    Needs to be done in visual studio. I keep having problems with making my own function to call for the statistics and the random number generator. they are both supposed to be separate functions from the main. Can anyone help? Language is C Overview: This third programming assignment will be the first to require the use of arrays. Like the second program, this program will be modular and make use of functions (including, this time, functions which process arrays). Brief...

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