Write a program which takes a number n as input and prints YAYY if the sum of all digits
except the rightmost digit is equal to the rightmost digit., otherwise print OOPS. (10 marks)
For example: If user enters 2237, it will print YAYY because 2+2+3 is equal to 7.
Whereas, if user enters 3425, it will print OOPS because 3+4+2 is not equal to 5.
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Please, enter a number n: ";
cin >> number;
int last = number % 10;
int sum = 0;
number = number / 10;
while (number > 0)
{
sum += number % 10;
number = number / 10;
}
if (sum == last)
{
cout << "YAYY";
}
else
{
cout << "OOPS";
}
}
Comments
Leave a comment