JulianJulianov

10.MethodsLab-Multiply Evens by Odds

Feb 15th, 2020
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.34 KB | None | 0 0
  1. 10. Multiply Evens by Odds
  2. Create a program that multiplies the sum of all even digits of a number by the sum of all odd digits of the same number:
  3. • Create a method called GetMultipleOfEvenAndOdds()
  4. • Create a method GetSumOfEvenDigits()
  5. • Create GetSumOfOddDigits()
  6. • You may need to use Math.Abs() for negative numbers
  7. Examples
  8. Input         Output       Comment
  9. -12345        54           Evens: 2 4
  10.                            Odds: 1 3 5
  11.                            Even sum: 6
  12.                            Odd sum: 9
  13.                            6 * 9 = 54
  14.  
  15. using System;
  16. using System.Linq;
  17.  
  18. namespace _10MultiplyEvensByOdds
  19. {
  20.     class Program
  21.     {
  22.         static void Main(string[] args)
  23.         {
  24.             var input = Math.Abs(int.Parse(Console.ReadLine()));
  25.  
  26.           /*  int[] allNumbers = new int[input.Length];
  27.  
  28.             for (int i = 0; i < input.Length; i++)
  29.             {
  30.                 char currentChar = input[i];            // Решението с масив е закоментирано!
  31.  
  32.                 if (char.IsDigit(currentChar))
  33.                 {
  34.                     allNumbers[i] = currentChar - '0';
  35.                 }
  36.             }*/
  37.             int evenSum = GetSumOfEvenDigits(input);
  38.             int oddSum = GetSumOfOddDigits(input);
  39.  
  40.             int result = GetMultipleOfEvenAndOdds(evenSum, oddSum);
  41.             Console.WriteLine(result);
  42.         }
  43.         private static int GetSumOfEvenDigits(int input)
  44.         {
  45.             int evenSum = 0;
  46.             while (input > 0)
  47.             {
  48.                 if (input % 2 == 0)
  49.                 {
  50.                    evenSum += input % 10;
  51.                 }
  52.                 input /= 10;
  53.             }
  54.             return evenSum;
  55.            // return allNumbers.Where(x => x % 2 == 1).Sum();
  56.         }
  57.         private static int GetSumOfOddDigits(int input)
  58.         {
  59.             int oddSum = 0;
  60.             while (input > 0)
  61.             {
  62.                 if (input % 2 != 0)
  63.                 {
  64.                     oddSum += input % 10;
  65.                 }
  66.                 input /= 10;
  67.             }
  68.             return oddSum;
  69.             //return allNumbers.Where(x => x % 2 == 0).Sum();
  70.         }
  71.         private static int GetMultipleOfEvenAndOdds(int evenSum, int oddNSum)
  72.         {
  73.             return evenSum * oddNSum;
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment