Answer to Question #286160 in C++ for lis

Question #286160

4. Write a C program to check whether the given year is a Lear Year or not. If leaf year print the year in the reverse order otherwise print its sum of cube roots of digits of year.

1
Expert's answer
2022-01-10T00:24:59-0500
#include <iostream>
#include <cmath>
#include <string>
using namespace std;

bool IsLeapYear(int y) {
    return (y%4 == 0 && y%100 != 0) || y%400 == 0;
}

string ReverseDigit(int x) {
    string res;


    if (x == 0) {
        return "0";
    }
    
    while (x > 0) {
        res += to_string(x%10);
        x /= 10;
    }
    return res;
}

double SumOfCubeRoots(int x) {
    double res = 0;

    if (x == 0) {
        return 0;
    }

    while (x > 0) {
        res += pow(x%10, 1./3.);
        x /= 10;
    }
    return res;
}

int main() {
    int year;
    cin >> year;

    if (IsLeapYear(year)) {
        cout << ReverseDigit(year) << endl;
    }
    else {
        cout << SumOfCubeRoots(year) << endl;
    }
    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