Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > How to Determine if a integer with any digits is A Even or Odd Number ?


Reply
 
Thread Tools Display Modes
  #1  
Old 06-13-2012, 11:10 AM
Xdirect Xdirect is offline
Newcomer
 
Join Date: Jun 2012
Posts: 6
Question 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 ~
Reply With Quote
  #2  
Old 06-13-2012, 12:49 PM
bpd bpd is offline
Centurion
 
Join Date: Jul 2003
Posts: 102
Default

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."
Reply With Quote
  #3  
Old 06-13-2012, 03:35 PM
snarfblam's Avatar
snarfblam snarfblam is offline
Senior Contributor

Forum Leader
* Expert *
 
Join Date: Apr 2005
Location: USA
Posts: 866
Default

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 _
Reply With Quote
  #4  
Old 06-14-2012, 12:56 AM
Xdirect Xdirect is offline
Newcomer
 
Join Date: Jun 2012
Posts: 6
Default

STILL ~ i failed =.= dont really understand how its work and how to do
i'm too new in vb
Reply With Quote
  #5  
Old 06-14-2012, 01:52 AM
Flyguy's Avatar
Flyguy Flyguy is offline
Lost Soul

Super Moderator
* Guru *
 
Join Date: May 2001
Location: Vorlon
Posts: 18,885
Default

Show what you have done so far. We are not going to do your homework for you!
Reply With Quote
  #6  
Old 06-14-2012, 05:18 AM
Xdirect Xdirect is offline
Newcomer
 
Join Date: Jun 2012
Posts: 6
Default

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 ???
Reply With Quote
  #7  
Old 06-14-2012, 05:21 AM
Flyguy's Avatar
Flyguy Flyguy is offline
Lost Soul

Super Moderator
* Guru *
 
Join Date: May 2001
Location: Vorlon
Posts: 18,885
Default

You are using the i to increment the value of SumEven/SumOdd, you should increment with the current value.
Reply With Quote
  #8  
Old 06-14-2012, 05:26 AM
Xdirect Xdirect is offline
Newcomer
 
Join Date: Jun 2012
Posts: 6
Default

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
Reply With Quote
  #9  
Old 06-14-2012, 07:00 AM
Flyguy's Avatar
Flyguy Flyguy is offline
Lost Soul

Super Moderator
* Guru *
 
Join Date: May 2001
Location: Vorlon
Posts: 18,885
Default

This is the current value: Val(Mid(n, i, 1))
Reply With Quote
  #10  
Old 06-14-2012, 08:59 AM
Xdirect Xdirect is offline
Newcomer
 
Join Date: Jun 2012
Posts: 6
Default

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 ~
Reply With Quote
  #11  
Old 06-14-2012, 09:17 AM
Gruff's Avatar
Gruff Gruff is offline
Bald Mountain Survivor

Super Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA
Posts: 5,882
Default

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.
Reply With Quote
  #12  
Old 06-14-2012, 09:37 AM
Xdirect Xdirect is offline
Newcomer
 
Join Date: Jun 2012
Posts: 6
Default

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 )
Reply With Quote
  #13  
Old 06-26-2012, 07:23 AM
ElderKnight ElderKnight is offline
Senior Contributor

Forum Leader
 
Join Date: Oct 2003
Location: Central Florida
Posts: 1,205
Default

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.
Reply With Quote
  #14  
Old 06-26-2012, 04:37 PM
passel's Avatar
passel passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 7,714
Default

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.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->