Skip to main content

C++: Code for Calculator.

Code no: 1 = Using different function 

#include <iostream>

using namespace std;

int sum(int a, int b)

{

int sum;

sum=a+b;

return sum;

}

int subtract(int a, int b)

{

int subtract;

subtract=a-b;

return subtract;

}

float divide(int a,int b)

{

    float divide;

divide=a/b;

return divide;

}

float multiply(int a,int b)

{

float multiply;

multiply=a*b;

return multiply;

}

int main()

{

int a,b,input,s,sb;

float m,d;

cout<<"Enter digit one : ";

cin>>a;

cout<<"Enter digit two : ";

cin>>b;

cout<<endl;

cout<<"1. Press 1 to add both numbers"<<endl<<"2. Press 2 to subtract both numbers"<<endl<<"3. Press 3 to multiply both numbers"<<endl<<"4. Press 4 to divide both numbers"<<endl;

    cout<<endl<<"Enter your choice = ";

cin>>input;

    

    s=sum(a,b);

    sb=subtract(a,b);

    m=multiply(a,b);

    d=divide(a,b);

    

if (input==1)

{

cout<<sum(a,b);

}

else if (input==2)

{

cout<<subtract(a,b);

}

else if (input==3)

{

cout<<multiply(a,b);

}

else if (input==4)

{

cout<<divide(a,b);

}

else 

{

cout<<"Invalid input.";

}

return 0;

}

Code no: 2 = Using one main function.

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
while (true)
{
cout<<endl;
cout<<"                  Calculator: "<<endl;
cout<<endl;
int numb1, numb2;
char operation;
cout<<"                  Please enter first no: = ";
cin>>numb1;
cout<<"                  Please enter second no: = ";
cin>>numb2;
cout<<"                 -----Please enter following character for respective operations-----"<<endl;
cout<<"                 1.  Press a for addition: "<<endl;
cout<<"                 2.  Press b for subtraction: "<<endl;
cout<<"                 3.  Press c for multiplication: "<<endl;
cout<<"                 4.  Press d for division: "<<endl;
cout<<"                 5.  Press e for reminder: "<<endl;
cout<<endl;
cout<<"                     Please enter operation : ";
cin>>operation;
if (operation=='a')
cout<<"                     The sum is "<<numb1+numb2<<endl;
else if (operation=='b')
cout<<"                     The subtraction is "<<numb1-numb2<<endl;
else if (operation=='c')
cout<<"                     The multiplication is "<<numb1*numb2<<endl;
else if (operation=='d')
cout<<"                     The division is "<<numb1/numb2<<endl;
else if (operation=='e')
cout<<"                     The reminder is "<<numb1%numb2<<endl;
else 
cout<<"                     Invalid Operation"<<endl;
   }
return 0;
}

Result:




Comments

Popular posts from this blog

C++ = Code for Counter

Code: #include <iostream> #include <conio.h> using namespace std; int main() {     int count = 0;     char input;     cout << "--------------------------" << endl;     cout << "          Counter         " << endl;     cout << "--------------------------" << endl;     cout << endl;     cout << "Press 1 to counter" << endl;     cout << "Press 0 to quit" << endl;     cout << endl;     cout << "Enter the number continuously : ";     while (true)     {         input = getch();         if (input == '1')         {             count += 1;             cout << "Your counts are " << count << endl;   ...

C++ = Code to check number is a prime or not. If not find the nearest prime number.

Code:  #include <iostream> using namespace std; bool prime(int n) { if (n<=1) { return false; }        for (int i=2;i<=n/2;i++)    {       if (n%i==0)   {   return false;   }    }         return true; } int nearest(int n) { for (int i=n-1;i>=2;i--) { if (prime(i)) { return i; } } return 0; } int main () { int number; cout<<"Enter any number : "; cin>>number; if (prime(number)) { cout<<"The number is prime. "<<endl; }     else     {     cout<<"The entered number is compositive."<<endl;     cout<<"The nearest prime number will be "<<nearest(number)<<endl; } } Result:

C++ = Code for checking any digit palindrome number

Code no: 1 = Using only main function to perform task. #include <iostream> using namespace std; int main() { int number,original,digit,reverse=0; cout<<"Enter any number : "; cin>>number; original=number; while (number!=0) { digit=number%10; reverse=(reverse*10)+digit; number=number/10; } if (original==reverse) { cout<<"This "<<original<<" is Palindrome number."; } else { cout<<"The "<<original<<" is not Palindrome number."; } } Result: Code no: 2 = Using two functions to perform same task. #include <iostream> using namespace std; int isprime(int num) { int digit=0,reverse=0; while (num!=0) { digit=num%10; reverse=(reverse*10)+digit;  num/=10; } return reverse; } int main() { int num; cout<<"Enter any digit to find its sum = "; cin>>num;          int original=num;          if (i...