Advertisement
Fanta-MindTerror

numbersAmount

Dec 24th, 2019
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.99 KB | None | 0 0
  1. class Program
  2.     {
  3.         static void Main(string[] args)
  4.         {
  5.             while (true)
  6.             {
  7.                 try
  8.                 {
  9.                     Console.WriteLine("Введите число");
  10.                     int N = Convert.ToInt32(Console.ReadLine());
  11.                     Console.WriteLine("Сумма цифр данного числа : "+numbersAmount(N));
  12.                 }
  13.                 catch (Exception e)
  14.                 {
  15.                     Console.WriteLine(e.Message);
  16.                 };
  17.             }
  18.         }
  19.  
  20.         static int numbersAmount(int N)
  21.         {
  22.             if (N <= 0)
  23.             {
  24.                 throw new Exception("Число должно быть натуральным");
  25.             }
  26.  
  27.             int result = 0;
  28.  
  29.             while (N > 0)
  30.             {
  31.                 int ostatok = N % 10;
  32.                 N /= 10;
  33.                 result += ostatok;
  34.             }
  35.             return result;
  36.         }
  37.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement