Saturday 29 October 2016

Calculator example C++

Calculator example C++

code:


#include<iostream>
using namespace std;
class calculater
{
public: int n1, n2, ans;
public: void plus();
void sub();
void devide();
void multiply();
void wtite();
void read();
void square();
void prime();


};
void calculater::plus()
{
system("cls");
cout << "\t\t Addition" << endl;
cout << "\t\t ........" << endl;
cout << "Enter 1st number:";
cin >> n1;
cout << "Enter second number:";
cin >> n2;
ans = n1 + n2;
cout << "Result=" << ans << endl;
system("pause");
}
void calculater::sub()
{
system("cls");
cout << "\t\t Subtraction" << endl;
cout << "\t\t ..........." << endl;
cout << "Enter 1st number:";
cin >> n1;
cout << "Enter second number:";
cin >> n2;
ans = n1 - n2;
cout << "Result=" << ans << endl;
system("pause");
}
void calculater::devide()
{
system("cls");
cout << "\t\t Division" << endl;
cout << "\t\t ........" << endl;
cout << "Enter 1st number:";
cin >> n1;
cout << "Enter second number:";
cin >> n2;
ans = n1 / n2;
cout << "Result=" << ans << endl;
system("pause");
}
void calculater::multiply()
{
system("cls");
cout << "\t\t Multiplication" << endl;
cout << "\t\t .............." << endl;
cout << "Enter 1st number:";
cin >> n1;
cout << "Enter second number:";
cin >> n2;
ans = n1*n2;
cout << "Result=" << ans << endl;
system("pause");
}
void calculater::square()
{
system("cls");
cout << "Square of number" << endl;
cin >> n1;
cout << "Square of number is =" << n1*n1 << endl;
system("pause");
}
void calculater::prime()
{
system("cls");
int a = 2;
cout << "Prime number" << endl;
cin >> n1;
while (n1%a != 0)
a++;
if (n1 == a)
cout << "Prime number" << endl;
else
cout << "Not prime" << endl;
system("pause");
}
void main()
{
calculater c;
char option;
do{
system("cls");
cout << "\t\t Advance Calculator" << endl;
cout << "\t\t...................." << endl;
cout << "1.Basic Calculator.....B" << endl;
cout << "2.Advance Calculator...A" << endl;
cout << "3.Exit.................E" << endl;
cout << "  Select option:";
cin >> option;
if (option == 'B')
{
system("cls");
cout << "\t\tBasic Calculator" << endl;
cout << "\t\t................" << endl;
cout << "1.Add..............+" << endl;
cout << "2.Subtract.........-" << endl;
cout << "3.Multiply.........*" << endl;
cout << "4.Divide.........../" << endl;
cout << "5.Exit.............E" << endl;
cout << "6.Select option:";
cin >> option;
if (option == '+')
{
c.plus();
}
else if (option == '-')
{
c.sub();
}
else if (option == '*')
{
c.multiply();
}
else if (option = '/')
{
c.devide();
}
}
if (option == 'A')
{
system("cls");
cout << "\t\tAdvance Calculator" << endl;
cout << "\t\t.................." << endl;
cout << "1.Square of number....S" << endl;
cout << "2.Prime number........P" << endl;
cout << "3.Back................B" << endl;
cout << "  select option:";
cin >> option;
if (option == 'S')
{
c.square();
}
else if (option == 'P')
{
c.prime();
}
}
} while (option != 'E');
}

0 comments:

Post a Comment