+ 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?
6 Answers
+ 3
Here's a hint:-
12345 % 10 = 5
+ 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.
+ 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
+ 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.
0
Oma Falk what does this code do?
I changed the input several times but it gave irrelevant integer outputs.