Saturday, 8 June 2013

 Write a Program to make Simple Calculator in C,C# and JAVA

Calculator is the simple program in the language. The calculator is the most basic program. Here this tutorial is for the make simple calculator in the C language# and JAVA language. This code is prepared for the beginner this code have not fully efficiency to execute faster and need so low space and much more functionality.

Logic as C# syntax:

Here for accepting value we use console.readline ().which is accept value and convert any value into the string and put into the variable. Here one problem is created by console.readline () which is the function console.Readline () is convert out double into the string and our variable is in the double so we need to convert that value into the double. So here we put one another command which is Convert.ToDouble (); which is used for convert the any value into the double in this simple calculator..
    For making calculator we need two variable which accept the double or float value. Here we take two double value a and b and the one string variable which accept +,-,* and / function so here we take one string variable choice which accept +,-,* and / function.
Here we take the if statement for choose only the +,-,*and / value if that not then go to the label rechoice which is upper on the choice value.
Now for the selecting the +,-,*and / function we take the switch case function which is work as the giving selected function by user. In to the switch case function we put the choice variable for the select the user want to + the value or – or * or /. As the user’s choice the switch is execute the case if user enter choice + then switch is execute the + case in the plus case we put the a + b so answer is show as user’s wish plus the two value. Same as the plus we put all the case for minus ( - ) , for multiplication ( * ) and for divide ( / ) and the result will be show as use of the Console.Writeline() function
.
At last we used the Console.Read() Because if you run the program so the output screen is appear and it disappear immediately so for the see the output  here we used Console.Read(). Console.Read() is wait for the accept any value so the output screen is stay as it is and we will see the our result.
Here the fully loaded code is given so enjoy the simple code for the simple calculator.

Simple Calculator program in C#:

This code is prepared as the upper given logic it is very easy to understand for the C#’s beginner those who are looking for the code to create a simple calculator in c# language..

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HHH_Workstation
{
    class calc
    {
        static void Main(string[] args)
        {
       
            String choice = "";
            Double a, b;
           
            Console.WriteLine("Enter first no:");
            a = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Enter second no:");
            b = Convert.ToDouble(Console.ReadLine());
            rechoice:
       
            Console.WriteLine("Enter choice like +,-,* or /");
            choice = Console.ReadLine();

            switch (choice)
            {
                case "+": Console.WriteLine(Convert.ToString(a + b));
                    break;
                case "-": Console.WriteLine(Convert.ToString(a - b));
                    break;
                case "*": Console.WriteLine(Convert.ToString(a * b));
                    break;
                case "/": Console.WriteLine(Convert.ToString(a / b));
                    break;
               default: goto rechoice;
            }

            Console.Read();

        }
    }
}

Output screen shots:

Simple Calculator Output

For discarding the choice :

Simple Calculator Output
Output window for Simple Calculator

If you copy the logic code and copy and pest the logic in to visual studio 2010 and run the console application of logic so you can see the output as we show upper. It’s fully working!!!

Write a Program to make a Simple Calculator in JAVA :

Here same logic we will apply in JAVA but the syntax of the java is different from the c# so here changes will be only in to syntax of JAVA. So enjoy the code in java.

Code:

 package HHH;

import java.util.Scanner;

public class calc {
   
     public static void main(String[] args) {

        String choice;
        int a,b;
       
        Scanner in = new Scanner(System.in);

        System.out.println("Enter first no:");
        a =  in.nextInt();
        System.out.println("Enter second no:");
        b = in.nextInt();
        System.out.println("Enter choice like +,-,* or /");
        choice = in.next();
       
        switch(choice)
        {
            case "+" : System.out.println(a+b);
                        break;
            case "-" : System.out.println(a-b);
                        break;
            case "*" : System.out.println(a*b);
                        break;
            case "/" : System.out.println(a/b);
                        break; 
        }
     
     }

    }

You can see changes of the syntax will be change in to the java like for print here use System.out.println("”); and for convert string to integer in.nextint(); if there is double value so we need to write in.nextdouble(); now see the output of the calculator in to the java.

Output screen shots:

This screen short is taken from the net beans this logic is copy and pest into the net beans class then you see the output like this. 

Output window for Simple Calculator
 
Write a Program to make a Simple Calculator in C:
Hear in c same as the java logic is used same as the java and c# but the changes is only syntax. So enjoy the simple calculator in c language. 

Code:

 #include<stdio.h>
 #include<conio.h>

 void main()
 {

  int  a,b;
  char choice;
  clrscr();

        printf("Enter first no:");
        scanf ("%d",&a);
        printf("Enter second no:");
        scanf ("%d",&b);

        rechoice:

        printf("Enter choice like +,-,* or /");
        scanf ("%c",&choice);


        switch (choice)
        {

        case '+': a = a+b; printf("%d",a);
                 break;
        case '-': a = a-b; printf("%d",a);
                 break;
        case '*': a = a*b; printf("%d",a);
                 break;
        case '/': a = a/b; printf("%d",a);
                 break;
         default: goto rechoice;
       
        }

         getch();
  }

 Output screen shorts:


This is the fully tutorial for the making the simple calculator in to the different different language for the beginner.

2 comments:

Post a Comment