There is a number. It is necessary with the help of a cycle to go through its numbers and add those that are suitable for the condition. I am familiar with C # recently, I just can’t figure out how to get the digits of a number and then do calculations with them? If possible, without using arrays. If the easiest option associated with arrays - so be it.
- By element, what do you mean? - LazyTechwork
- For example, the number 213. And it is necessary to divide into the numbers 2, 1 and 3. - Ilya
|
3 answers
int n = 210; int s = 0; while(n!=0) { // Здесь поставить условие s = s + n % 10; n = n / 10; } Console.WriteLine(s); - two operations out of three are wrong, bad thing - Igor
- What is the result for
-213? - rdorn
|
You can do this:
var number = 596; var digits = number.ToString().ToCharArray(); var positive = digits.Where(x => x % 2 == 0); var negative = digits.Where(x => x % 2 != 0); |
If you need from any number to get its components (figures), you can do it like this:
var number = 228; int[] digits = number.ToString().Select(c => Convert.ToInt32(c) - 48).ToArray(); //(-48) - смотри ASCII таблицу |