Файлы. Найти сумму всех цифр числа - C#
Формулировка задачи:
Ввести в файл число. Считать его и найти сумму всех цифр этого числа.
Решение задачи: «Файлы. Найти сумму всех цифр числа»
textual
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- namespace TestSharp
- {
- class Program
- {
- static void Main(string[] args)
- {
- // string []arr = File.ReadAllLines("e:\\123.txt");
- int[] arr = System.IO.File.ReadAllText("e:\\123.txt").Split(' ').Select(n => int.Parse(n)).ToArray();
- int result = 0;
- for (int i = 0;i<arr.Length;i++)
- {
- int buffer = arr[i];
- while(buffer !=0)
- {
- result += buffer % 10;
- buffer /= 10;
- }
- }
- Console.WriteLine(result);
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д