how do i output only integers when their are also decimal answers
Author |
Message |
coltss
|
Posted: Thu Apr 30, 2009 8:41 pm Post subject: how do i output only integers when their are also decimal answers |
|
|
Hi guys im new to programming just need some help here
have to make a program that receives a integer beween 1 to 50 and finds the factors for that number. Well lets say the number is 10 . my program outputs 10/1 = 10 , 10/2 = 5 , 10/3 = 3.33, etc..
How do i make it so just outputs 1, 2, 5 , 10?
Turing: |
var number : int
var counter2,counter : real
put " Enter a number between 1 and 50 "
get number
counter := 0
counter2 := 0
for count: 1. . number
counter2 := counter2 + 1
counter := number / counter2
put counter
delay (1000)
end for
|
Please specify what version of Turing you are using
4.1.1 |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Saad
![](http://compsci.ca/v3/uploads/user_avatars/182387547249e7ebb91fb8a.png)
|
Posted: Thu Apr 30, 2009 8:47 pm Post subject: Re: how do i output only integers when their are also decimal answers |
|
|
round
floor
ceil
All these can be used to round to up/down or proper round. |
|
|
|
|
![](images/spacer.gif) |
coltss
|
Posted: Thu Apr 30, 2009 8:53 pm Post subject: RE:how do i output only integers when their are also decimal answers |
|
|
yes but i dont need to round i just need them to not be outputed just need to out put the factors of the number |
|
|
|
|
![](images/spacer.gif) |
zero-impact
![](http://compsci.ca/v3/uploads/user_avatars/872397874480c7089d4888.gif)
|
Posted: Thu Apr 30, 2009 8:59 pm Post subject: RE:how do i output only integers when their are also decimal answers |
|
|
mod |
|
|
|
|
![](images/spacer.gif) |
Insectoid
![](http://compsci.ca/v3/uploads/user_avatars/13760332514cbd0ce972eaa.jpg)
|
Posted: Thu Apr 30, 2009 9:12 pm Post subject: Re: how do i output only integers when their are also decimal answers |
|
|
if number mod count = 0 then
put number div count
end if |
|
|
|
|
![](images/spacer.gif) |
coltss
|
Posted: Fri May 01, 2009 5:09 pm Post subject: RE:how do i output only integers when their are also decimal answers |
|
|
ty guys appreciate it |
|
|
|
|
![](images/spacer.gif) |
DtY
![](http://compsci.ca/v3/uploads/user_avatars/8576159234be48b7a8b0e8.png)
|
Posted: Fri May 01, 2009 7:35 pm Post subject: RE:how do i output only integers when their are also decimal answers |
|
|
You should probably also know what mod actually does. (In the examples % is mod)
Mod is similar to division, but it tells you how much is left after dividing but not going into decimal places. (You probably did this in elementary school with long division, you write the answer, "r" then the remainder)
For example:
5 % 2 = 1
when you divide 2 into 5 (5/2) you get two with a remainder of one
4 % 2 = 0
When you get zero back from mod(ulo), that means the numerator goes evenly into the denominator leaving no remainder.
The most useful use of this (I've found) is alternating between two procedures.
Say you have a counter x, and you want to put out "a" then "b" for the number of times the user enters (printing "a" is once, "b" is the second)
let count = user input
let x = 0 (current count)
for x -> count do:
if (x mod 2) is 0: print "a"
or else: print "b"
(end for)
There's no point trying to use something if you have no idea what it does ![Smile Smile](http://compsci.ca/v3/images/smiles/icon_smile.gif) |
|
|
|
|
![](images/spacer.gif) |
|
|