I need to make a program that takes a number like 117
and adds the individual digits together (1+1+7) which equals 9
and divides it by 9 to see if the number is divisable by 9
but I don't know how to isolate each digit in order to add them together.
Can anyone help me
Thanks for the solutions guys but now I'm curious... is ther a possible way to divide a number like 117 into 3 digits (1, 1 & 7) and add them together (1+1+7) to get 9? in code that is.
I thought you could use a command like cin.get although cin.get is for use with characters so I guess you can't use that but is there a way to isolate numbers in that manner (setw) or something like that?
What you are trying to have ccould be treated as BCD number operations . I am not sure , but I think there is no such operation in C standard library . But as workaround you can use the sprintf to print hex number to the charater string and each position in this character string will be ASCII coded decimal based initial number (of sprintf output will be decimal set).
SO after that it cacn be easy converted to whatever you need . But sprintf is not as short as this above .
If you are curious enough - some procecssors support BCD commands so using assembly you can directly work with such conversion , but check asm commands first .
Thanks for the solutions guys but now I'm curious... is ther a possible way to divide a number like 117 into 3 digits (1, 1 & 7) and add them together (1+1+7) to get 9? in code that is.
look maybe you have to take the number, and then do like this
N=N/10
and also the digit will be D=N%10
and do a loop while N#0
and then u can have the digits...its so easy
Mostafa