kirililchev3

Multiply Even by Odds

Jun 1st, 2018
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.31 KB | None | 0 0
  1. using System;
  2.  
  3. namespace P09.Multiply_Evens_by_Odds
  4. {
  5.     class Program
  6.     {
  7.         private static void Main(string[] args)
  8.         {
  9.             int n = Math.Abs(int.Parse(Console.ReadLine()));
  10.             int result = GetMultipleOfEvensAndOdds(n);
  11.             Console.WriteLine(result);
  12.         }
  13.         private static int GetMultipleOfEvensAndOdds (int n)
  14.         {
  15.             int sumEvens = GetSumofEvenDigits(n);
  16.             int sumOdds = GetSumofOddDigits(n);
  17.             return sumEvens * sumOdds;
  18.         }
  19.        
  20.         private static int GetSumofOddDigits (int n)
  21.         {
  22.             int oddSum = 0;
  23.             while (n>0)
  24.             {
  25.                 int lastDigit = n % 10;
  26.  
  27.                 if (lastDigit%2 != 0)
  28.                 {
  29.                     oddSum += lastDigit;
  30.                 }
  31.                 n /= 10;
  32.             }
  33.             return oddSum;
  34.         }
  35.        
  36.         private static int GetSumofEvenDigits (int n)
  37.         {
  38.             int evenSum = 0;
  39.  
  40.             while (n>0)
  41.             {
  42.                 int lastDigit = n % 10;
  43.                 n /= 10;
  44.  
  45.                 if (lastDigit % 2 == 0)
  46.                 {
  47.                     evenSum += lastDigit;
  48.                 }
  49.                
  50.             }
  51.             return evenSum;
  52.         }
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment