TheBulgarianWolf

Greater of Two Values(int,char,string)

Oct 30th, 2020
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.25 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3.  
  4. namespace ConsoleApp1
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             Console.Write("Enter the type of the values: ");
  11.             string type = Console.ReadLine();
  12.             switch (type)
  13.             {
  14.                 case "int":
  15.                     Console.Write("Enter the first value: ");
  16.                     int num1 = int.Parse(Console.ReadLine());
  17.                     Console.Write("Enter the second value: ");
  18.                     int num2 = int.Parse(Console.ReadLine());
  19.                     GetMax(num1, num2);
  20.                     break;
  21.                 case "char":
  22.                     Console.Write("Enter the first char: ");
  23.                     string char1 = Console.ReadLine();
  24.                     Console.Write("Enter the second char: ");
  25.                     string char2 = Console.ReadLine();
  26.                     char charOne = char1[0];
  27.                     char charTwo = char2[0];
  28.                     GetMax(charOne, charTwo);
  29.                     break;
  30.                 case "string":
  31.                     Console.Write("Enter the first string: ");
  32.                     string str1 = Console.ReadLine();
  33.                     Console.Write("Enter the second string: ");
  34.                     string str2 = Console.ReadLine();
  35.                     GetMax(str1, str2);
  36.                     break;
  37.                 default:
  38.                     Console.WriteLine("Invalid input type!");
  39.                     break;
  40.             }
  41.         }
  42.  
  43.         static void GetMax(int num1,int num2)
  44.         {
  45.             if(num1 > num2)
  46.             {
  47.                 Console.WriteLine(num1);
  48.             }
  49.             else if(num2 > num1)
  50.             {
  51.                 Console.WriteLine(num2);
  52.             }
  53.             else
  54.             {
  55.                 Console.WriteLine("The numbers are equal!");
  56.             }
  57.         }
  58.  
  59.         static void GetMax(char char1,char char2)
  60.         {
  61.             if(char1 > char2)
  62.             {
  63.                 Console.WriteLine(char1);
  64.             }
  65.             else if(char2 > char1)
  66.             {
  67.                 Console.WriteLine(char2);
  68.             }
  69.             else
  70.             {
  71.                 Console.WriteLine("The chars are the same!");
  72.             }
  73.         }
  74.  
  75.         static void GetMax(string str1,string str2)
  76.         {
  77.             if(str1.Length > str2.Length)
  78.             {
  79.                 Console.WriteLine(str1);
  80.             }
  81.             else if(str2.Length > str1.Length)
  82.             {
  83.                 Console.WriteLine(str2);
  84.             }
  85.             else
  86.             {
  87.                 int count = 0;
  88.                 foreach(char i in str1)
  89.                 {
  90.                    
  91.                     if(i > str2[count])
  92.                     {
  93.                         Console.WriteLine(str1);
  94.                         break;
  95.                     }
  96.                     else if (i < str2[count])
  97.                     {
  98.                         Console.WriteLine(str2);
  99.                         break;
  100.                     }
  101.                     else
  102.                     {
  103.                         count++;
  104.                     }
  105.                    
  106.                 }
  107.             }
  108.         }
  109.  
  110.     }
  111. }
  112.  
Add Comment
Please, Sign In to add comment