Jump to the post that solved this thread.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error in getting sum of numbers program
#1
Solved: 9 Years, 9 Months, 1 Week ago
hi all

I am new to scripting and i have written program which give sum of the digits in a number as follows:
Code:
Dim num,sum,getdigit
sum=0
getdigit=""
num=Inputbox("Enter a four digit number")
If Len(num) <> 4 Then
Msgbox("Number is not four digit no")
else
For i=0 to Len(num)
getdigit=Mid(num,i+1,1)
'MsgBox(getdigit)
sum = sum+cInt(getdigit)
next
MsgBox("Sum of the digits is " & sum)
End if
I am getting Type mismatch error at
sum= sum + cInt(getdigit)
but I am not able to understand why I am getting this error because i am converting string to number.
Can somebody help me with this?
Reply
#2
Solved: 9 Years, 9 Months, 1 Week ago
Hi,
i have updated your for loop code, it is working now
Code:
Dim num,sum,getdigit
sum=0
getdigit=""
num=Inputbox("Enter a four digit number")
If Len(num) <> 4 Then
Msgbox("Number is not four digit no")
else
For i=0 to Len(num)-1
getdigit=Mid(num,i+1,1)
'MsgBox(getdigit)
sum = sum+cInt(getdigit)
next
MsgBox("Sum of the digits is " & sum)
End if
Reply
#3
Solved: 9 Years, 9 Months, 1 Week ago
I understood the error... Error was coming only in the last iteration!!
Thanks for the help.
Reply
Jump to the post that solved this thread.


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to increase numbers for value in Excel Jyobtech 2 3,351 10-26-2012, 04:56 PM
Last Post: Jyobtech
  display any 10 numbers in ascending order using arrays in vb scripting haritha 4 23,829 01-03-2012, 05:46 PM
Last Post: shivu.hanu
  "How to write Batch Script runner program" suresz449 2 3,828 12-19-2011, 05:15 PM
Last Post: Munna.Sarfraz
  "VBScript Program" suresz449 0 2,637 12-19-2011, 09:55 AM
Last Post: suresz449
  Write a program to Capture Desktop Screen shot kumarmoyilla 2 11,445 07-11-2011, 10:51 AM
Last Post: souvikghosh_diatm

Forum Jump:


Users browsing this thread: 1 Guest(s)