C#: A Simple Calculator

In this C# tutorial we are  "developing a simple calculator" . Here we are taking five variables "n1" for number one ,"n2" for number two, "ch" for choice, "res" of float type for result and "ch1" of char type for choice "Yes" respectively. For this we are using do-while loop here. Now, lets get started!!

using System;
class xyz
{
public static void Main(string[] args)
 {
    int n1,n2,ch;
    double res=0.0;
    char ch1= ' ';
    do{
 
           Console.WriteLine("Please enter your choice ");
           Console.WriteLine(" 1. Add\n 2. Subtract\n 3. Multiply\n 4. Divide ");
           Console.Write("\nYour Choice :  ");
           ch=Convert.ToInt32(Console.ReadLine());
           Console.WriteLine("Please enter two numbers");
           Console.Write("Num1 : ");
           n1=Convert.ToInt32(Console.ReadLine());
           Console.Write("Num2 : ");
           n2=Convert.ToInt32(Console.ReadLine());
           switch(ch)
             {
              case 1:res=n1+n2;
              break;
              case 2:res=n1-n2;
              break;
              case 3:res=n1*n2;
              break;
              case 4:res=n1/n2;
              break;
              default:Console.WriteLine("Wrong Choice!!");
              break;
             }
          Console.WriteLine("Your Result :  "+res);
          Console.WriteLine("Do you want to continue?? Press Y!!");
          ch1=Convert.ToChar(Console.ReadLine());
          }while(ch1=='Y' || ch1=='y');
          Console.WriteLine("Thank You!! for using this app");
 }
}
SHARE

Raunak Hajela

Hi. I’m CEO/Founder of Kodestat. I’m Student, Geek, Web Designer, Developer, Blog Enthusiast and Player. Inspired to make things looks better. I like playing with codes plus I am marvel fan :-P You can check my design portfolio here. For consulation fell free to drop an email at raunakhajela@gmail.com.

  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment