Write a program to enter a natural number from keyboard
and then print first and last digit of entered number.
#include <iostream>
int main() {
int n;
std::cin >> n;
if (n > 0) {
std::string s = std::to_string(n);
std::cout << "First digit:" << *s.begin() << std::endl;
std::cout << "Last digit:" << *(s.end() - 1) << std::endl;
} else {
std::cout << "The entered number is not natural" << std::endl;
}
return 0;
}
Comments
Leave a comment