C++
//Write prototype for function factorial that accepts an int num and returns an int
/*
WITH LOOP OF YOUR CHOICE:
Write code for function factorial that accepts an int num
and returns the num's factorial
factorial(5);
1*2*3*4*5
returns 120
DON'T FORGET TO WRITE TEST CASE.
*/
//write includes statements
//write using statements for cin and cout
/*
Use a do while loop to prompt the user for
a number, call the factorial function, and display the number's
factorial. Also, loop continues as long as user wants to.
*/
#include <iostream> using namespace std; int factorial(int n); int main() { int n; do { cout << "Enter a number(negative to stop): "; cin >> n; if (n >= 0) { cout << "Factorial of " << n << " is " << factorial(n) << endl; } } while (n >= 0); return 0; } int factorial(int n) { int result = 1; for (int i = 1; i <= n; ++i) { result *= i; } return result; }
C++ //Write prototype for function factorial that accepts an int num and returns an int /*...
C++ clock.h /* Write a function get_hours that returns an int and accepts an int seconds_since_1970 parameter */ int get_hours(int seconds_since_1970); /* Write a function get_minutes that returns an int and accepts an int seconds_since_1970 parameter */ int get_minutes(int seconds_since_1970); /* Write a function get_seconds that returns an int and accepts an int seconds_since_1970 parameter */ int get_seconds(int seconds_since_1970); clock.cpp #include "clock.h" /* Write get_hours code to return hours given seconds since 1970 int */ /* Write get_minutes code to...
C programming
KAM5 Write a function unsigned long long int factorial(int num) to compute the factorial of a number. The factorial of a number is given by n! = 1*2* ... *n So 1! = 1, 2! = 1*2, 3! = 1*2*3 ... For example: Test Result printf("%llu\n", factorial(5)); 120 printf("%lu\n", factorial(20)); 2432902008176640000
C++ //get_letter_grade.h /* Write a function gpa_to_letter_grade that returns a string and accepts a double gpa parameter */ //get_letter_grade.cpp /* Write function code for gpa_to_letter_grade that returns a string and accepts a double gpa parameter YOU MUST USE A SWITCH STATEMENT Given a double 3.5 returns the string A TIP: You'll have to convert the double to an int using multiplication Table 3.5 to 4 returns an A 3.0 to 3.49 returns a B 1.7 to 2.99 returns a C...
Given the following function: int fun1(int count){ int Num ; for (i = 0; i < count; ++i) { cin >> Value; if (i == 0) Num = Value; else if (Value > Num) Num = Value; } return Num ; } What is the output of the following C++ code segment if the following list is entered? (Input list: 5 -15 -90 -2 -60 -30) int Num = 0 , Value, numValues; cin >> numValues; if (numValues > 0) cout...
In c++ code and test a function with prototype float except(int arg); that accepts an int argument and returns the expression 1.0 / arg if arg != 0; otherwise, except() throws an exception of type string. A suitable message might be “Error – attempt to divide by zero”.
Help with these questions
A function rectarea has already been defined with this prototype: int rectarea(int width); It collects an integer height from the user and returns the area of the resulting triangle; Write code that will call rectarea twice with values 5 and 10, and print the results obtained. Warning: in a composed function like "f(a),b0);", you don't know whether a) will be called first or b(!. The variable age has been defined as an integer. Write code which...
Write a method named factorial that accepts an integer n as a parameter and returns the factorial of n, or n!. A factorial of an integer is defined as the product of all integers from 1 through that integer inclusive. For example, the call of factorial(4) should return 1 2 3 4, or 24. The factorial of 0 and 1 are defined to be 1. You may assume that the value passed is non-negative and that its factorial can fit...
/* Array expander: Write a function that accepts an int array as argument. The function should create a new array that is n times the size of the argument array, where n is a user input. The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0. The function should return a pointer to the new array */ #include <iostream> using namespace std; const int NUM_ELEM...
(C++ program )Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to the element 1 of the new array. Element 1 of the argument array should be copied to element 2 of the new array, and so forth....
in c++ language 1.Re-write the following for loop statement by using a while loop statement: int sum = 0; for(int i=0;i<=1000;i++){ sum+=i; } 1 (b). Following is the main () function of a program. The program asks a user to enter 150 integers and print the largest integer user entered. int main() { int score, highest; //missing portion of the program return 0; } 1(c). Find the missing portion of the following code, so the...