MAY3AM
MAY3AM

Reputation: 1212

Adding digits of a number until get one-digit number

Assume user enter a digit like "3452" i want a function to add this number digits one by one until get one-digits number as i mentioned below:

first aspect is:
3+4=7 -> 7+5= 12 ->( if number of digits exceed from 1 ) 1+2= 3 -> 3+2= 5

Second Aspect is:
3+4= 7 -> 7+5= 12 -> 12+2= 14 -> 1+4= 5

I wrote this:

    Function Sum(number)
        Dim i, intTemp
        intTemp = 0

        For i=1 to Len(number)
            intTemp = intTemp + CInt(Mid(number, i, 1))
        Next

        Sum = intTemp
    End Function

but this function return 2-digits number instead of one digits...guide me please

Edit: i put a Do while loop in a if statement, if statement check length of intTemp digits like this:

    if Len(intTemp)>1 then
        Do While Len(intTemp) = 1
            some code like first section
        Loop
    end if

but it doesn't Work! :(

Upvotes: 0

Views: 1704

Answers (4)

knvchowdary
knvchowdary

Reputation: 81

Here I'm declaring the variables & assigning the values

Dim str
str = InputBox("Enter the value")

Here I'm taking the temporary value for printing the @ end of result

temp = str
otp = 0
sum = 0
while(Len(str)>1)
For i = 1 to Len(str)
sum = sum+cint(mid(str,i,1))
Next
If otp = 0 Then
otp = sum
End If
str = cstr(sum)
sum = 0
wend
MsgBox "Total sum = "& otp
MsgBox "Single digit of "&temp+" is :"&str

Upvotes: 0

PatricK
PatricK

Reputation: 6433

It's fairly simple to me with your logic of summing digits. You need to recurse as soon as intTemp has 2 digits (i changed the name to DigitSum):

Function DigitSum(number)
    Dim i, intTemp, sNumber
    intTemp = 0
    sNumber = CStr(number)
    For i = 1 To Len(sNumber)
        intTemp = intTemp + CInt(Mid(sNumber, i, 1))
        If Len(CStr(intTemp)) > 1 Then intTemp = DigitSum(intTemp)
    Next
    DigitSum = intTemp
End Function

Upvotes: 2

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38755

As I can't make head or tails out of your spec, I created a "make it easy to experiment" script to play with two functions that deliver the expected result for your (single!) sample:

Option Explicit

Const cbDebug = False

WScript.Quit demoMain()

Function demoMain()
  Dim aTests : aTests = Array( _
      Array("3452", 5, 5) _
    , Array("0", 0, 0) _
    , Array("", 0, 0) _
    , Array("99", 9, 9) _
    , Array("999999", 9, 9) _
    , Array("991999", 1, 1) _
  )
  Dim aTest
  For Each aTest In aTests
      Dim sInp : sInp = aTest(0)
      WScript.Echo "--------", qq(sInp)
      Dim nExp : nExp = aTest(1)
      Dim nAct : nAct = fancySum01(sInp)
      WScript.Echo " Asp 1:", nExp, nAct, CStr(nExp = nAct)
      nExp = aTest(2)
      nAct = fancySum02(sInp)
      WScript.Echo " Asp 2:", nExp, nAct, CStr(nExp = nAct)
  Next
  demoMain = 0
End Function

'' "3452" 3+4=7 -> 7+5= 12 ->( if number of digits exceed from 1 ) 1+2= 3 -> 3+2= 5
Function fancySum01(sInp)
  fancySum01 = 0
  Dim p
  For p = 1 To Len(sInp)
      fancySum01 = fancySum01 + CByte(Mid(sInp, p, 1))
      If fancySum01 > 9 Then fancySum01 = (fancySum01 \ 10) + (fancySum01 Mod 10)
      If cbDebug Then WScript.Echo "  *", p, fancySum01
  Next
End Function

'' "3452" 3+4= 7 -> 7+5= 12 -> 12+2= 14 -> 1+4= 5
Function fancySum02(sInp)
  fancySum02 = 0
  Dim p
  For p = 1 To Len(sInp)
      fancySum02 = fancySum02 + CByte(Mid(sInp, p, 1))
      If cbDebug Then WScript.Echo "  *", p, fancySum02
  Next
  Dim r : r = CStr(fancySum02)
  If 1 < Len(r) Then fancySum02 = fancySum02(r)
End Function

Function qq(s) : qq = """" & s & """" : End Function

Sample output (cbDebug = False)

cscript 20845743.vbs
-------- "3452"
 Asp 1: 5 5 True
 Asp 2: 5 5 True
-------- "0"
 Asp 1: 0 0 True
 Asp 2: 0 0 True
-------- ""
 Asp 1: 0 0 True
 Asp 2: 0 0 True
-------- "99"
 Asp 1: 9 9 True
 Asp 2: 9 9 True
-------- "999999"
 Asp 1: 9 9 True
 Asp 2: 9 9 True
-------- "991999"
 Asp 1: 1 1 True
 Asp 2: 1 1 True

I really would like to get a hint at the real world problem you try to solve.

Upvotes: 0

Taegost
Taegost

Reputation: 1216

How about if you did something like this. It assumes that the number you're passing in is more than a single digit (greater than 9) and will continue looping until you end up with a single-digit result.

 Function Sum(number)
    Dim i, intTemp, workingNum
    workingNum = number

    Do
        intTemp = 0

        For i=1 to Len(workingNum)
            intTemp = intTemp + CInt(Mid(number, i, 1))
        Next

        workingNum = intTemp
    Loop until workingNum < 9

    Sum = intTemp
End Function

Upvotes: 0

Related Questions