Advertisement
Martichka

[C#] Methods/ Task 13 - Many In One

Jan 23rd, 2013
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.77 KB | None | 0 0
  1. using System;
  2. class ManyInOne
  3. {
  4.     //Reverse Method
  5.     static string Reverse(decimal number)
  6.     {
  7.         string newNum = "";
  8.         string temp = number.ToString();
  9.         for (int i = temp.Length - 1; i >= 0; i--)
  10.         {
  11.             newNum += temp[i] + "";
  12.         }
  13.         return newNum;
  14.     }
  15.  
  16.     //Calculates Average
  17.     static decimal AverageCalculates(decimal[] arr)
  18.     {
  19.         decimal sum = 0m;
  20.         decimal average = 0m;
  21.         foreach (int number in arr)
  22.         {
  23.             sum += number;
  24.         }
  25.          average += (sum / arr.Length);
  26.         return average;
  27.     }
  28.  
  29.     //Calculatesa linear equation
  30.     static float LinearEquation(float a, float b)
  31.     {
  32.         float linear = 0f;
  33.         if (a == 0)
  34.         {
  35.             Console.WriteLine("error");
  36.         }
  37.         if (a != 0)
  38.         {
  39.             linear = (-b) / a;
  40.         }
  41.         return linear;
  42.     }
  43.  
  44.     //Filling the array
  45.     static void FillTheArray(decimal[] arr)
  46.     {
  47.         for (int i = 0; i < arr.Length; i++)
  48.         {
  49.             arr[i] = int.Parse(Console.ReadLine());
  50.         }
  51.     }
  52.  
  53.     static void Main()
  54.     {
  55.         Console.WriteLine("What would you like to do:");
  56.         Console.WriteLine("1. Reverse digits of a number.");
  57.         Console.WriteLine("2. Calculates the average of a sequence of integers.");
  58.         Console.WriteLine("3. Solves a linear equation a * x + b = 0.");
  59.         Console.Write("Enter the number of task you chose: ");
  60.         byte choose = byte.Parse(Console.ReadLine());
  61.        
  62.         switch(choose)
  63.         {
  64.             case 1:
  65.                 Console.WriteLine("Enter a number to reverse it's digits:");
  66.                 decimal number = decimal.Parse(Console.ReadLine());
  67.                 string reverseNum = Reverse(number);
  68.                 Console.WriteLine(reverseNum);
  69.                 break;
  70.             case 2:
  71.                 Console.WriteLine("How many numbers you want to be in your sequence: ");
  72.                 int count = int.Parse(Console.ReadLine());
  73.                 decimal[] sequence = new decimal[count];
  74.                 FillTheArray(sequence);
  75.                 decimal average = AverageCalculates(sequence);
  76.                 Console.WriteLine("The average of your numbers is {0}", average);
  77.                 break;
  78.             case 3:
  79.                 Console.WriteLine("Enter two numbers:");
  80.                 float a = float.Parse(Console.ReadLine());
  81.                 float b = float.Parse(Console.ReadLine());
  82.                 float result = LinearEquation(a, b);
  83.                 Console.WriteLine("The result is {0}", result);
  84.                 break;
  85.             default:
  86.                 Console.WriteLine("Wrong Enter!");
  87.                 break;
  88.         }
  89.         Main();
  90.    
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement