Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Program
- {
- static void Main(string[] args)
- {
- while (true)
- {
- try
- {
- Console.WriteLine("Введите число");
- int N = Convert.ToInt32(Console.ReadLine());
- Console.WriteLine("Сумма цифр данного числа : "+numbersAmount(N));
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message);
- };
- }
- }
- static int numbersAmount(int N)
- {
- if (N <= 0)
- {
- throw new Exception("Число должно быть натуральным");
- }
- int result = 0;
- while (N > 0)
- {
- int ostatok = N % 10;
- N /= 10;
- result += ostatok;
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement