Can sb help me to correct my vb code? - vba

Dim age As Integer = 1
Do While 1980 + age <> age * age
lblResult.Text = "The solution is " & age & " years old."
age = age + 1
Loop
When I run this program, it will give 44 not 45. Is there anything with it?

The problem is that the code updates lblResult.Text before it increments age, so when age is incremented from 44 to 45, the loop exits without updating the label to show the final value of age.
To fix the code, update lblResult.Text after incrementing age. Although you could do so inside the loop ...
Do While 1980 + age <> age * age
age = age + 1
lblResult.Text = "The solution is " & age & " years old."
Loop
... it suffices to update the label just once, after the loop finishes:
Do While 1980 + age <> age * age
age = age + 1
Loop
lblResult.Text = "The solution is " & age & " years old."

Related

How do I make the results for a invalid option show

need help to make the input of 101 show as invalid instead of A* as an output on a console app from visual studio code
Imports System
Module Program
Sub Main(args As String())
Dim mark As Byte
Dim grade As String
Console.WriteLine("enter a mark out of 100")
mark = Console.ReadLine
If mark <= 39 Then
grade = "u"
ElseIf mark <= 49 Then
grade = ("e")
ElseIf mark <= 59 Then
grade = ("d")
ElseIf mark <= 69 Then
grade = ("C")
ElseIf mark <= 79 Then
grade = ("B")
ElseIf mark <= 89 Then
grade = ("A")
ElseIf mark >= 99 Then
grade = ("A*")
Else
grade = ("invailid")
End If
Console.WriteLine()
Console.WriteLine("Grade is :" & grade)
Console.ReadLine()
End Sub
End Module

I'm getting infinite loop and array is not printing as well

noOfStudent = int(input("Enter Number of Student : "))
while range(noOfStudent):
subjectArray = []
markArray = []
studentName = input("Enter Student Name : ")
noOfSubject = int(input("Eneter Number of Subjects : "))
totalMark = 0
while range(noOfSubject):
subjectName = input("Enter Subject Name : ")
subjectArray.append(subjectName)
mark = int(input("Enter Mark :"))
markArray.append(mark)
break
totalMark = totalMark + mark
average = totalMark / noOfSubject
print(studentName,subjectArray,markArray)
subjectArray.reverse()
markArray.reverse()
print(studentName,subjectArray,markArray)
subjectArray.clear()
markArray.clear()
print(studentName,subjectArray,markArray)
Array is taking entries of the data but studentName is running infinitely
other than that it is not printing the Average as well as marks
it just keep taking entries and nothing else
I want students name with their subjects and marks as well as total of their marks along with avg

age calculation in vb.net coding

net coding and i am really stuck in this basic age calculation. I have created a form field and in a groupbox it should display the age of a person. The biggest problem i am facing with the code is factoring in the month. I have tried all types such as using DateInterval.Month but still no luck. Here MyEntPatient.DOB is the DOB of patient
Dim Years As Integer
Dim BDAY As New DateTime(Now.Year)
BDAY = MyEntPatient.DOB
If (BDAY > Now) Then
Years = DateDiff(DateInterval.Year, MyEntPatient.DOB, Now) - 1
Else
Years = DateDiff(DateInterval.Year, MyEntPatient.DOB, Now)
End If
Me.gpxPatientDetails.Text = " Age:" + Years.ToString()
I think you want to have the difference of the date, I think this example help you:
Dim birthday As New DateTime(12, 12, 2012)
Dim difference As DateTime = DateTime.Now - birthday
Dim years As Integer = difference.Years
If you have problem with bigger/smaller age, because someone doesn't have a birthday this year (born in December for example), you can try this:
Dim bday As New DateTime(2010, 1, 25)
Dim months As Integer = DateDiff(DateInterval.Month, bday, Now)
Dim years As Integer = months / 12
Function AgeCalculator(ByVal FromDate As String, Optional flgyearOnly As Boolean = False) As String '23/11/2017
If Not IsDate(FromDate) Then Return ""
Dim tmpYear As String = "", tmpMonth As String = "", tmpdiff As Integer = 0
Dim tmpAge As String = ""
tmpdiff = DateDiff(DateInterval.Day, CDate(Format2DateMMM(FromDate)), Now)
If tmpdiff <= 0 Then Return ""
If tmpdiff > 0 And tmpdiff <= 29 Then
Return tmpdiff & " Days"
End If
If tmpdiff = 30 Then
Return " 1 Month"
End If
tmpYear = tmpdiff / 365
If InStr(tmpYear, ".") > 0 Then
tmpYear = Microsoft.VisualBasic.Left(tmpYear, InStr(tmpYear, ".") - 1)
End If
If Val(tmpYear) = 0 Then tmpYear = ""
If Val(tmpYear) > 0 Then
tmpAge = tmpYear & " years"
If flgyearOnly Then Return tmpAge
End If
tmpdiff = (tmpdiff - (Val(tmpYear) * 365))
tmpMonth = tmpdiff / 30
If InStr(tmpMonth, ".") > 0 Then
tmpMonth = Microsoft.VisualBasic.Left(tmpMonth, InStr(tmpMonth, ".") - 1)
End If
If Val(tmpMonth) = 0 Then tmpMonth = ""
If tmpMonth > 0 Then
tmpAge &= " " & tmpMonth & " Months"
End If
tmpdiff = (tmpdiff - (Val(tmpMonth) * 30))
If Val(tmpdiff) > 0 Then
tmpAge &= " " & tmpdiff & " Days"
End If
Return tmpAge: End Function
A bit old posting, but the question is not!
Typically the 'age' is the whole years completed:
Dim DOB As Date = DateValue("1970-10-28")
Dim Tday As Date = DateValue("2022-02-21")
Console.WriteLine(
Int((CInt(Tday.ToString("yyyyMMdd")) - CInt(DOB.ToString("yyyyMMdd"))) / 10000)
)

Grade Average using While or Until Loop

I am taking an online Visual Basic programming 1 course with very little instruction. I have spent hours on every assignment and can usually find help by looking things up and testing. I have not been able to find help for this (or I find it in another language and I don't know how to translate into VB console). I am asking the user to input test scores. My problem is I don't know how many scores that they will input. I need this number so that I can calculate an average and return a grade.
Module Module1
Sub Main()
Dim testScore As Integer = 1
Dim scoreSum As Integer = 0
Dim numberOfScore As Integer
Dim average As Integer
Console.WriteLine("Enter all the test scores. When you are done enter 0. ")
Do While (testScore <> 0)
testScore = Console.ReadLine()
scoreSum = scoreSum + testScore
numberOfScore += testScore
average = scoreSum / numberOfScore
Console.Write(scoreSum.ToString)
Loop
Console.WriteLine("The average is " + (scoreSum / numberOfScore).ToString())
If (average >= 90) Then
Console.WriteLine("Your Grade is A ")
ElseIf (average >= 80) Then
Console.WriteLine("Your Grade is B ")
ElseIf (average >= 70) Then
Console.WriteLine("Your Grade is C ")
ElseIf (average >= 60) Then
Console.WriteLine("Your Grade is D ")
Else
Console.WriteLine("Your Grade is F. " & "You will have to repeat this course. ")
End If
Console.ReadLine()
End Sub
End Module
You need to increment numberOfScore by 1 instead of by testScore. Average would be the sum over number of scores.
Sub Main()
Dim testScore As Integer = 1
Dim scoreSum As Integer = 0
Dim numberOfScore As Integer
Dim average As Integer
Console.WriteLine("Enter all the test scores. When you are done enter 0. ")
testScore = Console.ReadLine()
Do
scoreSum = scoreSum + testScore
numberOfScore += 1
average = scoreSum / numberOfScore
Console.Write(scoreSum.ToString)
testScore = Console.ReadLine()
Loop While (testScore <> 0)
Console.WriteLine("The average is " + (scoreSum / numberOfScore).ToString())
If (average >= 90) Then
Console.WriteLine("Your Grade is A ")
ElseIf (average >= 80) Then
Console.WriteLine("Your Grade is B ")
ElseIf (average >= 70) Then
Console.WriteLine("Your Grade is C ")
ElseIf (average >= 60) Then
Console.WriteLine("Your Grade is D ")
Else
Console.WriteLine("Your Grade is F. " & "You will have to repeat this course. ")
End If
Console.ReadLine()
End Sub

compute age from given birthdate

I have 2 comboboxes and 2 textboxes. My first combobox contains months in this format january, february, etc, and the other combobox contains numbers from 1 to 31. My first textbox is txtyear. Once the user input birth year to txtyear a variable BOD will be equals to this.
Dim BOD as string
BOD = cbomonth.text + "-" + cboday.text + "-" + txtyear.text
The purpose of my last textbox is to handle the age of the user that will be computed when the cursor lost focus on txtyear.
Can anyone help how to compute the age.
There are really two questions here:
How to convert the string input into a DateTime object
How to calculate age once you have your data in the correct format.
I'll let you follow other's instructions for how use TryParseExtract which is definitely the correct way to go here.
When determining someone's age from their DOB, try using this:
Public Function GetCurrentAge(ByVal dob As Date) As Integer
Dim age As Integer
age = Today.Year - dob.Year
If (dob > Today.AddYears(-age)) Then age -= 1
Return age
End Function
It is the vb version of the top answers on Jeff Atwood's very popular question How do I calculate someone's age
I wrote a blogpost about calculating age from dob as well.
Here's a little different way using the year and month properties of the Date class:
Dim BOD as string
BOD = cbomonth.text + "-" + cboday.text + "-" + txtyear.text
Dim dt As Date
If Date.TryParseExact(BOD, "MMMM-dd-yyyy", Nothing, Globalization.DateTimeStyles.None, dt) Then
Dim Age As New Date(Now.Subtract(dt).Ticks)
MsgBox(String.Format("Your age is : {0} Years and {1} Months", Age.Year - 1, Age.Month - 1))
Else
MsgBox("Birth Date is in wrong format")
End If
Here's a technique when you use Visual Studio 2012
VB.NET language
Private Sub dtpBOfD_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtpBOfD.ValueChanged
lblAge.Text = Age(dtpBOfD.Value)
End Sub
Public Shared Function Age(DOfB As Object) As String
If (Month(Date.Today) * 100) + Date.Today.Day >= (Month(DOfB) * 100) + DOfB.Day Then
Return DateDiff(DateInterval.Year, DOfB, Date.Today)
Else
Return DateDiff(DateInterval.Year, DOfB, Date.Today) - 1
End If
End Function
Use this function
Function String2Date(ByVal sDay As String, ByVal sMonth as String, ByVal sYear as String) As Date
StdDateString = sMonth & " " & sDay & ", " & sYear
End Function
And apply it ..
Dim dt1 as Date = String2Date(ComboBox2.Text,ComboBox1.Text,txtYear.Text).ToShortDateString
Dim dt2 as Date = Now.ToShortDateString
Dim dt3 as TimeSpan = (dt2 - dt1)
Dim diff as Double = dt3.Days
Dim sAge as String
sAge = Str(Int(diff / 365)) & " Year "
diff = diff Mod 365
sAge = sAge & Str(Int(diff / 30)) & " Month(s)"
diff = diff Mod 30
sAge = sAge & Str(diff) & " Day(s)"
txtAge.Text = sAge
for complete age information use this code in c#.`
public string calculateDays(int day, int Month, int year)
{
int Diffyear;
int DiffMonth;
int DiffDay;
int cuYear=DateTime.Now.Year;
int cuMonth=DateTime.Now.Month;
int cuDay=DateTime.Now.Day;
string Age;
Diffyear= cuYear-year;
DiffMonth=cuMonth-Month;
DiffDay=cuDay-day;
if ((DiffMonth) < 0)
{
Diffyear -= 1;
}
if ((DiffDay) < 0)
{
DiffMonth -= 1;
if ((cuMonth - 1) < 8)
{
if (((cuMonth - 1) % 2) == 0)
{
if ((cuMonth - 1) == 2)
if (cuYear % 4 == 0)
{
DiffDay = 29 + DiffDay;
}
else
{
DiffDay = 28 + DiffDay;
}
else
DiffDay = 30 + DiffDay;
}
else
DiffDay = 31 + DiffDay;
}
else
{
if (((cuMonth - 1) % 2) == 0)
{
DiffDay = 31 + DiffDay;
}
else
{
DiffDay = 30 + DiffDay;
}
}
}
if ((DiffMonth) < 0)
{
DiffMonth = DiffMonth+12;
}
if (Diffyear < 0)
{
Diffyear = Diffyear * (-1);
}
if ((DiffDay) < 0)
{
DiffDay = DiffDay * (-1);
}
Age = "Age: " + Diffyear.ToString() + " year, " + DiffMonth.ToString() + " months, " + DiffDay.ToString() + " days";
return Age;
}
`
Dim d1 As Date
Dim d2 As Date
d1 = Format(dob.Value, "yyyy/MM/dd"
d2 = Format(System.DateTime.Now, "yyyy/MM/dd")
d = DateDiff(DateInterval.Year, d1, d2)'d-1 provides accurate age