Answer to Question #259036 in C++ for Charmaine

Question #259036

Write a program to enter a natural number from keyboard

and then print first and last digit of entered number.


1
Expert's answer
2021-10-31T12:09:04-0400
#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;
}

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

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS