Help me with this plz, it's in C# 👇🏻👇🏻👇🏻 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Help me with this plz, it's in C# 👇🏻👇🏻👇🏻

I want to receive user input as an ineger and check all the digits one by one to see if they're odd or even. I need to know just one thing: How can i divide a digit from an integer and evaluate it separately?

15th Jul 2020, 3:41 PM
Mosi B.
Mosi B. - avatar
6 Answers
15th Jul 2020, 3:49 PM
Oma Falk
Oma Falk - avatar
+ 3
Here's a hint:- 12345 % 10 = 5
15th Jul 2020, 3:49 PM
Arsenic
Arsenic - avatar
+ 3
Assuming you have am integer number (let's say <n>), you can use the % (modulo operator) to obtain the last digit. Once you got the digit, you then divide <n> by 10 to remove the last digit that had been obtained. The combination of use of modulo and division is to be repeated until <n> becomes zero. Note that such approach will extract the digits backwards, from the last digit to the first. You can also convert the number to string, and check each character, whether they are odd or even. This approach allows you freedom to choose from which point the evaluation shall begin, backwards or forwards. But there is little performance overhead.
15th Jul 2020, 3:59 PM
Ipang
+ 3
Convert the integer to a string, so that you can deal with each character separately. Next, convert each char to an integer, so that you can check if it's odd or even. if Convert.ToInt32(str)%2 ==0 // its even
15th Jul 2020, 4:01 PM
Christopher Leon
+ 2
Mosi B. It calculates th sum of all digits. He gets the last digit by using module operator. It is almost what u need.
15th Jul 2020, 8:17 PM
Oma Falk
Oma Falk - avatar
0
Oma Falk what does this code do? I changed the input several times but it gave irrelevant integer outputs.
15th Jul 2020, 8:15 PM
Mosi B.
Mosi B. - avatar