Question

I am creating a recursive function in c++ and have the following code so far. However,...

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;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

tart here X *negative.cpp X #include <iostream> using namespace std; 1 2 3 4 5 6 7 8 int multiplication (int x, int y) { intOut Put: D: 19891A0224\negative.exe Enter x : -10 al Enter y : -20 You are entered Negative numbers tPlease enter Positive nu

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;

}

Dear Student: This Program given the solution for your question Still you have any query? Feel free to ask in comment section

Add a comment
Know the answer?
Add Answer to:
I am creating a recursive function in c++ and have the following code so far. However,...
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
  • Fix the function so that it checks if the string is a palindrome #include <iostream> using...

    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;...

    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...

    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...

    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...

    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...

    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...

    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...

    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)...

    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(){...

  • what is the output for the following code? explain the steps. /*#include <iostream> using namespace std;...

    what is the output for the following code? explain the steps. /*#include <iostream> using namespace std; int f(int &i) { i = 10; return(5 * i); } int main() { int n = 5; f(n); cout << n << "\n"; return 0; } #include <iostream> using namespace std; int sub1(int n) { n--; return n; } int main() { int m = 10; for(int j = 0; j < 10; j++) m -= sub1(j); cout << m << "\n"; return...

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