Write a C++ program to accept a 5 digit integer and to validate the input based on the following rules.
Rules
1) The input number is divisible by 2. 2) The sum of the first two digits is less than last two digits. if the input number satisfies all the rules, the system prints valid and invalid, otherwise,
Example 1:
Enter a value: 11222
Output: Input number is valid
Example 2:
Enter a value: 1234
Output: Input number is invalid
#include <iostream>
#include <string>
#include <vector>
bool validate(int number)
{
std::vector<int> dig;
while (number != 0) {
dig.push_back(number % 10);
number /= 10;
}
return dig.size() == 5 and dig[0] + dig[1] > dig[3] + dig[4];
}
int main()
{
int val;
std::cout << "Enter a value: ";
std::cin >> val;
if (validate(val)) {
std::cout << "Input number is valid\n";
} else {
std::cout << "Input number is invalid\n";
}
return 0;
}
Comments
Leave a comment