/****************************************************************************** Problem Description: Create a scientific calculator on Console Application C# containing the following functions using OOP architecture. 1. Addition 2. Subtraction 3. Multiplication 4. Division 5. Square root 6. Cube root 7. Tan 8. Sin 9. Cos 10. Power (exponential) 11. Factorial Instructions: A class named “NumberManipulator” will be created which will contain all the properties and methods. A separate function should cater to the necessities mentioned above. For instance, a method named “CalculateSquareRoot” should be created for calculating the square root of a number. *******************************************************************************/ using System; class NumberManipulator { static void Main(string[] args) { Console.WriteLine("Enter the action to be performed"); Console.WriteLine("Press 1 for Addition"); Console.WriteLine("Press 2 for Subtraction"); Console.WriteLine("Press 3 for Multiplication"); Console.WriteLine("Press 4 for Division"); Console.WriteLine("Press 5 for Square root"); Console.WriteLine("Press 6 for Cube root"); Console.WriteLine("Press 7 for Tan"); Console.WriteLine("Press 8 for Sin"); Console.WriteLine("Press 9 for Cos"); Console.WriteLine("Press 10 for Power (exponential)"); Console.WriteLine("Press 11 for Factorial\n"); int action = Convert.ToInt32(Console.ReadLine()); double result = 0; switch (action){ case 1:{ Console.WriteLine("Enter 1st input"); int input_1 = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter 2nd input"); int input_2 = Convert.ToInt32(Console.ReadLine()); result = Addition(input_1, input_2); break; } case 2:{ Console.WriteLine("Enter 1st input"); int input_1 = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter 2nd input"); int input_2 = Convert.ToInt32(Console.ReadLine()); result = Subtraction(input_1, input_2); break; } case 3:{ Console.WriteLine("Enter 1st input"); int input_1 = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter 2nd input"); int input_2 = Convert.ToInt32(Console.ReadLine()); result = Multiplication(input_1, input_2); break; } case 4:{ Console.WriteLine("Enter 1st input"); int input_1 = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter 2nd input"); int input_2 = Convert.ToInt32(Console.ReadLine()); result = Division(input_1, input_2); break; } case 5:{ Console.WriteLine("Enter input"); double input_1 = Convert.ToDouble(Console.ReadLine()); result = CalculateSquareRoot(input_1); break; } case 6:{ Console.WriteLine("Enter input"); double input_1 = Convert.ToDouble(Console.ReadLine()); result = CalculateCubeRoot(input_1); break; } case 7:{ Console.WriteLine("Enter input"); double input_1 = Convert.ToDouble(Console.ReadLine()); result = CalculateTan(input_1); break; } case 8:{ Console.WriteLine("Enter input"); double input_1 = Convert.ToDouble(Console.ReadLine()); result = CalculateSin(input_1); break; } case 9:{ Console.WriteLine("Enter input"); double input_1 = Convert.ToDouble(Console.ReadLine()); result = CalculateCos(input_1); break; } case 10:{ Console.WriteLine("Enter input"); double input_1 = Convert.ToDouble(Console.ReadLine()); result = CalculatePower(input_1); break; Console.WriteLine("Enter base"); double input_1 = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("Enter power"); double input_2 = Convert.ToDouble(Console.ReadLine()); result = CalculatePower(input_1, input_2); break; } } Console.WriteLine("The result is {0}", result); Console.ReadKey(); } //Addition public static int Addition(int input_1, int input_2){ int result = input_1 + input_2; return result; } //Subtraction public static int Subtraction(int input_1, int input_2){ int result = input_1 - input_2; return result; } //Multiplication public static int Multiplication(int input_1, int input_2){ int result = input_1 * input_2; return result; } //Division public static int Division(int input_1, int input_2){ int result = input_1 / input_2; return result; } //Square Root public static double CalculateSquareRoot(double input_1){ double result = Math.Sqrt(input_1); return result; } //Cube Root public static double CalculateCubeRoot(double input_1){ double result = Math.Ceiling(Math.Pow(input_1, (double) 1 / 3)); return result; } //Tan public static double CalculateTan(double input_1){ double result = Math.Tan(input_1); return result; } //Sin public static double CalculateSin(double input_1){ double result = Math.Sin(input_1); return result; } //Cos public static double CalculateCos(double input_1){ double result = Math.Cos(input_1); return result; } //Power (exponential) public static double CalculatePower(double input_1, double input_2){ double result = Math.Pow(input_1,input_2); return result; } }
Write, Run & Share C# code online using OneCompiler's C# online compiler for free. It's one of the robust, feature-rich online compilers for C# language, running on the latest version 8.0. Getting started with the OneCompiler's C# compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C#
and start coding.
OneCompiler's C# online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
using System;
namespace Sample
{
class Test
{
public static void Main(string[] args)
{
string name;
name = Console.ReadLine();
Console.WriteLine("Hello {0} ", name);
}
}
}
C# is a general purpose object-oriented programming language by Microsoft. Though initially it was developed as part of .net but later it was approved by ECMA and ISO standards.
You can use C# to create variety of applications, like web, windows, mobile, console applications and much more using Visual studio.
Data Type | Description | Range | size |
---|---|---|---|
int | To store integers | -2,147,483,648 to 2,147,483,647 | 4 bytes |
double | to store large floating point numbers with decimals | can store 15 decimal digits | 8 bytes |
float | to store floating point numbers with decimals | can store upto 7 decimal digits | 4 bytes |
char | to store single characters | - | 2 bytes |
string | to stores text | - | 2 bytes per character |
bool | to stores either true or false | - | 1 bit |
datatype variable-name = value;
When ever you want to perform a set of operations based on a condition or set of few conditions IF-ELSE is used.
if(conditional-expression) {
// code
}
else {
// code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression) {
case value1:
// code
break; // optional
case value2:
// code
break; // optional
...
default:
// code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement) {
// code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while(condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Array is a collection of similar data which is stored in continuous memory addresses. Array values can be fetched using index. Index starts from 0 to size-1.
data-type[] array-name;
Method is a set of statements which gets executed only when they are called. Call the method name in the main function to execute the method.
static void method-name()
{
// code to be executed
}