Загрузка...

How to find the sum of the digits of a number?

Thread in C# created by dfshsu Oct 23, 2022. 175 views

  1. dfshsu
    dfshsu Topic starter Oct 23, 2022 58 Sep 11, 2021
    Найти сумму цифр числа с помощью рекурсии. 561 = 12

    Не понимаю как можно из числа типа int вытащить одну цифру
     
  2. Возрожденный
     
  3. SSTEALER
    SSTEALER Oct 23, 2022 3 Oct 14, 2022
    int a = int.Parse(Console.ReadLine());
    int s = 0;
    while (a > 0)
    {

    s = s + a % 10;
    a = a /10 ;

    }
    Console.WriteLine(s);
     
Top
Loading...