Computer Science Canada

how do i output only integers when their are also decimal answers

Author:  coltss [ 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

Author:  Saad [ 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.

Author:  coltss [ 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

Author:  zero-impact [ Thu Apr 30, 2009 8:59 pm ]
Post subject:  RE:how do i output only integers when their are also decimal answers

mod

Author:  Insectoid [ 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

Author:  coltss [ 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

Author:  DtY [ 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


: