Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

3.

/*program to create basic calculator


performing add , sub,mul and div in C++*/

#include <iostream>
using namespace std;

class calculator {
float a, b;

public:

void get() {
cout<<"enter first number:";
cin>>a;
cout<<"enter second number:";
cin>>b;
}
float add()
{
return a+b;
}
float sub()
{
return a-b;
}
float mul()
{
return a*b;
}
float div()
{
if(b==0.0){
cout<<"division by zero"<<endl;
exit(0);
}
else{
return a/b;
}
}
};

int main(){
int choice;
float result;
calculator cal;
cout<<"enter 1 add 2 numbers"<<"\n enter
2 subtract 2 numbers"<<"\n enter 3 multiply 2
numbers"<<"\n enter 4 divide 2 numbers"
<<"\n enter 0 to exit"<<"\n";
do {
cout<<"\n enter choice:";
cin>>choice;

switch (choice)
{
case 1:
cal.get();
cout<<"result:"<<cal.add()<<endl;
break;
case 2:
cal.get();
cout<<"result:"<<cal.sub()<<endl;
break;
case 3:
cal.get();
cout<<"result:"<<cal.mul()<<endl;
break;
case 4:
cal.get();
cout<<"result:"<<cal.div()<<endl;
break;
}

} while (choice >= 1 && choice <= 4);

return 0;
}

OUTPUT:
enter 1 add 2 numbers
enter 2 subtract 2 numbers
enter 3 multiply 2 numbers
enter 4 divide 2 numbers
enter 0 to exit

enter choice:1
enter first number:5
enter second number:3
result:8

enter choice:2
enter first number:8
enter second number:5
result:3
enter choice:3
enter first number:2
enter second number:9
result:18

enter choice:4
enter first number:8
enter second number:4
result:2

enter choice:0

4. /*program to sort array of integers in


ascending order using pointers in C++*/
#include<iostream>
using namespace std;

int main()
{
int n, i, j, *a, temp;
cout<<"enter the number of elements to store
in the array:"<<endl;
cin>>n;
cout<<"enter the number of elements in the
array:"<<endl;
for(i = 0; i < n; i++){
cin >> *(a + i);
}
for(i = 0; i < n; i++){
for(j = i+1; j < n; j++)
{
if(*(a + i) > *(a + j))
{
temp = *(a + i);
*(a + i) = *(a + j);
*(a + j) = temp;
}
}
}
cout<<"element in the array after
sorting:"<<endl;
for(i = 0; i < n; i++)
{
cout << *(a + i) << endl;
}
return 0;
}
OUTPUT:
enter the number of elements to store in the
array:
6
enter the number of elements in the array:
45
90
18
7
100
65
element in the array after sorting:
7
18
45
65
90
100

You might also like