Advertisement
Guest User

The Sum.

a guest
Oct 23rd, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApp3
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             bool inputError = true; // This is used to continue the loop if the user inputs something other than Y or N.
  14.             char userContinue; // This is for checking whether the user wishes the program to continue calculating numbers.
  15.             bool userChoice = true; // this is used to continue the loop of calculating numbers.
  16.             int numOne; // this is the number the user inputs.
  17.             int sum; // this is used to calculate the sum of numbers from 0 to the input.
  18.             while (userChoice == true)
  19.             {
  20.                 Console.WriteLine("Please enter a whole number: ");
  21.                 numOne = Convert.ToInt32(Console.ReadLine());
  22.                 sum = 0;
  23.  
  24.                 if (numOne > 0)
  25.                 {
  26.                     for (int i = numOne; i > 0; i--)
  27.                     {
  28.                         sum = sum + i;
  29.                     }
  30.                 }
  31.  
  32.                 else if (numOne < 0)
  33.                 {
  34.                     for (int i = numOne; i < 0; i++)
  35.                     {
  36.                         sum = sum + i;
  37.                     }
  38.                 }
  39.  
  40.                 else
  41.                 {
  42.                     Console.WriteLine("The sum is 0.");
  43.                 }
  44.  
  45.                 Console.WriteLine("The sum of all the numbers up to " + numOne + " from 0 is: " + sum);
  46.                 Console.WriteLine("Do you wish to try again? Choose Y or N.");
  47.                 inputError = true;
  48.  
  49.                 while (inputError == true)
  50.                 {
  51.                     userContinue = Console.ReadKey().KeyChar;
  52.                     if (userContinue.Equals('Y'))
  53.                     {
  54.                         userChoice = true;
  55.                         inputError = false;
  56.                     }
  57.  
  58.                     else if (userContinue.Equals('N'))
  59.                     {
  60.                         userChoice = false;
  61.                         inputError = false;
  62.                     }
  63.  
  64.                     else
  65.                     {
  66.                         Console.WriteLine("Not a valid input, try again.");
  67.                         inputError = true;
  68.                     }
  69.                 }
  70.                
  71.             }
  72.             Console.ReadKey();
  73.         }
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement