Basic arithmetic operations

Program "Basic arithmetic operations" is intended to calculate the four mathematical operations: addition, subtraction, multiplication and division.

As noted during the development of algorithm for the purposes of the program are used:

The variables X, Y and Z for storing values ​​of variables and results of type double (the number that belongs to the set of real numbers, floating point double precision in the calculations).

The variables A and B are the auxiliary variables type of string, used when entering values ​​of variables by users.This is because the program is using functions to verify that the user have entered the number of the set of real numbers, and it is doing on this type of data. Program accepts the value entered by the keyboard as text, and then switch it to the numeric value provided that enter text is number from the set of real numbers.

The variable Answer type of string is used to store the user answers to the question "do you want to start the program again?".

Program compared to the algorithm has an extension in terms of added three mathematical operations what in the essence does not change anything.

/************************************************************************************************

* Program name : Basic arithmetic operations *

* Program ver. : 2.0 *

* Created by : SharpDevelop *

* Code author : Perić Željko *

* Code language : C# *

* Date created : 27.01.2012 *

* Time created : 18:41 *

* *

* *

* Program Description : Simple console application to calculate four simple math operations *

* Addition, subtraction, multiplication and division. *

* *

* *

* Program Accuracy Notice : *

* *

* All calculations in program are executed using double type of variable. *

* By default , a double value contains 15 decimal digits of precision , *

* although a maximum of 17 digits is maintained internally. *

* Result is shown in scientific format , rounded at fourteen decimals. *

* This could cause fatal error because , in some cases for accurate result , *

* many more digits are significant , especially when very large or very small *

* numbers are involved. *

* Usually input values that user type in , have it's own precision error... *

* *

* So the result that this program gives is not always 100 % accurate, but approximate. *

* *

* *

* All the best, *

* Author. *

************************************************************************************************/

using System;

namespace Basic_arithmetic_operations

{

class Program

{

public static void Main(string[] args)

{

//

// declaration of variables

//

string A = " ";

string B = " ";

string Answer = "N";

double X = 0;

double Y = 0;

double Z = 0;

bool Number_Relevance = false;

//

// beginning label ,

// marks the point of return in program

// if the user wants to re-start program

// used for 'goto' command

//

beginning :

//

// Set console dimensions , text color , clear console and write Title

//

Console.SetWindowSize(63,33);

Console.Title = "Basic arithmetic operations";

Console.ForegroundColor = ConsoleColor.Green;

Console.Clear();

Console.WriteLine("***************************************************************");

Console.WriteLine(" Program basic arithmetic operations");

Console.WriteLine("***************************************************************");

//

// Get X value from Console

//

A = " ";

Number_Relevance = false;

Console.WriteLine();

Console.WriteLine("Enter value for X : ");

A = Console.ReadLine();

Number_Relevance = double.TryParse(A,out X);

//

// Check whether the entered number belongs to the set of real numbers

// If not re-request the value of X

//

while (Number_Relevance == false)

{

A = " ";

Number_Relevance = false;

Console.WriteLine("Error , You must enter a number. Please enter value for X : ");

A = Console.ReadLine();

Number_Relevance = double.TryParse(A,out X);

}

//

// Get Y value from Console

//

B = " ";

Number_Relevance = false;

Console.WriteLine();

Console.WriteLine("Enter value for Y: ");

B = Console.ReadLine();

Number_Relevance = double.TryParse(B,out Y);

//

// Check whether the entered number belongs to the set of real numbers

// If not re-request the value of Y

//

while (Number_Relevance == false)

{

B = " ";

Number_Relevance = false;

Console.WriteLine("Error , You must enter a number. Please enter value for Y : ");

B = Console.ReadLine();

Number_Relevance = double.TryParse(B,out Y);

}

//

// Clear console for result output and write Title

//

Console.Clear();

Console.WriteLine("***************************************************************");

Console.WriteLine(" Program basic arithmetic operations");

Console.WriteLine("***************************************************************");

Console.WriteLine();

Console.WriteLine(" X = " + X.ToString() + " Y = " + Y.ToString());

//

// Basic arithmetic operations + , - , * , /

// Calculate and write the result

//

//

// Z = X + Y

//

Z = X + Y;

Console.WriteLine();

Console.WriteLine("***************************************************************");

Console.WriteLine();

Console.WriteLine(" Z = X + Y" + " Z = " + X.ToString() + " + " + Y.ToString() + " Z = " + Z.ToString());

Console.WriteLine();

//

// Z = X - Y

//

Z = X - Y;

Console.WriteLine();

Console.WriteLine("***************************************************************");

Console.WriteLine();

Console.WriteLine(" Z = X - Y" + " Z = " + X.ToString() + " - " + Y.ToString() + " Z = " + Z.ToString());

Console.WriteLine();

//

// Z = X * Y

//

Z = X * Y;

Console.WriteLine();

Console.WriteLine("***************************************************************");

Console.WriteLine();

Console.WriteLine(" Z = X * Y" + " Z = " + X.ToString() + " * " + Y.ToString() + " Z = " + Z.ToString());

//

// Z = X / Y

//

// If Y equals zero , dividing by zero is not possible.

// You may try to skip this question , the result is interesting

// from math aspect , if you have adequate scool you should anderstand.

if ( Y != 0 )

{

Z = X / Y;

Console.WriteLine();

Console.WriteLine("***************************************************************");

Console.WriteLine();

Console.WriteLine(" Z = X / Y" + " Z = " + X.ToString() + " / " + Y.ToString() + " Z = " + Z.ToString());

Console.WriteLine();

Console.WriteLine("***************************************************************");

Console.WriteLine();

}

else

{

Console.WriteLine();

Console.WriteLine("***************************************************************");

Console.WriteLine();

Console.WriteLine(" Z = X / Y" + " Z = ?" + " Dividing by zero is not possible ");

Console.WriteLine();

Console.WriteLine("***************************************************************");

Console.WriteLine();

}

//

// The request for an answer , if user wants to start program again.

//

Answer = " ";

Console.WriteLine("Do you want to start the program again <Y/N>");

Answer = Console.ReadLine(); // get the ansver

Answer = Answer.ToUpper(); // set all the letters inside ansver to Upper case

while(Answer != "Y" && Answer != "N")

{

Answer = " ";

Console.WriteLine("Error, answer could only be Y for Yes or N for No !");

Console.WriteLine("Do you want to start the program again <Y/N>");

Answer = Console.ReadLine();

Answer = Answer.ToUpper();

}

if (Answer == "Y")

{

//

// jump to beginning label

//

goto beginning;

}

Console.WriteLine();

Console.Write("Press any key to continue . . . ");

Console.ReadKey(true);

}

}

}

/************************************************************************

* Program Licence : *

* *

* Copyright 2012 , Perić Željko *

* (periczeljkosmederevo@yahoo.com) *

* *

* According to it's main purpose , this program is licenced *

* under the therms of 'Free Software' licence agreement. *

* *

* If You do not know what those therms applies to *

* please read explanation at the following link : *

* (http://www.gnu.org/philosophy/free-sw.html.en) *

* *

* Since it is Free Software this program has no warranty of any kind. *

************************************************************************

* Ethical Notice : *

* *

* It is not ethical to change program code signed by it's author *

* and then to redistribute it under the same name , especially *

* if it is incorrect. *

* *

* It is recommended that if you make improvement in program code , *

* to make remarks of it and then to sign it by Your's name , *

* for further redistribution as new major version of program. *

* *

* Author name and references of old program code version should be *

* kept , for tracking history of program development. *

* *

* For any further information please contact code author at his email. *

************************************************************************/

/************************************

* List Of Revisions *

************************************

* Major revision of version 1.0 *

* Author 25.11.2013 *

* New comments added *

* Program licence changed *

* New version number 2.0 *

************************************/

Attached at the bottom of the page is the Basic arithmetic operations.zip archive with a complete solution for IDE SharpDevelop C#,

that you can download and within there is a folder with the Release exe file of program. For proper work of program it is neccessary

.Net framework 4.0 that can be found on Microsoft offical web site . There you can find information about what it is, and what it is for.

All the best,

Author