N/APosted on - 04/03/2012
I’m an information technology student and our professor asked us to create a program using visual basic 6.0 that would determine if a number is a even or odd. In addition, if the input is not a whole number or a letter, the program should return an error stating "Please Enter a number!" and if the number is even or odd the program should return a message stating "the number x(x=user input number) is even" or "the number x(x=user input number) is odd".
I’m a little clueless right now and would like some help on this.Thank you so much.
I need some help with visual basic 6.0 programming
Private sub cmdOK_click()
Dim i as integer
// check whether number is a character
If val(txtinput.text)=0 then // val will return 0 for any non-n numeric numbers
Msgbox(“not valid number”)
//check for whole number
i=val(txtinput.text) //i will take only the integer part of the number.
If i<>val(txtinput.text) then // if the integer part is not equal to entire number
Msgbox(“Not a whole number”)
If i Mod 2 == 0 then // If the remainder after division of the number by 2 is zero then
msgbox(“Number is even”)
else // The number is odd and the division remainder is not zero.
msgbox(“Number is Odd”)
You get to validate the data entered first. Check a valid numeric, check whether a whole number.
Then only do specify whether it is even or odd.
Val(<string>) is a function in V B that allows to convert a string representation of a number to integer format. And returns zero if the string is not a numeric. We use this. If the returned value is 0 then the number entered is not a valid number.
The whole number is such a number that has no fractional part. Hence, we check in the following way.
A double variable will hold the integer and fractional part together. However an integer variable will have only the integer part and excludes the factional part. Thus after assigning the double to integer, if both are not equal, we conclude that it is not a whole number. AT last, if the number given is perfectly divisible by 2, then we say it is even, else, it is odd. Thanks.