Question

In: Computer Science

1. Write a program in C++ to find the factorial of a number. Prompt the user...

1. Write a program in C++ to find the factorial of a number. Prompt the user for a number and compute the factorial by using the following expression. Use for loops to write your solution code.
Factorial of n = n! = 1×2×3×...×n; where n is the user input.
Sample Output:
Find the factorial of a number:
------------------------------------
Input a number to find the factorial: 5
The factorial of the given number is: 120

2. Code problem 1 using While loop.

3. Write the code for a C++ program testMath.cpp that reads in two integer numbers from user input. Write the following functions in addition to the main() function for computing and returning the following to the main( ) function. 1. Function sum( ) computes and returns the sum of the two numbers, 2. Function diff( ) computes and returns the difference of the two numbers (always positive), 3. Function avg( ) computes and returns the average, 4. Function max( ) computes and returns the maximum of the two numbers 5. Function min( ) computes and returns the minimum of the two numbers   
The main( ) function performs the unit testing and tests all the functions.
Sample Output
This program reads in two integer numbers from user input
and returns their sum, difference, average, maximum and minimum.
Please enter the two numbers: 17 5
The numbers entered are 17 and 5.
The sum of 17 and 5 is: 22
The difference of 17 and 5 is: 12
The average of 17 and 5 is: 11
Fall 2019
The maximum of 17 and 5 is: 17
The minimum of 17 and 5 is: 5

Solutions

Expert Solution

#include <iostream>
using namespace std;
// part one
int factorialFor(int n){
   int f = 1;
   if (n == 0 || n == 1)
   {
       return f;
   }
   for (int i = 2; i <= n; ++i)
   {
       f *= i;
   }

   return f;
}
// part two
int factorialWhile(int n){
   int f = 1, i = 2;
   if (n == 0 || n == 1)
   {
       return f;
   }
   while(i <= n)
   {
       f *= i;
       ++i;
   }
   return f;
}

int main()
{
   int n;
   cout<<"Input a number to find the factorial: ";
   cin>>n;

   cout<<"\nThe factorial of the given number is: "<<factorialFor(n)<<endl;

   cout<<"The factorial of the given number is(using while): "<<factorialWhile(n)<<endl;
   return 0;
}

// part 3

#include <iostream>
using namespace std;

int sum(int a, int b){
   return (a+b);
}

int diff(int a, int b){
   return (a > b? (a-b):(b-a));
}

int agv(int a, int b){
   return (a+b)/2;
}

int max(int a, int b){
   return (a > b? a:b);
}

int min(int a, int b){
   return (a > b? b:a);
}

int main()
{
   int a,b;
   cout<<"Please enter the two numbers: ";
   cin>>a>>b;

   cout<<"The numbers entered are "<<a<<" and "<<b<<"."<<endl;
   cout<<"The sum of "<<a<<" and "<<b<<" is: "<<sum(a,b)<<endl;
   cout<<"The difference of "<<a<<" and "<<b<<" is: "<<diff(a,b)<<endl;
   cout<<"The average of "<<a<<" and "<<b<<" is: "<<agv(a,b)<<endl;
   cout<<"The maximum of "<<a<<" and "<<b<<" is: "<<max(a,b)<<endl;
   cout<<"The minimum of "<<a<<" and "<<b<<" is: "<<min(a,b)<<endl;
  
   return 0;
}


Related Solutions

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. 
IN C This assignment is to write a program that will prompt the user to enter...
IN C This assignment is to write a program that will prompt the user to enter a character, e.g., a percent sign (%), and then the number of percent signs (%) they want on a line. Your program should first read a character from the keyboard, excluding whitespaces; and then print a message indicating that the number must be in the range 1 to 79 (including both ends) if the user enters a number outside of that range. Your program...
C++ PLEASE Write a program to prompt the user to display the following menu: Guess-Number                       ...
C++ PLEASE Write a program to prompt the user to display the following menu: Guess-Number                        Concat-names             Quit If the user selects Guess-number, your program needs to call a user-defined function called int guess-number ( ). Use random number generator to generate a number between 1 – 100. Prompt the user to guess the generated number and print the following messages. Print the guessed number in main (): Guess a number: 76 96: Too large 10 Too small 70 Close...
Write a C program Your program will prompt the user to enter a value for the...
Write a C program Your program will prompt the user to enter a value for the amount of expenses on the credit card. Retrieve the user input using fgets()/sscanf() and save the input value in a variable. The value should be read as type double. The user may or may not enter cents as part of the input. In other words, expect the user to enter values such as 500, 500.00 and 500.10. The program will then prompt the user...
Create a C++ program that will prompt the user to input an integer number and output...
Create a C++ program that will prompt the user to input an integer number and output the corresponding number to its numerical words. (From 0-1000000 only) **Please only use #include <iostream>, switch and if-else statements only and do not use string storing for the conversion in words. Thank you.** **Our class is still discussing on the basics of programming. Please focus only on the basics. Thank you.** Example outputs: Enter a number: 68954 Sixty Eight Thousand Nine Hundred Fifty Four...
Create a C++ program that will prompt the user to input an positive integer number and...
Create a C++ program that will prompt the user to input an positive integer number and output the corresponding number to words. Check all possible invalid input data. (Please use only switch or if-else statements. Thank you.)
Create a C++ program that will prompt the user to input an integer number and output...
Create a C++ program that will prompt the user to input an integer number and output the corresponding number to its numerical words. (From 0-1000000 only) **Please only use #include <iostream> and switch and if-else statements only. Thank you. Ex. Enter a number: 68954 Sixty Eight Thousand Nine Hundred Fifty Four Enter a number: 100000 One Hundred Thousand Enter a number: -2 Number should be from 0-1000000 only
write a program to perform the following in C Your program should prompt the user to...
write a program to perform the following in C Your program should prompt the user to enter ten words, one at a time, which are to be stored in an array of strings. After all of the words have been entered, the list is to be reordered as necessary to place the words into alphabetical order, regardless of case. Once the list is in alphabetical order, the list should be output to the console in order. The program should execute...
Design a program that asks the user for a number and the program computes the factorial...
Design a program that asks the user for a number and the program computes the factorial of that number and displays the result . Implement with two different modules - one that uses a for loop and one that uses a while loop Grading Rubrick Program Compiles Cleanly  syntax errors25 pts-5 per errorProgram runs without runtime errors ( validation)run-time errors 25 pts-5 per errorProgram give correct answersLogic errors30 pts-5 per errorProgram is repeatableNot repeatable5 pts-5 ptsProgram is well modularizedBarely Modularized10 pts-...
JAVA PROGRAM 1. Write a program to find the factorial value of any non-negative number entered...
JAVA PROGRAM 1. Write a program to find the factorial value of any non-negative number entered through the keyboard.(method) (Factorial of n: n! = 1*2*3*…*n, 0! = 1.) 2. Write a program that prompts the user to input a positive integer. It should then output a message indicating whether the number is a prime number. (method) Please post a screenshot of the codes. Thanks!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT