Answer to Question #204372 in C++ for nazl

Question #204372

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


1
Expert's answer
2021-06-09T17:40:45-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog