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.
#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;
}
Comments
Leave a comment