Answer to Question #313973 in C++ for wali

Question #313973

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.


1
Expert's answer
2022-03-18T12:13:42-0400
#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";
	}
}

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