| |||||||
| Coding / Scripting / Programming Discussions on all manner of coding and scripting. |
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 (permalink) | |
| hey guys heres the problem i have to do...im totally stumped. %% Write a function called sumDigits() that takes in a number and returns %% the sum of the digits in the number as a single digit number. Your %% function should sum the digits in the number, and if that sum is greater %% than 9, then you should repeat the above process till you end up with a %% single digit. %% %% Note: You MUST use iteration to get credit for this problem. %% %% Example: %% sumDigits(34) should return 7 (since 3+4 = 7) %% sumDigits(345) should return 3 (since 3+4+5 = 12; 2+1 = 3) %% You can assume that the input will always be positive. %% The function header is given below: %% function digit = sumDigits(num) and here is what i have so far: function digit = sumDigits(num) %sumDigits takes a number and adds all of its digits until %the sum of all digits is a single digit number. % ex: 15 = 1 + 5 = 6 while(num > 9) mod(num, 10); num = floor(num/10) end num -------------------------------------------------------------------------------------- ok so i know theoretically what needs to happen but i cant execute! What i am trying to do is use mod to get the remainder of the last number...then somehow add that to the digit before it. mabye i could create a vector , remove the last digit from the number somehow and put it in the vector... then get the sum of the vector and reciprocate the process...idk what to do.. help Yikes =) | ||
| | | |
| Sponsored Links |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| producing phage > function of kanamycin | username#132 | Anything Goes | 0 | 21-January-06 02:00 PM |
| Usb Boot Function on NF7-S | Dr3w | Motherboards / CPUs | 2 | 13-July-05 05:52 PM |
| Slashdot // MATLAB Programming Contest Winner Announced | Gizmo | Slashdot RSS | 0 | 22-May-05 02:12 PM |