Write a C# Sharp program to check whether a given number (integer) is Oddish or Evenish

  • برمجة سي شارب
  • برمجة

Write a C# Sharp program to check whether a given number (integer) is Oddish or Evenish. 
A number is called "Oddish" if the sum of all of its digits is odd, and a number is called "Evenish" if the sum of all of its digits is even.
Example:
120 -> Oddish
321 -> Evenish
43 -> Oddish
4433 -> Evenish
373 -> Oddish.
Expected Output:
Original number: 120
Check the said number is Oddish or Evenish! Oddish
Original number: 321
Check the said number is Oddish or Evenish! Evenish
Original number: 43
Check the said number is Oddish or Evenish! Oddish
Original number: 4433
Check the said number is Oddish or Evenish! Evenish

الأجوبة

using System;
using System.Linq;
namespace exercises
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = 120;
            Console.WriteLine("Original number: " + n);
            Console.WriteLine("Check the said number is Oddish or Evenish! " + test(n));
            n = 321;
            Console.WriteLine("\nOriginal number: " + n);
            Console.WriteLine("Check the said number is Oddish or Evenish! " + test(n));
            n = 43;
            Console.WriteLine("\nOriginal number: " + n);
            Console.WriteLine("Check the said number is Oddish or Evenish! " + test(n));
            n = 4433;
            Console.WriteLine("\nOriginal number: " + n);
            Console.WriteLine("Check the said number is Oddish or Evenish! " + test(n));
        }
        public static string test(int n)
        {
            string str = n.ToString();
            int total = 0;
            for (int i = 0; i < str.Length; i++)
                total += int.Parse(str[i].ToString());
            return (total % 2 == 0 ? "Evenish" : "Oddish");
        }
    }
}
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...