Concept of recursive function to take user input of two numbers and performs an addition of those two numbers
/*
* p4_pshr_Adder.cpp
*
* Created on: Nov 23, 2010
* Program Description:
* This program uses the concept of recursive function to take user input of two numbers and
* performs an addition of those two numbers.
*/
#include <iostream>
#include <cctype>
#include <iomanip>
using namespace std;
class Add {
private:
public:
unsigned int Adder(unsigned int, unsigned int);
};
unsigned int Add::Adder(unsigned int num1,unsigned int num2) {
// This recursive function very smartly deals with the 3 possible cases of the input number
// entered by the user being both equal to zero, one number being equal to zero and other number
// being equal to zero.
if (num1 != 0 && num2 != 0) {
// If both number entered by user becomes zero, this recursive function comes to and end
// and moves to the next line. But, if both the numbers are non-zero, then, it adds "2"
// the times either one of the number is not equal to zero as we decrease the input by "1" each!!
return 2 + Adder(num1 - 1, num2 - 1);
}
else if (num1 == 0 && num2 != 0) {
// If one of the number is zero, it decreases the other number by 1 and adds "1" on to it!!
return 1 + Adder(0, num2 - 1);
}
else if (num2 == 0 && num2 != 0) {
// If the other number is zero, it decreases the next number by 1 and adds "1" on to it!!
return 1 + Adder(num1 - 1, 0);
}
else {
// If both the number entered by user was "0" then it return "0" as the sum.
return 0;
}
}
int main(void) {
Add a1; // Instance of class Add.
unsigned int addentOne; // Variable to store unsigned int.
unsigned int addentTwo; // Variable to store unsigned int.
// User Input
cout << endl << "Enter the first number: ";
cin >> addentOne;
cout << endl << "Enter the second number: ";
cin >> addentTwo;
// Formatting the output using recursive function created in the class.
cout << endl << "ADDITION USING RECURSION";
cout << endl << "------------------------";
cout << endl << " " << addentOne;
cout << endl << " +" << addentTwo;
cout << endl << " ------";
cout << endl << " " << a1.Adder(addentOne, addentTwo);
// Calling the recursive function Adder of the instance of class Add (a1).
return 0;
}
* p4_pshr_Adder.cpp
*
* Created on: Nov 23, 2010
* Program Description:
* This program uses the concept of recursive function to take user input of two numbers and
* performs an addition of those two numbers.
*/
#include <iostream>
#include <cctype>
#include <iomanip>
using namespace std;
class Add {
private:
public:
unsigned int Adder(unsigned int, unsigned int);
};
unsigned int Add::Adder(unsigned int num1,unsigned int num2) {
// This recursive function very smartly deals with the 3 possible cases of the input number
// entered by the user being both equal to zero, one number being equal to zero and other number
// being equal to zero.
if (num1 != 0 && num2 != 0) {
// If both number entered by user becomes zero, this recursive function comes to and end
// and moves to the next line. But, if both the numbers are non-zero, then, it adds "2"
// the times either one of the number is not equal to zero as we decrease the input by "1" each!!
return 2 + Adder(num1 - 1, num2 - 1);
}
else if (num1 == 0 && num2 != 0) {
// If one of the number is zero, it decreases the other number by 1 and adds "1" on to it!!
return 1 + Adder(0, num2 - 1);
}
else if (num2 == 0 && num2 != 0) {
// If the other number is zero, it decreases the next number by 1 and adds "1" on to it!!
return 1 + Adder(num1 - 1, 0);
}
else {
// If both the number entered by user was "0" then it return "0" as the sum.
return 0;
}
}
int main(void) {
Add a1; // Instance of class Add.
unsigned int addentOne; // Variable to store unsigned int.
unsigned int addentTwo; // Variable to store unsigned int.
// User Input
cout << endl << "Enter the first number: ";
cin >> addentOne;
cout << endl << "Enter the second number: ";
cin >> addentTwo;
// Formatting the output using recursive function created in the class.
cout << endl << "ADDITION USING RECURSION";
cout << endl << "------------------------";
cout << endl << " " << addentOne;
cout << endl << " +" << addentTwo;
cout << endl << " ------";
cout << endl << " " << a1.Adder(addentOne, addentTwo);
// Calling the recursive function Adder of the instance of class Add (a1).
return 0;
}
Comments
Post a Comment