Writing a 4-digit user entry, then printing the ones and tens in C++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Writing a 4-digit user entry, then printing the ones and tens in C++

How can it be written ?

16th Sep 2021, 6:43 PM
Hiba Ali 🌸✨‎
Hiba Ali 🌸✨‎ - avatar
8 Answers
+ 4
Can you please show your attempt? So that we can modify it!
16th Sep 2021, 7:02 PM
Saurabh
Saurabh - avatar
+ 3
Hiba Ali 🌸✨‎ please show us your attempt so we, the community, can help you
16th Sep 2021, 7:17 PM
BroFar
BroFar - avatar
+ 1
I know, to be able to show the ones, you have to divide, see the remainder of the number's division by 10, and the remainder I am not sure of
16th Sep 2021, 7:07 PM
Hiba Ali 🌸✨‎
Hiba Ali 🌸✨‎ - avatar
+ 1
Hiba Ali 🌸✨‎ yeah you are going in correct way your logic is correct try then add your code with your attempt if u are unable to solve self. you have to print 2 numbers once and tens place take two extra variables one and tens and assign those seperated numbers in these variables then print after in next line you can use exit if(count==2) you can set condition like this
16th Sep 2021, 7:19 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 1
During input you can receive the number as a string and then print the string characters one at a time.
16th Sep 2021, 7:20 PM
Brian
Brian - avatar
+ 1
#include <iostream> using namespace std; int main() { int num,t; cout<<"enter a number": ; cin>>num; t=num; t=num%10; t/=10; return 0; } It is not a rule that it be four digits that can be entered by the user with num.
16th Sep 2021, 7:34 PM
Hiba Ali 🌸✨‎
Hiba Ali 🌸✨‎ - avatar
+ 1
Thank u
17th Sep 2021, 6:56 AM
Hiba Ali 🌸✨‎
Hiba Ali 🌸✨‎ - avatar
0
Grab last 2 digits by passing the number as LHS to modulo by 100. To get tens divide the result by 10. To get ones modulo the result by 10 #include <iostream> int main() { int n = 2021; int last2digits = n % 100; // 21 - last 2 digits std::cout << ( last2digits / 10 ) << " " << ( last2digits % 10 ); // '2 1' return 0; }
17th Sep 2021, 12:55 AM
Ipang