Code to split number into digits? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Code to split number into digits?

How can I write The condition to split number into digits?

5th Feb 2022, 6:51 PM
ABUBAKAR ALHOMIDY
ABUBAKAR ALHOMIDY - avatar
5 Answers
+ 4
Rem = num%10 will give you last digit. Use a loop to do the same until you get the all digit
5th Feb 2022, 6:58 PM
Kashyap Kumar
Kashyap Kumar - avatar
+ 9
while (number > 0) { int digit = number%10; number /= 10; } Hope it will help
6th Feb 2022, 12:52 AM
Vaibhav
Vaibhav - avatar
+ 9
*Language : C * #include<stdio.h> int main() { int num; scanf("%d",&num); while(num > 0) //do till num greater than 0 { int mod = num % 10; //split last digit from number printf("%d\n",mod); //print the digit. num = num / 10; //divide num by 10. num /= 10 also a valid one } return 0; } Hope it will help 🙂
6th Feb 2022, 12:54 AM
Vaibhav
Vaibhav - avatar
+ 1
But this give me reserve number
5th Feb 2022, 7:04 PM
ABUBAKAR ALHOMIDY
ABUBAKAR ALHOMIDY - avatar
+ 1
G'day ABUBAKAR ALHOMIDY as Kashyap Kumar ⭕ said, you can get the last digit of a decimal integer with 👉remainder=number%10👈. The next trick is to shift your number to the right. You can do that by 👉number/=10👈 code example: int rem, num=145; while (num>0){ rem+=num%10; rem*=10; num/=10; } (Note: I'm learning C, so perhaps my syntax isn't correct for C++) EDIT: this was wrong.... it didn't advance the remainder, and when it did then the number was be reversed! What about this? https://code.sololearn.com/cDQDmCiWLY44/?ref=app
5th Feb 2022, 8:55 PM
HungryTradie
HungryTradie - avatar