Exercises for Beginners 2

closed account (1v5E3TCk)
Write a small calculator that takes as input one of the four arithmetic operations, the two arguments to those operations, and then prints out the result;

there is more then one solutions for that exercises:

1.Using if statements:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>

using namespace std;


int main()

{

int first, second, choice; // integers for stroring the numbers and selected operation

             cout<<"Input the first number.\n";
             cin>>first;

             cout<<"Input the second number.\n";
             cin>>second;

             cout<<"--Choose your operation--\n"<<endl;
             cout<<"1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n";
             cin>>choice;

        if(choice==1)
             {cout<<"Addition of the numbers is:"<<first+second<<endl;  }

        else if(choice==2)
                   {cout<<"Subtraction of the numbers is:"<<first-second<<endl;    }

        else if(choice==3)
                  {cout<<"Multiplication of the numbers is:"<<first*second<<endl;     }

        else if(choice==4)
                  {cout<<"Division of the numbers is:"<<first/second<<endl;  }


        system("pause");
}


2. Using switch-case:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

#include <iostream>

using namespace std;


int main()

{

int first, second, choice; // integers for stroring the numbers and selected operation

             cout<<"Input the first number.\n";
             cin>>first;

             cout<<"Input the second number.\n";
             cin>>second;

             cout<<"--Choose your operation--\n"<<endl;
             cout<<"1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n";
             cin>>choice;

        switch(choice)
        case 1:
             cout<<"Addition of the numbers is:"<<first+second<<endl;
             break;

        case 2:
             cout<<"Subtraction of the numbers is:"<<first-second<<endl;
             break;
        
        case 3:
              cout<<"Multiplication of the numbers is:"<<first*second<<endl;
              break;
 
        case 4:
              cout<<"Division of the numbers is:"<<first/second<<endl;
              break;

        system("pause");
}



3. Using using functions for each operation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

#include <iostream>
using namespace std;

int menu()
{
       int a;

             while(1)
{
             cout<<"**********************\n";
             cout<<"**Choose your operation**\n";
             cout<<"**********************\n";
             cout<<"1.Addition\n2.Subtraction\n3.Multiplaction\n4.Division\n5.Exit\n";
             cin>>a;

             if(a==1||a==2||a==3||a==4||a==5){break;}

             cout<<"Bad ınput.\n";}
             return a;
}

void addition(int x, int y)
{
             cout<<"Input the first number.\n";
             cin>>x;

             cout<<"Input the second number.\n";
             cin>>y;

             cout<<"\n**Addition of numbers:"<<x+y<<endl;
                
    }
    
void subtraction(int x, int y)
{
             cout<<"Input the first number.\n";
             cin>>x;

             cout<<"Input the second number.\n";
             cin>>y;

             cout<<"\n**Subtraction of numbers:"<<x-y<<endl;
              
    }    
    
void multiplaction(int x, int y)
{
             cout<<"Input the first number.\n";
             cin>>x;

             cout<<"Input the second number.\n";
             cin>>y;

             cout<<"\n**Multiplaction of the numbers:"<<x*y<<endl;
                
    }       
    
void division(double x, double y){
             cout<<"Input the first number.\n";
             cin>>x;

             cout<<"Input the second number.\n";
             cin>>y;

             cout<<"\n**Division of the numbers:"<<x/y<<endl;
               
    }     
    
int main()
{
    int x,y,b;    
    b=menu();
    if(1==b){addition(x,y);}       
    if(2==b){subtraction(x,y);}
    if(3==b){multiplaction(x,y);}
    if(4==b){division(x,y);}
    if(5==b) {return 0;}
    }
Topic archived. No new replies allowed.