ТЗ: Найти сумму всех нечетных цифр четырехзначного числа, заданного пользователем. Проблема в том, что не знаю как именно просуммировать нечётные числа, поделил число на цифры, записал условие, а дальше не могу сделать ничего using System; namespace vtoroe_zadanie { class Program { static void Main(string[] args) { Console.Title = "Практическая работа №11, задание №2"; Console.Write("Введите четырехзначное число: "); int N = Convert.ToInt32(Console.ReadLine()); int A = N / 1000; int B = (N % 1000) / 100; int C = (N % 100) / 10; int D = N % 10; { if (N>999 && N<10000) { if (A % 2 != 0 || B % 2 != 0 || C % 2 != 0 || D % 2 != 0) Console.Write($"Сумма: {A+B+C+D} "); } } } } } CSHARP using System; namespace vtoroe_zadanie { class Program { static void Main(string[] args) { Console.Title = "Практическая работа №11, задание №2"; Console.Write("Введите четырехзначное число: "); int N = Convert.ToInt32(Console.ReadLine()); int A = N / 1000; int B = (N % 1000) / 100; int C = (N % 100) / 10; int D = N % 10; { if (N>999 && N<10000) { if (A % 2 != 0 || B % 2 != 0 || C % 2 != 0 || D % 2 != 0) Console.Write($"Сумма: {A+B+C+D} "); } } } } }
Подели, а дальше циклом пройдись по ним. Может, в массив записать, а дальше циклом с условием (деление на 2) суммируй в переменную для суммы
public static class Program { public static void Main() { int N = int.Parse(Console.ReadLine()); int sum = 0; while (N > 0) { sum += ((N % 10) % 2) == 1? N % 10 : 0; N /= 10 ; } Console.WriteLine(sum); } } CSHARP public static class Program { public static void Main() { int N = int.Parse(Console.ReadLine()); int sum = 0; while (N > 0) { sum += ((N % 10) % 2) == 1? N % 10 : 0; N /= 10 ; } Console.WriteLine(sum); } }
Если подправить твоё решение то так: using System; namespace vtoroe_zadanie { class Program { static void Main(string[] args) { Console.Title = "Практическая работа №11, задание №2"; Console.Write("Введите четырехзначное число: "); int N = Convert.ToInt32(Console.ReadLine()); int A = N / 1000; int B = (N % 1000) / 100; int C = (N % 100) / 10; int D = N % 10; { if (N>999 && N<10000) { int S = 0; if (A % 2 != 0) S += A; if (B % 2 != 0) S += B; if (C % 2 != 0) S += C; if (D % 2 != 0) S += D; Console.Write($"Сумма: {S} "); } } } } } CSHARP using System; namespace vtoroe_zadanie { class Program { static void Main(string[] args) { Console.Title = "Практическая работа №11, задание №2"; Console.Write("Введите четырехзначное число: "); int N = Convert.ToInt32(Console.ReadLine()); int A = N / 1000; int B = (N % 1000) / 100; int C = (N % 100) / 10; int D = N % 10; { if (N>999 && N<10000) { int S = 0; if (A % 2 != 0) S += A; if (B % 2 != 0) S += B; if (C % 2 != 0) S += C; if (D % 2 != 0) S += D; Console.Write($"Сумма: {S} "); } } } } } но лучше так: using System; namespace vtoroe_zadanie { class Program { static void Main(string[] args) { Console.Title = "Практическая работа №11, задание №2"; Console.Write("Введите четырехзначное число: "); int N = Convert.ToInt32(Console.ReadLine()); if (N>999 && N<10000) { int S = 0; while (N > 0) { int R = N % 10; if (R % 2 != 0) S += R; N /= 10; } Console.Write($"Сумма: {S} "); } } } } CSHARP using System; namespace vtoroe_zadanie { class Program { static void Main(string[] args) { Console.Title = "Практическая работа №11, задание №2"; Console.Write("Введите четырехзначное число: "); int N = Convert.ToInt32(Console.ReadLine()); if (N>999 && N<10000) { int S = 0; while (N > 0) { int R = N % 10; if (R % 2 != 0) S += R; N /= 10; } Console.Write($"Сумма: {S} "); } } } }
Ты тюбик, потому что цикл должен когда-то закончится (тогда когда больше не останется цифр в числе, то есть число будет равно нулю)
using System; namespace vtoroe_zadanie { class Program { static void Main(string[] args) { Console.Title = "Практическая работа №11, задание №2"; Console.Write("Введите четырехзначное число: "); string N = Console.ReadLine(); int sum = 0; foreach (char c in N) { int x = Convert.ToInt32(c.ToString()); if (x % 2 == 1) { sum += x; } } Console.Write($"Сумма: {sum} "); } } } CSHARP using System; namespace vtoroe_zadanie { class Program { static void Main(string[] args) { Console.Title = "Практическая работа №11, задание №2"; Console.Write("Введите четырехзначное число: "); string N = Console.ReadLine(); int sum = 0; foreach (char c in N) { int x = Convert.ToInt32(c.ToString()); if (x % 2 == 1) { sum += x; } } Console.Write($"Сумма: {sum} "); } } }