****I'm trying to compile this code, however, the answer still shows "0". This the formula I'm trying to use: (num1!) / ((num2!)(num1 - num2)! ) ***** (C++)
FOR INSTANCE; Big Number = 20 and Small Number = 3
#include <iostream>
using namespace std;
int factorial(int num1, int num2){
unsigned long long answer = 0;
unsigned long long top = 1;
unsigned long long bottom = 1;
unsigned long long bottom2 = 1;
unsigned long long bottom_3 = 1;
int num3 = num1 - num2;
for(int i = 1 ; i <=num1; i++){
top *= i;
}
cout << "Top: " << top << endl;
for(int i = 1 ; i <=num3; i++){
bottom *= num3;
}
cout << "Bottom: " << bottom << endl;;
for(int i = 1 ; i <=num2; i++){
bottom2 *= num2;
}
cout << "Bottom 2: " << bottom2 << endl;;
bottom_3 = bottom2 * bottom;
cout << "Bottom3 : " << bottom_3 << endl;
answer = (top / bottom_3);
cout << "Answer@FACTORIAL: " << answer << endl;
return answer;
}
int main() {
int num1 = 0;
int num2 = 0;
unsigned long long answer = 0;
cout << "Insert BIG number" << endl;
cin >> num1;
cout<< "Insert SMALL number" << endl;
cin >> num2;
answer = factorial(num1, num2);
cout << "Answer: "<<answer << endl;;
}
//modified a logic mistake
#include <iostream>
using namespace std;
int factorial(int num1, int num2){
unsigned long long answer = 0;
unsigned long long top = 1;
unsigned long long bottom = 1;
unsigned long long bottom2 = 1;
unsigned long long bottom_3 = 1;
int num3 = num1 - num2;
for(int i = 1 ; i <=num1; i++){
top *= i;
}
cout << "Top: " << top << endl;
for(int i = 1 ; i <=num3; i++){
bottom *= i;//modified here
}
cout << "Bottom: " << bottom << endl;;
for(int i = 1 ; i <=num2; i++){
bottom2 *= i;//modified here
}
cout << "Bottom 2: " << bottom2 << endl;;
bottom_3 = bottom2 * bottom;
cout << "Bottom3 : " << bottom_3 << endl;
answer = (top / bottom_3);
cout << "Answer@FACTORIAL: " << answer << endl;
return answer;
}
int main() {
int num1 = 0;
int num2 = 0;
unsigned long long answer = 0;
cout << "Insert BIG number" << endl;
cin >> num1;
cout<< "Insert SMALL number" << endl;
cin >> num2;
answer = factorial(num1, num2);
cout << "Answer: "<<answer << endl;;
}
output:
Insert BIG number
20
Insert SMALL number
3
Top: 2432902008176640000
Bottom: 355687428096000
Bottom 2: 6
Bottom3 : 2134124568576000
Answer@FACTORIAL: 1140
Answer: 1140
Process exited normally.
Press any key to continue . . .
****I'm trying to compile this code, however, the answer still shows "0". This the formula I'm...
So the assignment is to write a program that have the user entered 3 numbers and than it will sort it by ascending order. Here are the code I wrote, but it won't compiled and I kept on getting an error message "Using uninitiazed memory" for all the variables. Can someone please help take a look and see whats going on? #include <iostream> using namespace std; int main() { //variables int num1, num2, num3; int lowest, middle, highest; //user inputs...
I'm kind of new to programming, and I am having trouble figuring out why my program isn't running. Below is the code that I wrote for practice. I will comment where it says the error is. So the error that I'm getting is "error: no match for 'operator>>' (operand types are 'std::istream {aka std::basic_istream<char>}' ". I'm not sure why I'm getting this because I added the library <iostream> at the top. Thank you. Code: #include <iostream> using namespace std; class...
Fix this code so only the function prototype comes before main. #include <iostream> using namespace std; bool isMultiple(int num1, int num2) { return num1 % num2 == 0; } int main() { char ch = 'Y'; int num1, num2; while(ch =='Y') // While ch is equal to Y { cout << "Enter two numbers(largest first): "; cin >> num1; // Getting 1st number cin >> num2; // Getting 2nd number if(isMultiple(num1, num2)) cout << num2 << " " << "IS...
6. (Short answer) The C++ code below is very close to working, but, as written, won't compile. When trying to compile, the compiler generates a number of errors similar to "cout' was not declared in this scope." Respond to the prompts below about the code.(20 points, 5 points each) #include <iostream> void FindMax(int a, int b, int& max) { if (a > b) max = a; else { max = b; } int main() { int num1 = 0; int...
This is a fill in the code type: // FILL IN THE CODE - Write a program to multiply 2 numbers, print out the results and print out the original numbers in ascending order. #include <iostream> using namespace std; int main() { int num1; // holds 1st number int num2; // holds 2nd number int result; // holds result of multiplication int *num1Ptr = nullptr; // int pointer which will be set to point to the 1st number int *num2Ptr...
#include <iostream> #include <conio.h> #include<limits> using namespace std; int main(){ char oparand, ch = 'Y'; int num1, num2, result; while(ch == 'Y'){ cout << "Enter first number: "; cin >> num1; while(1){//for handling invalid inputs if(cin.fail()){ cin.clear();//reseting the buffer cin.ignore(numeric_limits<streamsize>::max(),'\n');//empty the buffer cout<<"You have entered wrong input"<<endl; cout << "Enter first number: "; cin >> num1; } if(!cin.fail()) break; } cout << "Enter second number: "; cin >> num2; while(1){ if(cin.fail()){ cin.clear(); cin.ignore(numeric_limits<streamsize>::max(),'\n'); cout<<"You have entered wrong input"<<endl; cout <<...
Please help asap
Q6
6" (15%) Fill up the missing bodies of factorialO and fibonacciO of the following codes: #include <iostream> using namespace std; unsigned long factorial(unsigned long); unsigned long fibonacci(unsigned long); / / function prototype int main0 unsigned long size_factorial, size fibonacci; cout << "input an unsigned integer for the upper bound of factorialO" < endl; cin >> size factorial; cout << "input an unsigned integer for the upper bound of fibonacci)" <endl; cin >>size_fibonacci; cout << "the result...
C++ question I'm trying to write a code that takes in n number of variables between 00 and 99. The program should be able to output the 1st and 2nd lowest digits as well as the highest and second highest digits. So if the input is 4 - 50 20 10 70 it should output 10 20 50 70. I think I've gotten the basic code but I can't seem to get the last digit to shift. #include <iostream> #include...
What are the errors in the following code? My professor gave us this prewritten code and asked us to find the errors in it so that it can run properly #include <cstdlib> #include <ctime> #include <iostream> using namespace std; // input: integer // output: none // adds five to the given parameter void addFive( int x ) { x += 5; } // input: none // output: a random number int generateRandomNumber() { srand( time(0) ); return rand() % 100;...
I'm trying to write in C++ a way to have the user input an double one at a time and have the program output (on entering 0) the number of times those variables differ by at least 1. For example if I enter 1, 1.7, 0.8, -0.1, -1, 0 ... I should get back 4. (or if I just input 0, the output should just be 0). I'm having trouble understanding how to use a while/for operation to make this...