How to create a Calculator that does the Sin, Cos and Tan and normal calculator
using System;
using System.Collections.Generic;
using System.Globalization;
namespace App
{
class Program
{
static void Main(string[] args)
{
char operation;
int number1=0;
int number2=0;
double angle = 0;
double angleRadians=0;
Console.Write("Enter the mathematical operation (+, -, *, /, 's' - Sin,'c' - Cos, 't' - Tan): ");
operation = Console.ReadLine().ToLower()[0];
if (operation == '+' || operation == '-' || operation == '*' || operation == '/')
{
Console.Write("Enter the first number: ");
number1 = int.Parse(Console.ReadLine());
Console.Write("Enter the second number: ");
number2 = int.Parse(Console.ReadLine());
}
else if (operation == 's' || operation == 'c' || operation == 't')
{
Console.Write("Enter the angle: ");
angle = double.Parse(Console.ReadLine());
// converting value to radians
angleRadians = (angle * (Math.PI)) / 180;
}
switch (operation)
{
case '+':
{
int result = number1 + number2;
Console.WriteLine("{0} + {1} = {2}", number1, number2, result);
break;
}
case '-':
{
int result = number1 - number2;
Console.WriteLine("{0} - {1} = {2}", number1, number2, result);
break;
}
case '*':
{
int result = number1 * number2;
Console.WriteLine("{0} * {1} = {2}", number1, number2, result);
break;
}
case '/':
{
if (number2 != 0)
{
double result = (double)number1 / (double)number2;
Console.WriteLine("{0} / {1} = {2}", number1, number2, result);
}
else
{
Console.WriteLine("ERROR: the denominator is zero.");
}
break;
}
case 's':
{
double result = Math.Sin(angleRadians);
Console.WriteLine("Sin({0}) = {1}",angle, result);
break;
}
case 'c':
{
double result = Math.Cos(angleRadians);
Console.WriteLine("Cos({0}) = {1}", angle, result);
break;
}
case 't':
{
double result = Math.Tan(angleRadians);
Console.WriteLine("Tan({0}) = {1}", angle, result);
break;
}
default:
{
Console.WriteLine("Wrong operation.");
break;
}
}
Console.ReadLine();
}
}
}
Comments
Leave a comment