Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Add digits of a number, and then add the digits of the sum.
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
pttp




PostPosted: Sun May 01, 2016 4:08 pm   Post subject: Add digits of a number, and then add the digits of the sum.

Hey everyone,
so for school, i have to make a program that adds all the digits of a number. But, if the sum is more than 1 digit, you have to add the digits of the sum. This continues until you have a 1 digit answer.
For example, 135 is 1+3+5=9. But 138 is 1+3+8=12=1+2=3

Now as for the code, I did the main part which can add the digits, but i dont know how to add the digits of the sum. My code is:
-----------------------------
var numberS : string
var number : int
var total : int := 0

loop
put "Please enter a number: " .. get numberS
exit when strintok(numberS)
cls
put "Enter a valid number"
end loop

for x : 1 .. length(numberS)
total := strint(numberS(x)) + total
end for

if length(total) > 1 then

end if

put total
--------------------

Also, I need to make the program also work with hexadecimal. Can anyone help please?
Really all i need help with is finding a way to add the digits of "total" until there is only 1 digit answer. And i need to know how to do stuff in hexadecimal.

thanks Smile
Sponsor
Sponsor
Sponsor
sponsor
pttp




PostPosted: Sun May 01, 2016 4:25 pm   Post subject: Re: Add digits of a number, and then add the digits of the sum.

I was thinking of doing something like:
---------------
if length(total) > 1 then
for x : 1 .. length(total)
total1 := strint(total(x)) + total1
end for
put total1

else put total
end if
------------------
and looping it until the length = 1
but obviously that doesn't work. So what can i do guys?
Insectoid




PostPosted: Sun May 01, 2016 4:57 pm   Post subject: RE:Add digits of a number, and then add the digits of the sum.

Have you learned about functions yet? That might be the easiest solution. Write a function that takes a number as input and outputs the sum of the digits. You can run it in a loop until the number is only one digit long.

The easiest way to make it work with hexadecimal is to write your own version of strint() and strintok() that also accepts the letters A to F. From there your code will be essentially the same.
pttp




PostPosted: Sun May 01, 2016 5:08 pm   Post subject: RE:Add digits of a number, and then add the digits of the sum.

how would i do the function? we didn't learn that yet because our teacher is bad. But i think we're supposed to use it. So how would i write this function? thanks so much.

As for hexa, i figured that out by myself but thanks
Insectoid




PostPosted: Sun May 01, 2016 5:20 pm   Post subject: RE:Add digits of a number, and then add the digits of the sum.

A function is kind of like a mini program inside your main program, so you can use the same code a bunch of times without writing it out over and over again. There are a lot of built-in functions in Turing- strint() is a function. It's a mini program that takes in a string and converts it to a number.

Writing a function looks like this:
code:
function foo (input : type) : type
    %do stuff in here!
    result output
end foo


The name of this function is foo. 'input' is the input to the function. You can have one or many inputs and you can name them whatever you want. the first 'type' is the input type. It can be string, int, real, etc. The second 'type' is the output type. Inside the function is all the code you need to compute an output. The 'result' keyword terminates the function and spits out a result.

Here's an example:
code:
function add (a : int, b : int) : int
    result a + b
end add[code]

Your function should look kinda like this:
[code]function addDigits (input : string) : string
    var answer : string
    %calculate sum of digits
    result answer
end addDigits


Now you can run the function over and over in a loop, passing the output back into the function as new input, until there's just one digit.
pttp




PostPosted: Sun May 01, 2016 5:28 pm   Post subject: RE:Add digits of a number, and then add the digits of the sum.

Hey,
i think i understand what youre saying but i dont know how to incorporate it into my code.

Also, I was playing around and i have an idea for my code but there's an error that i cant fix. Can you help me fix it?

------------------
var numberS : string
var number : int
var total : int := 0

loop
put "Please enter a number: " .. get numberS
exit when strintok(numberS)
cls
put "Enter a valid number"
end loop

loop
for x : 1 .. length(numberS)
total := strint(numberS(x)) + total
numberS := intstr (total)
end for
if length(numberS) > 1 then
put numberS
exit
end if
exit when length(numberS) = 1
end loop

put numberS
-------------------------

thanks Smile
Insectoid




PostPosted: Sun May 01, 2016 6:47 pm   Post subject: RE:Add digits of a number, and then add the digits of the sum.

Your error message means you are trying to access an index in the string that doesn't exist. If your string is length 4, then you're trying to access character 5. This crashes your program. Can you figure out why?
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: