Skip to content

Commit 1ff35f3

Browse files
committed
feat: Implemented a Calculator
1 parent f8f4676 commit 1ff35f3

File tree

1 file changed

+46
-8
lines changed

1 file changed

+46
-8
lines changed

exercises/calculator.cpp

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,50 @@
11
/*
2-
Write a program that given two numbers as input make the main operations.
2+
Write a program that given two numbers as input make the main operations.
33
4-
Output:
5-
Insert first number: 4
6-
Insert second number: 2
4+
Output:
5+
Insert first number: 4
6+
Insert second number: 2
77
8-
SUM: 6
9-
Difference: 2
10-
Multiplication: 8
11-
Division: 2
8+
SUM: 6
9+
Difference: 2
10+
Multiplication: 8
11+
Division: 2
1212
*/
13+
#include <iostream>
14+
15+
using namespace std;
16+
17+
int main(){
18+
int num1;
19+
int num2;
20+
21+
cout<< "Insert first number: ";
22+
cin >> num1;
23+
24+
if(cin.fail()){
25+
cerr<<"Error: The given value is not a number"<<endl;
26+
exit(1);
27+
}
28+
29+
cout<< "Insert second number: ";
30+
cin>> num2;
31+
32+
if(cin.fail()){
33+
cerr<< "Error: The given value is not a number"<<endl;
34+
exit(1);
35+
}
36+
37+
38+
cout<< endl<<
39+
"SUM: "<< num1 + num2<< endl<<
40+
"Difference: " << num1 - num2<< endl<<
41+
"Multiplication: "<< num1 * num2 << endl;
42+
43+
if(num2 == 0){
44+
cout<<"Division: Unable to divide by 0"<< endl;
45+
return 0;
46+
}
47+
48+
cout<< "Division: "<< num1 / num2 <<endl;
49+
return 0;
50+
}

0 commit comments

Comments
 (0)