I am creating a recursive function in c++ and have the following code so far. However, can you help me fix it so that it doesn't crash when negative numbers are entered. If negative numbers are entered i want to let the user know negative numbers are not accepted and to try again
#include <iostream>
using namespace std;
int multiplication(int x, int y)
{
int sum = y;
//if value of x is 1, then result of x*y will be y
if (x == 1)
return sum;
//if x geater than 1 then call the multiplication function and add
eat iteration
else
sum = sum + multiplication(x - 1, y);
return sum;
}
int main()
{
//variables
int x, y, multi;
cout << "Enter x";
cin >> x;
cout << "Enter y";
cin >> y;
//multiplication function
multi = multiplication(x, y);
cout << multi;
}


The Program Code is :
#include <iostream>
using namespace std;
int multiplication(int x, int y)
{
int sum = y;
///if value of x is 1, then result of x*y will be y
if (x == 1)
return sum;
///if x geater than 1 then call the multiplication function and add
eat iteration
else
sum = sum + multiplication(x - 1, y);
return sum;
}
int main()
{
///variables
int x, y, multi;
cout << "Enter x : ";
cin >> x;
cout << "Enter y : ";
cin >> y;
while(x < 0 || y < 0)/// While loop for finding negative
numbers
{
cout<<"You are entered Negative numbers " <<endl;
cout<<"Please enter Positive numbbers of x and y
"<<endl;
cout << "Enter x : ";
cin >> x;
cout << "Enter y : ";
cin >> y;
}
///multiplication function
multi = multiplication(x, y);
cout << multi;
}

I am creating a recursive function in c++ and have the following code so far. However,...
Fix the function so that it checks if the string is a palindrome #include <iostream> using namespace std; //Fix the function so that it checks if the string is a palindrome //(same forwards as backwards ex: racecar / radar) bool is_palindrome(string str, int i){ //base cases if (/* add the condition */) return false; if (/* add the condition */) return true; //recursive call return is_palindrome(str, i-1); } int main(){ string x; cin >> x; if (is_palindrome(x,x.length())){...
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...
Fix the following code to print whether the input is positive or negative #include <iostream> #include <string> using namespace std; void positiveOrNegative(int); // declare function int main(){ int y; int result; cin >> y; result = positiveOrNegative(y); return 0; } void positiveOrNegative(int x){ if (x>=0) cout << "Positive"; else cout << "Negative"; }
C++ Write a function so that the main() code below can be replaced by the simpler code that calls function MphAndMinutesToMiles(). Original main(): int main() { double milesPerHour; double minutesTraveled; double hoursTraveled; double milesTraveled; cin >> milesPerHour; cin >> minutesTraveled; hoursTraveled = minutesTraveled / 60.0; milesTraveled = hoursTraveled * milesPerHour; cout << "Miles: " << milesTraveled << endl; return 0; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <iostream> using...
Fix this C++ code so that the function prototypes come before main. Main needs to be declared first. Then call to the functions from inside of main. #include<cstdlib> using namespace std; //prompt user for input void getGrades(int gradeArray[],int size) { for(int i=0; i<size; i++) { cout<<"Enter the grade "<<i+1<<": "; cin>>gradeArray[i]; } } // finding average of all numbers in array float computeAverage(int numbers[],int size) { float sum=0; for(int i=0; i<size; i++) { sum = sum + numbers[i]; //compute sum...
61. The following program is accepted by the compiler: int sum( int x, int y ) { int result; result = x + y; } T__ F__ 62. The following implementation is accepted by the compiler: void product() { int a; int b; int c; int result; cout << "Enter three integers: "; cin >> a >> b >> c; result = a * b * c; ...
c++ Write the following 2 functions and test them. Both of them need to be recursive functions. int sum(int n); // recursive version to calculate the sum of 1 + 2 + ..... + n int str_length(char s[]; // Returns length of the string s[] and the null character, '0\', is not counted in the length). Example of program execution; Enter a positive integer: 10 (user input) The sum of 1+ 2+....+10 is: 55 Enter a sentence: Hello World! (user...
Here is what i have so far... However i am having a little bit of trouble with it. when inputting a number the sum comes out perfect but when i break the digits up i cant do mmore than 4 digits and i cant do a negitive number... someone help. QUESTION: Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits. For example,...
15.6: Fix the code to print the count from 1 to x (to loop x times) #include <iostream> // include the header file using namespace std; void count(int x){ for(int i=1; i<=x; i++){ cout<<x; } } int main(){ int x,i; cin >> x; count(x); return 0; } 15.7 Fix the nested for loops to print the count from 1 to x twice, e.g. "12....x12.....x" #include <iostream> // include the header file using namespace std; int main(){...
Create a program with the main function and one additional function. The additional function asks for input from the user and returns the output back to the main function for display. This is for c++ and here is my code, could you guys please tell me what I am missing from the instructions. Thank you #include <iostream> using namespace std; int sum (int, int); int main() { int n1, n2, total; cout << "Enter two numbers...