Question

Write a C# console application that has one method to perform the four basic arithmetic operations...

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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 :)

Add a comment
Know the answer?
Add Answer to:
Write a C# console application that has one method to perform the four basic arithmetic operations...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT