Write a C# console application that has one method to perform the four basic arithmetic operations of add, subtract, multiply, and divide. In the main program, first print a line on the console showing your name. Call the method you created above with each of the four arithmetic operations. You have to pass the method two integer numbers as well as a flag for which operation you want the method to perform. The method performs the operation on the numbers you pass from the main program and prints out the result on to the console. You method must print nice messages so the user understands what the result is all about. For example, when asked to add two numbers 6 and 6, the method might print on the console: "The sum of 6 and 6 is: 12" Also, fix the following problems: a. The whole application terminates with error if the main program asks the method to divide by zero b. When the main program passes some unknown flag for the operation
This is a very interesting Console application to be developed in C#. For simplicity purposes, I had divided the solution into parts. So, let's start:
PART ONE: REQUIREMENT ANALYSIS:
According to the problem statement, the following points need to be fulfilled:
* Create a console application using C#.
* This console application should take two user inputs and a flag input.
* The flag input is used to find the type of operation going to be performed.
* Based on these 3 inputs, write a method that will perform the operations and return the values.
So, now it is pretty clear what things need Co to be done.
PART TWO: APPROACH
Here are the details of step by step approach with the code analysis:
STEP 1: Take 2 input from the user after showing the Welcome message.
STEP 2: Validate whether these inputs belong to the proper format or not.
STEP 3: If validation successful, Show the operations menu and take FLAG as input.
STEP 4: Validate the Format of FLAG. If successful, Call our defined method to execute the operation.
STEP 5: Perform operation based on the FLAG variable and handle the case for DivideByZero Exception explicitly.
STEP 6: Return the result and return NaN(Not a number) when DivideByZero Case occurs.
PART THREE: FULL CODE IMPLEMENTATION
Few points to be noted before jumping to code:
* Please read the comments at all the important sections.
* Please understand the code flow from the main method and don't directly jump on any outside method.
* I will attach the Input/Output snapshots below.
***********************************CODE*****************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SimpleCalc
{
class Program
{
/// <summary>
/// This method is used to perform basic calci operations.
/// </summary>
/// <param name="a"> Parameter 1 </param>
/// <param name="b"> Parameter 2 </param>
/// <param name="flag"> Flag to identify the type of
operation</param>
/// <returns></returns>
static double BasicCalciOperation(double a,double b,int flag)
{
//Case Handling
switch(flag)
{
//Addition
case 1: return (a + b);
//Subtraction
case 2: return (a - b);
//Multiplication
case 3: return (a * b);
//Division
case 4:
{
//Handling Divide by Zero case
if (b == 0)
Console.WriteLine("Division By Zero is not possible.");
else
return (a / b);
break;
}
default:break;
}
//When the divide by zero case exists, then only this will be
returned
return double.NaN;
}
static void Main(string[] args)
{
//Double Values to store the inputs
double a, b;
//Flag to select the type of operation
int flag;
//Welcome message
Console.WriteLine("====================Hello User, Welcome to Basic
Calci====================");
Console.WriteLine("====================Enter two numbers to perform
basic Calci Operations==================");
//Exception Handling
try
{
//For multiple iterations in a single launch
while (true)
{
//Input 1
Console.WriteLine("Enter number 1: ");
a = Convert.ToInt32(Console.ReadLine());
//Input 2
Console.WriteLine("Enter number 2: ");
b = Convert.ToInt32(Console.ReadLine());
//Welcome Message
Console.WriteLine("====================Basic Calci Operation ->
Num_1 # Num_2==================");
Console.WriteLine("1.Add\n2.Subtract\n3.Multiply\n4.Divide");
Console.WriteLine("Please select your choice ?");
//Input Flag
flag = Convert.ToInt32(Console.ReadLine());
//Calling our method
Console.WriteLine("The output of the Basic Calci Operation is " +
BasicCalciOperation(a, b, flag));
//Continue or Exit
Console.WriteLine("Do you want to continue? Y or N\n");
char ch = Console.ReadKey().KeyChar;
if (ch == 'y' || ch == 'Y')
continue; //Continue the program again
else
break; //Exit the program
}
}
catch (FormatException ex)
{
//Handling Format exception when Format of input is incorrect
Console.WriteLine("Please enter only integers\n" +
ex.Message);
Console.ReadKey();
}
}
}
}
***********************************CODE*****************************************
PART FOUR: INPUT/OUTPUT:
Input/Output:
DivideByZero Case:
I hope it will help you.
Please don't forget to thumbs up if it helped you.
Keep learning and keep HomeworkLibing :)
All the best and happy coding :)
Write a C# console application that has one method to perform the four basic arithmetic operations...
1.Write a Python program to perform simple arithmetic operations (add, subtract, multiply and divide) on 2 numbers and display the output on console ( use methods in your program) Range of numbers : 1 to 100 (Do not use 0)
Could you code this question on Matlab? :)
Question: Design a GUI based application, named Calculator, which perform basic arithmetic operations ie., add, subtract, multiply, and divide Your design must have 1) Two separate input fields for reading the two operands (let's say a and b) 2) 4 Four button (labeled as +, -, *, /) to let the user select an operation. 3) An output field for displaying the result Following table shows operations performed on pushing each of...
c++
Q.2. a) Create a C++ class template called MathCalc, to perform arithmetic operations between two numbers. These arithmetic operations are addition, subtraction, multiplication and division. The MathCalc class template accepts two type parameters, which can be defined as: template <class T, class U>. This allows MathCalc to carry out arithmetic operations between two different data types. MathCalc has the following public methods, described here in pseudocode: i. Ret Sum(augend, addend); Ret is the return value of the method. ii....
Question 20: Write a python program that will perform the operations of simple calculator. The operations are : Addition, subtraction, Multiplication and division. Sample output : Select operation. 1.Add 2.Subtract 3.Multiply 4.Divide Enter choice(1/2/3/4): 3 Enter first number: 15 Enter second number: 14 15 * 14 = 210
06) Write a C program to perform matrix operations based of threads. The program accepts from the user a positive integer N. A menu is given to the user: 1. generate matrices: generates three NxN matrices A, B, C with random integer numbers between 0 and 9 2. add: matrix D- A+B+C 3. subtract: matrix D A-B-C 4. display matrices: A, B, C, D 5. display count of result values more than 8. 6. exit: terminate the program When the...
1. Prompt the user for one of the arithmetic operators ('op'): +, -, *,/, or ** 2. Prompt the user for two input integers ('a' and'b') 3. The input numbers can be positive or negative 4. Perform the arithmetic operation using the two input numbers 5. The first entered number ('a') is the left side, the second ('b') the right side of the operation For exponentiation, the first number is the base, the second the exponent Print (display) the following...
Problem 3 (35) Design a calculator that performs four operations: addition, multiplication, division and subtraction with 2 integer type numbers a) Ask user what type of operation to perform (+, , * or/) a. If the user inputs 'none' then the program terminates. Otherwise it will keep continuing the calculation. (hint: use while) b) Ask user to provide 2 integer number inputs c) Perform operation (whatever operation they mentioned in a) d) Print result e) In division operation, perform a...
Create a procedure driven program for performing arithmetic with fractions. Using a switch statement in main to perform the various actions. Similar to this; Menu a) Load/Reload first fraction b) Load/Reload second fraction c) Add two fractions and display answer d) Subtract two fractions and display answer e) Multiply two fractions and display answer f) Divide two fractions and display answer g) Exit Provide functions that allow for The reloading of the fractions. The addition of two rational numbers. The...
PROBLEM STATEMENT The mini-calculator will use a small ALU to perform arithmetic operations on two 4-bit values which are set using switches. The ALU operations described below are implemented with an Adder/Subtractor component. A pushbutton input allows the current arithmetic result to be saved. An upgraded mini-calculator allows the saved value to be used in place of B as one of the operands. The small ALU that you will design will use the 4-bit adder myadder4 to do several possible...
Write a c++ program: Many mathematical problems require the addition, subtraction, and multiplication of two matrices. Write an ADT Matrix. You may use the following class definition: const int MAX_ROWS = 10; const int MAX_COLS = 10; class MatrixType { public: MatrixType(); void MakeEmpty(); void SetSize(int rowsSize, int colSize); void StoreItem(int item, int row, int col); void Add(MatrixType otherOperand, MatrixType& result); void Sub(MatrixType otherOperand, MatrixType& result); void Mult(MatrixType otherOperand, MatrixType& result); void Print(ofstream& outfile); bool AddSubCompatible(MatrixType otherOperand); bool MultCompatible(MatrixType otherOperand);...