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. 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.
you can only use conditional structures . NO loops.
#include <iostream>
using namespace std;
int SummAcc(int num)
{
static int summ = 0;
if (num > 0)
{
summ += num % 10;
SummAcc(num / 10);
}
return summ;
}
int main()
{
int n;
cout << "Please, enter a number n: ";
cin >> n;
int last = n%10;
int res = SummAcc(n/10);
if (res == last)
cout << "YAYY";
else
cout << "OOPS";
}
Comments
Leave a comment