Adding Fraction ..............




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
#include <iostream.h>  \\     Title: Adding Fraction    \\
#include <math.h>      \\     Author:Ismael Balana(biginner)\\
        \\     Description:This program is able to compute fraction in additional operation.\\
		\\     Assuming that 1(n)     1 (nn)    4             \\
		\\                  ------ + ----- = ---------          \\
		\\                   2 (d)    2 (dd)    4             \\
		\\     In the formula i used                              \\
		\\                 (n*dd) + (d*nn)                       \\
		\\               -------------------- =Sum            \\
		\\                  (d*dd)                        \\
		

int main()
{
	int n,nn,d,dd,denominator,numerator;
        
        double proper;
    
	

	cout <<"Input numerator:   ";
	cin >> n;
	cout <<"Input denominator: ";
	cin >> d;
	cout <<"Input second numerator:   ";
	cin >> nn;
	cout <<"Input second denominator: ";
	cin >> dd;
	
	numerator= n*dd + d*nn;
	cout <<"Numerator:  "<<numerator<<endl;
	denominator= d*dd;
	cout <<'\n';
	cout <<"Denominator:"<<denominator<<endl;
	if (numerator>denominator)
	{
		cout<<"Fraction is improper";
		proper=numerator/denominator;
		cout<<"\nProper fraction in decimal form:"<<proper<<endl;
		
	}

	
	else
	{
		cout<<"Fraction is Proper\n";
	}

	 


	return 0;
}

i need your comment and suggestion to make it better
thanks..
I hope you likeit


Last edited on
I guess you'll want to change the data type of the "proper" variable.

 
proper = numerator / denominator;


may cause not to display the digits after the decimal point. A good suggestion is to make it either float or double data type.
well, i guess i need to learn more about daclaring variables and their data types.. but thanks for the good suggestion.

i want to ask you something..
what is difference between #include directives and usingname space ........
can you elaborate please...

and do while statement ....hence im too confusing both of them..
thanks in advance..
The #include directives indicate which libraries you are using. The reason namespace was created is because in some libraries, more than one function have the same prototypes. To give distinction for these functions, we put them into namespace.

For example, let's have these two header files.
1
2
3
4
5
6
7
8
9
10
11
//one.h
//include guards omitted
namespace one{
    void f();
}

//two.h
//include guards omitted
namespace two{
    void f();
}


If we include both headers, the compiler won't be able to tell which f() to call if we call that in our program. However, doing one::f() or two::f() clearly defines which f() to call.

"using namespace" will help remove typing the namespace in your code over and over again. A sample:
1
2
3
4
5
6
#include "one.h"
#include "two.h"

using namespace one;
f(); //will call one::f();
two::f(); //still ok, will call two::f(); 


I see no problem in your code if you are not using the C++ standards. In C++ standard, the iostream.h is already deprecated. If you set your flags properly, the compiler will throw that warning. It will also suggest you use:
 
#include <iostream> 

instead of
 
#include <iostream.h> 


If you want to use c-standard libraries, just add 'c' at the beginning of the library name. Example:
 
#include <cctype> 


The do-while loop is no different from a while loop. The do-while loop ensures that the block is executed at least once before evaluating the condition of the loop.
Last edited on
tnx.......
even it so to complicated for me at least i learned....
tnx...
if you dont mind can you teach me again about c++ more ...
i want to learn more hence C++ is very interesting ...program

No problem.

Just ask what you do not understand.
Topic archived. No new replies allowed.