-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcycle1.2_q_4.cpp
More file actions
106 lines (92 loc) · 1.92 KB
/
cycle1.2_q_4.cpp
File metadata and controls
106 lines (92 loc) · 1.92 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
4. Write a function called power() that takes a double value for n and an int
value for p, and returns the result as a double value. Create a series of
overloaded functions with the same name that, in addition to double, also
work with types char, int, long, and float. Write a main() program that
exercises these overloaded functions with all argument types.
*/
#include <iostream>
using namespace std;
double power(double n,int p){
int i;
double product=1;
for(i=0;i<p;i++){
product = product*n;
}
return (product);
}
double power(int n,int p){
int i;
int product=1;
for(i=0;i<p;i++){
product = product*n;
}
return (product);
}
double power(float n, int p){
int i;
float product=1;
for(i=0;i<p;i++){
product = product*n;
}
return (product);
}
double power(int n, char letter){
int p=0;
if(letter=='s')
p=2;
else if(letter=='c')
p=3;
else{
cout<< "invalid input "<<endl;
return 0;
}
int i,product=1;
for(i=0;i<p;i++){
product = product*n;
}
return (product);
}
int main(){
int opt;
cout << "\t Power calculator " <<endl;
cout << "Choose the option " <<endl;
cout << " 1 Base-double Exponent-int \n 2 Base-int Exponent-int \n 3 Base-float Exponent-int \n 4 Base-int Exponent-char \n";
cin >> opt;
switch(opt){
case 1:{
double b;
int e;
cout<<"Enter the Base and Exponent "<<endl;
cin>>b>>e;
double r = power(b,e);
cout << r;
break;
}
case 2:{
int b,e;
cout<<"Enter the Base and Exponent "<<endl;
cin>>b>>e;
int r = power(b,e);
cout << r;
break;
}
case 3:{
float b;
int e;
cout<<"Enter the Base and Exponent "<<endl;
cin>>b>>e;
double r = power(b,e);
cout << r;
break;
}
case 4:{
char e;
int b;
cout << "Enter the base and Exponent (square-'s',cube-'c')" << endl;
cin>>b>>e;
double r = power(b,e);
cout << r;
}
}
}