Advertisement
fingli

01. Sum Factorial Even Digits

Jul 1st, 2024
544
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.14 KB | None | 0 0
  1. namespace _01.SumFactorialEvenDigits
  2. {
  3.     internal class Program
  4.     {
  5.         static void Main(string[] args)
  6.         {
  7.             int input = int.Parse(Console.ReadLine());
  8.             int[] digits = IntToArray(input);
  9.             int sum = 0;
  10.  
  11.             for (int i = 0; i < digits.Length; i++)
  12.             {
  13.                 if (digits[i] % 2 == 0)
  14.                 {
  15.                     int factorial = 1;
  16.  
  17.                     for (int j = 1; j <= digits[i]; j++)
  18.                     {
  19.                         factorial *= j;
  20.                     }
  21.                     sum += factorial;
  22.                 }
  23.                 else
  24.                 {
  25.                     continue;
  26.                 }
  27.             }
  28.             Console.WriteLine(sum);
  29.  
  30.  
  31.             static int[] IntToArray(int input)
  32.             {
  33.                 List<int> result = new List<int>();
  34.  
  35.                 while (input > 0)
  36.                 {
  37.                     int digit = input % 10;
  38.                     result.Add(digit);
  39.                     input /= 10;
  40.                 }
  41.                 return result.ToArray();
  42.             }
  43.         }
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement