(Digits of an Integer) Write a program that inputs a five-digit integer, separates the integer into its digits and prints them separated by three spaces each. [Hint: Use the integer division and modulus operators.] For example, if the user types in 42339, the program should print:
4 2 3 3 9
Solution:
4 2 3 3 9
Solution:
#include <iostream> using std::cin; using std::cout; using std::endl; int main() { int fivedigit; int first, second, third, fourth, fifth; cout << "Please enter the five digit integer: "; cin >> fivedigit; if(fivedigit < 10000) { cout << "Incorrect digit! Try again!"; } if(fivedigit > 99999) { cout << "Incorrect digit! Try again!"; } first = fivedigit/10000; second = (fivedigit/1000)%10; third = (fivedigit/100)%10; fourth = (fivedigit/10)%10; fifth = fivedigit%10; cout << first; cout << " " << second; cout << " " << third; cout << " " << fourth; cout << " " << fifth; return 0; }
Post A Comment:
0 comments: