# Answer to Question #60610 in C++ for shruti

Question #60610

write a C++ program to accept a four digit number and print whether it is odd or even number. Write a program to print the digits of the number in reverse order. (e.g 1234 is an even number. The reverse of the numbers 4321).

Expert's answer

#include <iostream>

int reverse (int n) { //function reversing number 'n'

int a1 = n / 1000, a2 = n / 100 % 10, a3 = n / 10 % 10, a4 = n % 10; //first, second, third and fourth digits

int new_n = a4*1000 + a3*100 + a2*10 + a1; //reversing digits

return new_n;

}

int main () {

int number; //number we work with

std::cin >> number; //getting number from user

std::cout << number << " is an " << ((number & 1) ? "odd" : "even") << " number. "; //printing odd or even is 'number'

//with 'number & 1' expression we are checking last bit of 'number'

//if it is 1 we write 'odd', otherwise - 'even'

int reversed_number = reverse (number); //getting reversed 'number'

std::cout << "The reverse of the numbers " << reversed_number << "\n"; //printing reversed 'number'

return 0;

}

int reverse (int n) { //function reversing number 'n'

int a1 = n / 1000, a2 = n / 100 % 10, a3 = n / 10 % 10, a4 = n % 10; //first, second, third and fourth digits

int new_n = a4*1000 + a3*100 + a2*10 + a1; //reversing digits

return new_n;

}

int main () {

int number; //number we work with

std::cin >> number; //getting number from user

std::cout << number << " is an " << ((number & 1) ? "odd" : "even") << " number. "; //printing odd or even is 'number'

//with 'number & 1' expression we are checking last bit of 'number'

//if it is 1 we write 'odd', otherwise - 'even'

int reversed_number = reverse (number); //getting reversed 'number'

std::cout << "The reverse of the numbers " << reversed_number << "\n"; //printing reversed 'number'

return 0;

}

Need a fast expert's response?

Submit orderand get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

## Comments

## Leave a comment