Advertisement
beeble

SumOf3Numbers

Apr 1st, 2014
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.75 KB | None | 0 0
  1. // Problem 01.  Sum of 3 Numbers
  2. //
  3. // Write a program that reads 3 real numbers from the console and prints their sum. Examples:
  4. //
  5. //         a        b       c       sum
  6. //     =====================================
  7. //         3        4       11       18
  8. //         -2       0       3        1
  9. //         5.5      4.5     20.1     30.1
  10.  
  11. using System;
  12.  
  13.     class SumOf3Numbers
  14.     {
  15.         static void Main()
  16.         {
  17.             Console.Title = "01. Sum of 3 Numbers";
  18.             double real;
  19.             double sum = 0;
  20.             int n = 0;           // used as a counter for the message line
  21.             bool check = false;
  22.             double[] numbers = new double [3];
  23.             for (int a = 0; a < 3; a++)             // to execute the program 3 times (3 times different numbers as an input)
  24.             {
  25.                 for (int i = 0; i < 3; i++)        // get 3 real numbers and stores them into numbers[]
  26.                 {
  27.                     while (true)
  28.                     {
  29.                         if (!check)                      // first check for the message on the console
  30.                         {
  31.                             n = n + 1;
  32.                             Console.WriteLine("Please enter a real number for number " + n + ".");
  33.                             if (n == 3)                   // to avoid n getting bigger than 3 in our message line, after successful
  34.                             {                             // completion of the first set of correct input numbers
  35.                                 n = 0;
  36.                             }
  37.                         }
  38.                         string input = Console.ReadLine();
  39.                         check = double.TryParse(input, out real);
  40.                         if (check)                          // second check for the input- to check if it is correct
  41.                         {
  42.                             numbers[i] = real;
  43.                             check = false;
  44.                             break;
  45.                         }
  46.                         else
  47.                         {
  48.                             Console.WriteLine("Not a real number.");
  49.                             check = true;
  50.                         }
  51.                     }
  52.                 }
  53.                 for (int index = (numbers.Length - 1); index >= (numbers.Length - 3); index--)     // always getting the last
  54.                 {                                                                                  // 3 numbers of the list
  55.                     sum = numbers[index] + sum;
  56.                 }
  57.                 Console.Write("\nThe sum is: {0}\n\n", sum);
  58.                 sum = 0;                                                  // for the next set of numbers to sum
  59.             }    
  60.         }
  61.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement