How do I get a program to simplify a fraction, and how do I get a program to recognize if a fraction is a whole number? There is probably a simple solution, but I am oblivious. Thanks alot.

This is a discussion on *fractions and whole numbers* within the **C++ Programming** forums, part of the General Programming Boards category; How do I get a program to simplify a fraction, and how do I get a program to recognize if ...

- 11-14-2005 #1

- Join Date
- Nov 2005
- Posts
- 25

## fractions and whole numbers

How do I get a program to simplify a fraction, and how do I get a program to recognize if a fraction is a whole number? There is probably a simple solution, but I am oblivious. Thanks alot.

- 11-14-2005 #2

- Join Date
- Nov 2005
- Posts
- 545

For the whole number thing you could just divide the two numbers and if that is+ 1 then...

Code:no.3 == no.1 / no.2; if (no.3 == 1) { //stuff } else { //stuff }

- 11-14-2005 #3

- Join Date
- Nov 2005
- Posts
- 25

sorry, but I meant if a number is a whole number

- 11-14-2005 #4

- Join Date
- Nov 2005
- Posts
- 545

yes if you divide the two numbers in the fraction and they equal one then it is a whole number.

20/20 is a whole number 20/20 = 1

- 11-14-2005 #5

- Join Date
- Nov 2005
- Posts
- 545

Ah right I might have got you:

if no.1 > no.2

- 11-14-2005 #6

- Join Date
- Nov 2005
- Posts
- 25

yeah, and 1.2/1.2=1, and 1.2 isn't a whole number

- 11-14-2005 #7

- Join Date
- Nov 2005
- Posts
- 545

fractions cannot be 1.2 end of

- 11-14-2005 #8

- Join Date
- Nov 2005
- Posts
- 545

you cannot have 1.2/1.2 it does nto make mathematical sense!

- 11-14-2005 #9

- Join Date
- Sep 2005
- Location
- Earth
- Posts
- 282

C++ converts fractions to decimal representation and automatically simplifies to lowest term.

Code:cout << 3.0/9.0;

It also automatically recognizes if it is a whole number.

Code:cout << 8.0/4.0;

- 11-14-2005 #10

- Join Date
- Nov 2005
- Posts
- 25

No, you don't understand. I want the program to test if a number is a whole number. it will only accept integers. so if it isn't supposed to output numbers like 1.2, but it can output numbers like 56.

- 11-14-2005 #11

- Join Date
- Sep 2005
- Location
- Earth
- Posts
- 282

you cannot have 1.2/1.2 it does nto make mathematical sense!

1.2 is just 1 and 1/5, or 6/5

6/5 over 6/5 is perfectly legal. Or (6/5) / 9 is perfectly legal.

- 11-14-2005 #12

- Join Date
- Nov 2005
- Posts
- 25

How do I convert decimals back to fractions?

- 11-14-2005 #13

- Join Date
- Nov 2005
- Posts
- 545

yes but you cannot have 1.2/1.2

- 11-14-2005 #14

- Join Date
- Nov 2005
- Posts
- 545

1.2/1.2 == 1/1 == 1

- 11-14-2005 #15

- Join Date
- Apr 2003
- Posts
- 2,663

you cannot have 1.2/1.2 it does nto make mathematical sense!

- Exactly how to get started with C++ (or C) today
- C Tutorial
- C++ Tutorial
- 5 ways you can learn to program faster
- The 5 Most Common Problems New Programmers Face
- How to set up a compiler
- 8 Common programming Mistakes
- What is C++11?
- Creating a game, from start to finish

- How to create a shared library on Linux with GCC - December 30, 2011
- Enum classes and nullptr in C++11 - November 27, 2011
- Learn about The Hash Table - November 20, 2011
- Rvalue References and Move Semantics in C++11 - November 13, 2011
- C and C++ for Java Programmers - November 5, 2011
- A Gentle Introduction to C++ IO Streams - October 10, 2011