Write a C++ program using the concept of OOP to find the greater number between three numbers
Algorithm:
- Create a GreaterNumber class with private variables num1, num2, num3 to store the three numbers.
- Define a public method findGreater() in the class to find the greater number between the three stored numbers.
- In the main() function, prompt the user to enter three numbers.
- Create a GreaterNumber object with the three numbers entered by the user.
- Call the findGreater() method of the GreaterNumber object to find the greater number.
- Print the greater number.
- End the program.
C++ Program:
#include <iostream>
class GreaterNumber {
private:
int num1, num2, num3;
public:
GreaterNumber(int n1, int n2, int n3) {
num1 = n1;
num2 = n2;
num3 = n3;
}
int findGreater() {
int greater = num1;
if (num2 > greater) greater = num2;
if (num3 > greater) greater = num3;
return greater;
}
};
int main() {
int n1, n2, n3;
std::cout << "Enter first number: ";
std::cin >> n1;
std::cout << "Enter second number: ";
std::cin >> n2;
std::cout << "Enter third number: ";
std::cin >> n3;
GreaterNumber g(n1, n2, n3);
std::cout << "Greater Number: " << g.findGreater() << std::endl;
return 0;
}
#OOP
#HappyProgramming
#HappyCoding
No comments:
Post a Comment