 |

06-13-2012, 11:10 AM
|
|
Newcomer
|
|
Join Date: Jun 2012
Posts: 6
|
|
How to Determine if a integer with any digits is A Even or Odd Number ?
|
Erm... I'm new in vb and i faced a question while doing exercises.
well the question is like tis :
Write a VB program to accept ONE (1) integer value with any number of digits, determine the sum of each even value digits and minus the sum of each odd value digits. Print the difference between them. For example, the user input 4567, output will be –2. [(4+6) – (5+7)].
I knw how to determine a number is a odd or even number BUT if comes to A INTEGER VALUE WITH NUMBER OF DIGITS, how can i determine the digits is a odd or even number respectively ? Can anyone show me the answer/code ?
THANKS ~ 
|
|

06-13-2012, 12:49 PM
|
|
Centurion
|
|
Join Date: Jul 2003
Posts: 102
|
|
|
Convert the number to a string of characters. Convert each character back to a digit and work from there.
|
__________________
"Give a man a fish and you feed him for a day. Teach him how to fish and you feed him for a lifetime."
|

06-13-2012, 03:35 PM
|
 |
Senior Contributor
Forum Leader * Expert *
|
|
Join Date: Apr 2005
Location: USA
Posts: 866
|
|
Well, since the user is entering the integer, it probably arrives as a string to begin with. You might want to verify that it is an integer, but that's beside the point.
So this is what you will probably need to know how to do: - Examine one digit at a time.
- Determine whether a digit is even or odd.
- Maintain a list of even or odd digits.
- Get the value of a digit given it's string representation (5 and "5" are not the same thing).
- Get the sum of a list of numbers.
- Subtract two numbers.
The first step in programming something is to break things up into smaller, simple steps. Now that we've done that, it's easier to identify which parts you might have trouble with, or which parts you're having trouble stitching together.
|
__________________
C# _VB.NET _
|

06-14-2012, 12:56 AM
|
|
Newcomer
|
|
Join Date: Jun 2012
Posts: 6
|
|
STILL ~ i failed =.= dont really understand how its work and how to do
i'm too new in vb 
|
|

06-14-2012, 01:52 AM
|
 |
Lost Soul
Super Moderator * Guru *
|
|
Join Date: May 2001
Location: Vorlon
Posts: 18,885
|
|
|
Show what you have done so far. We are not going to do your homework for you!
|
|

06-14-2012, 05:18 AM
|
|
Newcomer
|
|
Join Date: Jun 2012
Posts: 6
|
|
Sorry =.= here is my code ~
Code:
Private Sub cmd1_Click()
Dim n As String, sumOdd As Integer, SumEven As Integer, i As Integer, ans As Integer
SumEven = 0
sumOdd = 0
n = InputBox("Enter a integer value with any number of digits", "Numbers", "Type Here")
For i = 1 To Len(n) Step 1
If Val(Mid(n, i, 1)) Mod 2 = 0 Then
SumEven = SumEven + i
Else
sumOdd = sumOdd + i
End If
Next i
ans = SumEven - sumOdd
Print ans
End Sub
The answer is correct when the n value is 4567 BUT when the n value is 9876, vb show me the wrong answer !! ~
WHY ???
|
|

06-14-2012, 05:21 AM
|
 |
Lost Soul
Super Moderator * Guru *
|
|
Join Date: May 2001
Location: Vorlon
Posts: 18,885
|
|
|
You are using the i to increment the value of SumEven/SumOdd, you should increment with the current value.
|
|

06-14-2012, 05:26 AM
|
|
Newcomer
|
|
Join Date: Jun 2012
Posts: 6
|
|
GOOD ANSWER !! but ....
what do you mean by increment with the current value ? the current value refer to ? ?
I don't really get it  sorry....
I'm too weak 
|
|

06-14-2012, 07:00 AM
|
 |
Lost Soul
Super Moderator * Guru *
|
|
Join Date: May 2001
Location: Vorlon
Posts: 18,885
|
|
|
This is the current value: Val(Mid(n, i, 1))
|
|

06-14-2012, 08:59 AM
|
|
Newcomer
|
|
Join Date: Jun 2012
Posts: 6
|
|
|
WoW ! You're smart and kinded heart , man ~
I finally solved my problem with the help of your guys ~
Thank you for what you guys did;
I’m glad someone like you guys
Could help me to get through it.
THANKS A LOT ~
|
|

06-14-2012, 09:17 AM
|
 |
Bald Mountain Survivor
Super Moderator * Expert *
|
|
Join Date: Aug 2003
Location: Oregon, USA
Posts: 5,882
|
|
With your permission Flyguy.
What Flyguy is getting at is that you are adding your loop variable
to your sum variables. Not the converted string character value.
Below is your code re-written to expose the conversion process.
If you had used debug to step though your original code the problem would have
been pretty much self evident.
BTW Option Explicit is your friend. Start using it at the top of every VB document.
Also Val() is not recommended as it can return any numerical type.
Use Cint() instead if you want to convert a string into an integer.
Code:
Option Explicit
Private Sub cmd1_Click()
Dim n As String, sumOdd As Integer, SumEven As Integer, i As Integer, ans As Integer
Dim IntDigit As Integer ' <--- Define new variable here
SumEven = 0
sumOdd = 0
n = InputBox("Enter a integer value with any number of digits", "Numbers", "Type Here")
For i = 1 To Len(n) Step 1
IntDigit = Cint(Mid(n, i, 1)) ' < --- Set the new variable here.
If IntDigit Mod 2 = 0 Then
SumEven = SumEven +IntDigit ' <--- Add the new variable here
Else
sumOdd = sumOdd + IntDigit ' <--- Or here
End If
Next i
ans = SumEven - sumOdd
MsgBox ans
End Sub
|
__________________
Burn the land and boil the sea
You can't take the sky from me
~T
Last edited by Gruff; 06-14-2012 at 09:31 AM.
|

06-14-2012, 09:37 AM
|
|
Newcomer
|
|
Join Date: Jun 2012
Posts: 6
|
|
|
Woah~ Cool man ~
Another typical method i learned from you guys ~
Thanks a lot ~ This Xtreme VB talk really helped me a lot on my studies ~
Again, THANKS for those who are helping me ^^
* and sorry on my broken english ( my english was not very good =X )
|
|

06-26-2012, 07:23 AM
|
|
Senior Contributor
Forum Leader
|
|
Join Date: Oct 2003
Location: Central Florida
Posts: 1,205
|
|
Also,
Code:
If IntDigit And 1 Then
will be True on odd numbers and False on even numbers.
May or may not be faster, but it looks cooler.
|
__________________
-- D.J.
I do not endorse any items advertised within this frame, and regret that the viewer is subjected to such.
|

06-26-2012, 04:37 PM
|
 |
Sinecure Expert
Super Moderator * Guru *
|
|
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 7,714
|
|
Plus, if you're adding all the even and subtracting all the odd digits, you could just accumulate the result as you go and save a few variables and lines.
Modified Gruff code:
Code:
Dim n As String, i As Integer, ans As Integer
Dim IntDigit As Integer ' <--- Define new variable here
n = InputBox("Enter a integer value with any number of digits", "Numbers", "Type Here")
For i = 1 To Len(n)
IntDigit = CInt(Mid(n, i, 1)) ' < --- Set the new variable here.
ans = IIf(IntDigit And 1, ans - IntDigit, ans + IntDigit) 'subtract the odd digits or add the even digits
Next i
MsgBox ans
Or if you really want to cause some head scratching, replace this line:
ans = IIf(IntDigit And 1, ans - IntDigit, ans + IntDigit) 'subtract the odd digits or add the even digits
with this line:
ans = ans + (1 + (-2 * (IntDigit And 1))) * IntDigit 'subtract the odd digits or add the even digits
|
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Last edited by passel; 06-26-2012 at 04:54 PM.
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|