Answer to Question #313546 in C++ for yyyy

Question #313546

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. Without using loops


1
Expert's answer
2022-03-17T17:09:45-0400
#include <iostream>
#include <string>
using namespace std;


int fun(string s) {
    if (s.size() == 1) {
        return -stoi(s);
    }
    return stoi(s.substr(0,1)) + fun(s.substr(1));
}


int main() {
    int n;
    string s;


    cin >> n;
    s = to_string(n);


    if (fun(s) == 0) {
        cout << "YAYY";
    }
    else {
        cout << "OOPS";
    }
    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