0
Code to split number into digits?
How can I write The condition to split number into digits?
5 Réponses
+ 4
Rem = num%10 will give you last digit. Use a loop to do the same until you get the all digit
+ 9
while (number > 0)
{
int digit = number%10;
number /= 10;
}
Hope it will help
+ 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 🙂
+ 1
But this give me reserve number
+ 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