Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 10. Multiply Evens by Odds
- 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:
- • Create a method called GetMultipleOfEvenAndOdds()
- • Create a method GetSumOfEvenDigits()
- • Create GetSumOfOddDigits()
- • You may need to use Math.Abs() for negative numbers
- Examples
- Input Output Comment
- -12345 54 Evens: 2 4
- Odds: 1 3 5
- Even sum: 6
- Odd sum: 9
- 6 * 9 = 54
- using System;
- using System.Linq;
- namespace _10MultiplyEvensByOdds
- {
- class Program
- {
- static void Main(string[] args)
- {
- var input = Math.Abs(int.Parse(Console.ReadLine()));
- /* int[] allNumbers = new int[input.Length];
- for (int i = 0; i < input.Length; i++)
- {
- char currentChar = input[i]; // Решението с масив е закоментирано!
- if (char.IsDigit(currentChar))
- {
- allNumbers[i] = currentChar - '0';
- }
- }*/
- int evenSum = GetSumOfEvenDigits(input);
- int oddSum = GetSumOfOddDigits(input);
- int result = GetMultipleOfEvenAndOdds(evenSum, oddSum);
- Console.WriteLine(result);
- }
- private static int GetSumOfEvenDigits(int input)
- {
- int evenSum = 0;
- while (input > 0)
- {
- if (input % 2 == 0)
- {
- evenSum += input % 10;
- }
- input /= 10;
- }
- return evenSum;
- // return allNumbers.Where(x => x % 2 == 1).Sum();
- }
- private static int GetSumOfOddDigits(int input)
- {
- int oddSum = 0;
- while (input > 0)
- {
- if (input % 2 != 0)
- {
- oddSum += input % 10;
- }
- input /= 10;
- }
- return oddSum;
- //return allNumbers.Where(x => x % 2 == 0).Sum();
- }
- private static int GetMultipleOfEvenAndOdds(int evenSum, int oddNSum)
- {
- return evenSum * oddNSum;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment