Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApp3
- {
- class Program
- {
- static void Main(string[] args)
- {
- bool inputError = true; // This is used to continue the loop if the user inputs something other than Y or N.
- char userContinue; // This is for checking whether the user wishes the program to continue calculating numbers.
- bool userChoice = true; // this is used to continue the loop of calculating numbers.
- int numOne; // this is the number the user inputs.
- int sum; // this is used to calculate the sum of numbers from 0 to the input.
- while (userChoice == true)
- {
- Console.WriteLine("Please enter a whole number: ");
- numOne = Convert.ToInt32(Console.ReadLine());
- sum = 0;
- if (numOne > 0)
- {
- for (int i = numOne; i > 0; i--)
- {
- sum = sum + i;
- }
- }
- else if (numOne < 0)
- {
- for (int i = numOne; i < 0; i++)
- {
- sum = sum + i;
- }
- }
- else
- {
- Console.WriteLine("The sum is 0.");
- }
- Console.WriteLine("The sum of all the numbers up to " + numOne + " from 0 is: " + sum);
- Console.WriteLine("Do you wish to try again? Choose Y or N.");
- inputError = true;
- while (inputError == true)
- {
- userContinue = Console.ReadKey().KeyChar;
- if (userContinue.Equals('Y'))
- {
- userChoice = true;
- inputError = false;
- }
- else if (userContinue.Equals('N'))
- {
- userChoice = false;
- inputError = false;
- }
- else
- {
- Console.WriteLine("Not a valid input, try again.");
- inputError = true;
- }
- }
- }
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement