-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfraction with classes.cpp
72 lines (65 loc) · 1.02 KB
/
fraction with classes.cpp
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
#include<iostream>
using namespace std;
class fraction
{
float numerator;
float denominator;
public:
void input()
{
cout<<"Numerator: "<<endl;
cin>>numerator;
cout<<"Denominator: "<<endl;
cin>>denominator;
}
void display()
{
cout<<"Fraction is : "<<numerator<<"/"<<denominator<<endl;
}
float convert()
{
return float(numerator/denominator);
}
};
void fraction_input(fraction f[],int n)
{
int i;
for (i = 0; i < n;i++)
{
cout<<"Fraction "<<i+1<<":"<<endl;
f[i].input();
}
cout<<endl;
}
void fraction_output(fraction f[],int n)
{
int i;
for (i = 0; i < n;i++)
{
f[i].display();
}
}
float fraction_sum(fraction f[],int n)
{
int i;
float sum = 0.0;
for (i = 0;i < n; i++)
{
sum+=f[i].convert();
}
return sum;
}
main()
{
int n;
cout<<"Enter the value of n : "<<endl;
cin>>n;
fraction f[n];
cout<<"Enter the fraction values : "<<endl;
fraction_input(f,n);
cout<<"Output: "<<endl;
fraction_output(f,n);
cout<<"Sum of n fractions : "<<endl;
cout<<fraction_sum(f,n)<<endl;
return 0;
}