Write a C++ program to MAke Simple calculator

Write a program and call it calc.cpp which is the basic calculator and receives three values from input via keyboard?

 The first value as an operator (Op1) should be a char type and one of (+, -, *, /, s) characters with the following meanings:
o ‘+’ for addition (num1 + num2)
o ‘-’ for subtraction (num1 – num2)
o ‘*’ for multiplication (num1 * num2)
o ‘/’ for division (num1 / num2)
o ‘s’ for swap
 Program should receive another two operands (Num1, Num2) which could be float or integer.
 The program should apply the first given operator (Op1) into the operands (Num1, Num2) and prints the relevant results with related messages in the screen.
 Swap operator exchanges the content (swap) of two variables, for this task you are not allowed to use any further variables (You should use just two variables to swap).
#include
#include

using namespace std;

int main()
{
//-------defining variables and initializing them-------------    
    double num1,num2;
    char operation,redo;
//--------Printing my name on screen----------------    
    cout< <"Welcome to the calculater program v.1.0 written by Your Name"<<endl;
    cout<<"***************************************************************"<<endl;
    cout<<endl<<endl<<endl;
//--here do loop is used so that the program can be used more then one time
//without exiting the run screen---------------------------    
    do
    {
 //----receiving the variables from input--------------         
    cout<<" Please enter an operation which you like to calculate (+,-,*,/,s)";
    cout<<"[s stands for swap]:"; cin>>operation ;
    cout< <endl<<endl;
     cout<<" Please enter two numbers to apply your requested operation(";
    cout<<operation<<"):"<<endl<<"1st num:"; cin>>num1;
    cout< <"2nd num:" ; cin>>num2;
    cout< <endl;
 //---used switch function so thet the operater can be decided------------  
    switch (operation)
    {
//------calculating the requested equation for inputs------------- 
//-------at the same time printing the results on screen-----------          
     case'+':            
             cout<<"The addition of two numbers ("<<num1<<","<<num2<<"):";
             cout<<num1+num2<<endl;
             break; 
     case'-':
             cout<<"The substraction of two numbers ("<<num1<<","<<num2<<"):";
             cout<<num1-num2<<endl;
             break;
      case'*':
             cout<<"The multiplication of two numbers ("<<num1<<","<<num2<<"):";
             cout<<num1*num2<<endl;
             break;
      case'/':
             cout<<"The division of two numbers ("<<num1<<","<<num2<<"):";
             if(num2==0)
             {
             cout<<"not valid"<<endl;
             }
             cout<<(num1/num2)<<endl;
             break;
      case's':
             cout<<"The swap of two numbers ("<<num1<<","<<num2<<"):";
             swap(num1,num2);
             cout<<"1stnumber="<<num1<<"and 2nd number="<<num2<<endl<<endl;                    
           break;
      default:
              cout<<"unknown command"<<endl;
      
           }
 //----now once again the program will ask the user if want to continue or not          
           cout<<"enter y or Y to continue:"; cin>>redo;
           cout< <endl<<endl;
           }
           while(redo=='y'||redo=='Y');
           
   system("pause");
    return 0;
    
    }

 

Java

The final keyword in Java Example

/***********************************************************************
 * *
 * The final keyword in Java Example
 * 
 * **********************************************************************
 **/
package lesson1;

/*
Final keyword has a numerous way to use:
A final class cannot be subclassed.
A final method cannot be overridden by subclasses
A final variable can only be initialized once
*/
public class Hello {
    public final int number;

    Hello() {
        number = 10;
    }
}

Public, Private, Protected and this (Java Access Modifiers) Example

/*************************************************************************
 * Public, Private, Protected and this (Java Access Modifiers) Example
 *************************************************************************/
package lesson1;

/*Access Levels
Modifier      Class Package Subclass World
public         Y       Y        Y       Y
protected      Y       Y        Y       N
no modifier    Y       Y        N       N
private        Y       N        N       N
*/
public class Student {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

public class MyClass {
    public static void main(String[] args) {
        Student stu = new Student();
        stu.setName("mark");
        System.out.println(stu.getName());
    }
}

Method Overloading in Java Example

/*************************************************************************
 * Method Overloading in Java Example
 *************************************************************************/
package lesson1;

public class MyClass {
    public static void main(String[] args) {
        System.out.println(Add(1, 36));
        System.out.println(Add(5.656, 40.66));
        System.out.println(Add("hello", " world"));
    }

    public static int Add(int a, int b) {
        return (a + b);
    }

    public static double Add(double a, double b) {
        return (a + b);
    }

    public static String Add(String a, String b) {
        return (a + b);
    }
}

Classes and Objects Example

/*************************************************************************
 * Classes and Objects Example
 *************************************************************************/
package lesson1;

public class Student {
    int id;
    String name;
    int age;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

public class MyClass {
    public static void main(String[] args) {
        Student mark = new Student();//mark -> object or instance
        mark.setId(1);
        mark.setName("Mark");
        mark.setAge(15);
        Student tom = new Student();//Tom -> object or instance
        tom.setId(1);
        tom.setName("Tom");
        tom.setAge(14);
        System.out.println(mark.getName() + " is " + mark.getAge() + " years old");
        System.out.println(tom.getName() + " is " + tom.getAge() + " years old");
    }
}

Parameter passing and Returning a Value from a Method Example

/*************************************************************************
 * Parameter passing and Returning a Value from a Method Example
 *************************************************************************/
package lesson1;

public class MyClass {
    public static void main(String[] args) {
        int sum = Add(100, 201, 211);
        int result = sum * 15;
        System.out.println(result);
    }

    public static void sayHello(String name) {
        System.out.println("Hello " + name);
    }

    public static int Add(int a, int b, int c) {
//System.out.println(a+ b +c);
        return (a + b + c);
    }
}

Java String Example

/*************************************************************************
 * Java String Example
 *************************************************************************/
package lesson1;

public class MyClass {
    public static void main(String[] args) {
        String myString = "Hello e World e";
        System.out.println(myString);
        int myStringLegth = myString.length();
        System.out.println(myStringLegth);
        String myStringinCase = myString.toUpperCase();
        System.out.println(myStringinCase);
        System.out.println(myString.indexOf('o'));
    }
}

String class :
• are basically immutable objects in Java.
• Immutable means once created, the strings cannot be changed.
• Whenever we create strings, it is this class that is instantiated.
• In Java strings can be instantiated in two ways:
– String str= “You Cannot Change me”;
– String str2=new String (“You Cannot Change me”);
Java.lang.String :
To create an empty string String

String str1=new String();
String str2="You cannot "+ "Change me";
Char ch[]={'a' , 'b' , 'c'};
String str3=new String(ch);
OUTPUT(abc)

The for Statement in Java (for loops) Example

/*************************************************************************
 * The for Statement in Java (for loops) Example
 *************************************************************************/
package lesson1;

public class MyClass {
    public static void main(String[] args) {
        int[] myintarray = {100, 31, 26, 48, 52};
        for (int index = 0; index < 5; index++) {
            System.out.println(myintarray[index]);
        }
        for (int element : myintarray) {
            System.out.println(element);
        }
    }
}

Arrays in Java Example

/*************************************************************************
 * Arrays in Java Example
 *************************************************************************/
package lesson1;

public class MyClass {
    public static void main(String[] args) {
        int[] myintarray = {100, 31, 26, 48, 52};
/*
int[] myIntArray = new int[3];
int[] myIntArray = {1,2,3};
int[] myIntArray = new int[]{1,2,3};
*/
        int index = 0;
        while (index < 5) {
            System.out.println(myintarray[index]);
            index++;
        }
    }
}

The do-while Statements (do-while Loops) Example

/*************************************************************************
 * The do-while Statements (do-while Loops) Example
 *************************************************************************/
package lesson1;

public class MyClass {
    public static void main(String[] args) {
        int a = 0;
        while (a < = -1) {
            System.out.println(a);
            a++;
        }
        System.out.println("------------------------------");
        int b = 0;
        do {
            System.out.println(b);
            b++;
        } while (b <= -1);
    }
}

The while Statements (while Loops) Example

/*************************************************************************
 * The while Statements (while Loops) Example
 *************************************************************************/
package lesson1;

public class MyClass {
    public static void main(String[] args) {
        int a = 10;
        while (a >= 1) {
            System.out.println(a);
            a--;
        }
    }
}

switch Statement in Java Example

/*************************************************************************
 * switch Statement in Java Example
 *************************************************************************/
package lesson1;

public class MyClass {
    public static void main(String[] args) {
        int score = 80;
// byte, short, int, or char.
        switch (score) {
            case 100:
            case 95:
            case 90:
                System.out.println("Very good");
                break;
            case 80:
            case 60:
                System.out.println("Good");
                break;
            case 40:
                System.out.println("OK");
                break;
            default:
                System.out.println("The grades are not defined");
                break;
        }
    }
}

IF … ELSE Statements and Relational Operators Example

/*************************************************************************
 * IF ... ELSE Statements and Relational Operators Example
 *************************************************************************/
package lesson1;

public class MyClass {
    /*
    == is equal to
    != is not equal to
    > is greater than
    < is less than
    >= is greater than or equal to
    < = is less than or equal to
    */
    public static void main(String[] args) {
        int x = 10;
        if (x <= 20) {
            System.out.println("yes x == 10");
        } else {
            System.out.println("no x != 10");
        }
    }
}

Math and Arithmetic Operators in Java Example

/*************************************************************************
* Math and Arithmetic Operators in Java Example
*
*************************************************************************/
The Java programming language supports various arithmetic operators for all floatingpoint
and integer numbers. These operators are (+)(addition), (-)(subtraction),(*)
(multiplication), (/)(division), and (%)(modulo). The following table summarizes the
binary arithmetic operations in the Java programming language.
Operator Use Description
+ op1 + op2 Adds op1 and op2; also used to concatenate strings
– op1 – op2 Subtracts op2 from op1
* op1 * op2 Multiplies op1 by op2
/ op1 / op2 Divides op1 by op2
% op1 % op2 Computes the remainder of dividing op1 by op2

public class ArithmeticDemo {
    public static void main(String[] args) {
//a few numbers
        int i = 37;
        int j = 42;
        double x = 27.475;
        double y = 7.22;
        System.out.println("Variable values...");
        System.out.println(" i = " + i);
        System.out.println(" j = " + j);
        System.out.println(" x = " + x);
        System.out.println(" y = " + y);
//adding numbers
        System.out.println("Adding...");
        System.out.println(" i + j = " + (i + j));
        System.out.println(" x + y = " + (x + y));
//subtracting numbers
        System.out.println("Subtracting...");
        System.out.println(" i - j = " + (i - j));
        System.out.println(" x - y = " + (x - y));
//multiplying numbers
        System.out.println("Multiplying...");
        System.out.println(" i * j = " + (i * j));
        System.out.println(" x * y = " + (x * y));
//dividing numbers
        System.out.println("Dividing...");
        System.out.println(" i / j = " + (i / j));
        System.out.println(" x / y = " + (x / y));
//computing the remainder resulting from dividing numbers
        System.out.println("Computing the remainder...");
        System.out.println(" i % j = " + (i % j));
        System.out.println(" x % y = " + (x % y));
//mixing types
        System.out.println("Mixing types...");
        System.out.println(" j + y = " + (j + y));
        System.out.println(" i * x = " + (i * x));
    }
}
The output from this program is:
Variable values...
i = 37
j = 42
x = 27.475
y = 7.22
Adding...
i + j = 79
x + y = 34.695
Subtracting...
i - j = -5
x - y = 20.255
Multiplying...
i * j = 1554
x * y = 198.37
Dividing...
i / j = 0
x / y = 3.8054
Computing the remainder...
i % j = 37
x % y = 5.815
Mixing types...
j + y = 49.22
i * x = 1016.58

Getting User Input using Java Example

/*************************************************************************
 * Getting User Input using Java Example
 *************************************************************************/
package lesson1;

import java.util.Scanner;

public class MyClass {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter some String");
        String user_input_string = scan.nextLine();
        long user_input_Long = scan.nextLong();
        System.out.println("The entered String is");
        System.out.print(user_input_string);
        System.out.println("The entered String is");
        System.out.print(user_input_Long);
    }
}

Variables and Types in Java

/*************************************************************************
 * Variables and Types in Java

    byte (number, 1 byte)
    short (number, 2 bytes)
    int (number, 4 bytes)
    long (number, 8 bytes)
    float (float number, 4 bytes)
    double (float number, 8 bytes)
    char (a character, 2 bytes)
    boolean (true or false, 1 byte)
 *************************************************************************/
package lesson1;

public class MyClass {
    public static void main(String[] args) {
        int my_int = 10;
        float my_float = (float) 6.7;
        double my_double = 111.77;
        char my_char = 'A';
        boolean my_bool = true;
        System.out.println(my_int);
        System.out.println(my_float);
        System.out.println(my_double);
        System.out.println(my_char);
        System.out.println(my_bool);
    }
}

Java Hello World Example

            
/*************************************************************************
* Compilation: javac HelloWorld.java
* Execution: java HelloWorld
*
* Prints "Hello, World". By tradition, this is everyone's first program.
*
* % java HelloWorld
* Hello, World
*
*
*************************************************************************/
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}

C++ Program to Convert Decimals to Binary

#include <iostream>
using namespace std;
int main()
{
                long dec,rem,i=1,sum=0;
    cout< <"Enter the decimal to be converted:";
    cin>>dec;
                do
    {
        rem=dec%2;
        sum=sum + (i*rem);
        dec=dec/2;
        i=i*10;
    }while(dec>0);
    cout< <"The binary of the given number is:"<<sum<<endl;
    cin.get();
    cin.get();
 
                return 0;
}
/*
OUTPUT:
Enter the decimal to be converted:16
The binary of the given number is:10000
*/

John Oliver

some text

Modular Javascript – Javascript Tutorial on the Object Literal Pattern

Modular Javascript #2 – Converting jQuery to an Object Literal Module

Modular Javascript #3 – Revealing Module Pattern Javascript Tutorial

Modular Javascript #4 – PubSub Javascript Design Pattern

NEWSLETTER

162639 insiders are already enjoying weekly updates and complimentary whitepapers!
Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

Email address:

CONNECT WID US

Sign up for our newsletter!

JOIN US!!

With 1,240,600 monthly unique visitors and over 500 authors we are placed among the top Java related sites around. Constantly being on the lookout for partners; we encourage you to join us. So If you have a blog with unique and interesting content then you should check out our JCG partners program. You can also be a guest writer for Java Code Geeks and hone your writing skills!