Percentage yielding 0.00 - vb.net

In my program to calculate grades, I need to find the percentage needed on the final to get a certain grade.
I calculate my variable PNA which is points needed for an A.
I do 900 - GSF (which is grade so far)
for a perfect score, I come back with 50 points needed.
Then, I do my variable PeNA, which is percentage needed on the final.
So I did multiple things:
PeNA = PNA / 150
and
PeNA = (900-GSF) / 150
Both results yielded 0.00%
I know that the result should be 33.33% but to no avail.
My question is how to format that PeNA variable and Statement to come back with the correct percent needed.
I think I might have a logic error somewhere in here. But I am not sure as to what is wrong.
Once I figure this out the project should be done.
Here is my code.
Sub Main()
Dim TG1 As Integer = -1
While TG1 < 0 Or TG1 > 200
Console.WriteLine("Please enter your grade for project #1 <0-200>: ")
TG1 = Console.ReadLine()
End While
Dim TG2 As Integer = -1
While TG2 < 0 Or TG2 > 200
Console.WriteLine("Please enter your grade for project #2 <0-200>: ")
TG2 = Console.ReadLine()
End While
Dim TG3 As Integer = -1
While TG3 < 0 Or TG3 > 200
Console.WriteLine("Please enter your grade for project #3 <0-200>: ")
TG3 = Console.ReadLine()
End While
Dim MT1 As Integer = -1
While MT1 < 0 Or MT1 > 150
Console.WriteLine("Please enter your grade on the midterm <0-150>: ")
MT1 = Console.ReadLine()
End While
Dim AB1 As Integer = -1
While AB1 < 0 Or AB1 > 30
Console.WriteLine("Please enter how much you were absent <0-30>: ")
AB1 = Console.ReadLine()
End While
Dim AB2 As Integer
If AB1 <= 2 Then AB2 = 100
If AB1 > 2 AndAlso AB1 < 8 Then AB2 = 100 - ((AB1 - 2) * 20)
If AB1 > 7 Then AB2 = 0
Dim GSF As Integer
GSF = TG1 + TG2 + TG3 + MT1 + AB2
Dim PNA As Integer
PNA = 900 - GSF
Dim PeNA As Integer
PeNA = (900 - GSF) / 150
Console.WriteLine("------------------------------------------------")
Console.WriteLine("--GRADE SUMMARY--")
Console.WriteLine("Project #1 : {0}", TG1)
Console.WriteLine("Project #2 : {0}", TG2)
Console.WriteLine("Project #3 : {0}", TG3)
Console.WriteLine("Midterm Exam : {0}", MT1)
Console.WriteLine("Participation : {0}", AB2)
Console.WriteLine("-------------------------------------------------")
Console.WriteLine("Total grade so far: {0}", GSF)
Console.WriteLine("")
Console.WriteLine("{0} {1} {2}", "Desired grade".PadRight(20), "Points needed".PadRight(20), "Percentage needed on final")
Console.WriteLine("{0} {1} {2}", "A".PadRight(20), PNA.ToString.PadRight(20), FormatPercent(PeNA).ToString.PadRight(20))
'A - 900-1000
'B - 800-899
'C - 700-799
'D - 600-699

Dim PNA As Integer
PNA = 900 - GSF
Dim PeNA As Integer
PeNA = (900 - GSF) / 150
PeNA should be declared SINGLE, DOUBLE or DECIMAL if you want it to store a fraction

Related

Visual basics help-Variables in for loop

I'm trying to get the integers of "num3"(input from the user) and squaring each character and eventually see if its a "happy number"=1 or a "unhappy number" includes 4
For example, 19 is happy number 1^2+9^2=82 , 8^2+2^2=68,6^2+8^2=100, 1^2+0+0=1
Sub Main()
Dim number, num2, total As Integer
Dim num3 As String
Console.Write("pick a number:")
number = Console.ReadLine()
num2 = 0
num3 = Convert.ToString(number)
Do Until total = 1 Or num2 = 4
For Each num3 In num3.Substring(0, num3.Length)
For counter = 1 To num3.Length And num3 = num3.Substring(0, num3.Length)
num2 = num3 ^ 2
total = total + num2
Console.WriteLine(total)
Next
num3 = total
Next
Loop
Console.ReadLine()
If total = 1 Then
Console.WriteLine("happy number")
ElseIf num2 = 4 Then
Console.WriteLine(number & "is a unhappy number")
Console.ReadLine()
End If
End Sub
End Module
however i'm stuck whether "num3" replace itself from the for loop
Here, I believe this should work:
Dim InputNum As Integer = 45 'Your input number
Dim TempNum As Integer = InputNum 'Here we hold number temporarily during steps 19>82....
Do
Dim SumTotal As Double = 0 'sum of numbers squares
For Each number As Char In TempNum.ToString
SumTotal += Integer.Parse(number) ^ 2
Next
TempNum = CInt(SumTotal)
Debug.Print(TempNum.ToString)
'If result is 4 it will loop forever
If SumTotal = 4 Then
Console.WriteLine(InputNum & " is an unhappy number")
Exit Do
ElseIf SumTotal = 1 Then
Console.WriteLine(InputNum & " is a happy number")
Exit Do
End If
Loop

How to count and output total number of loops in visual basic console application?

I am learning visual basic and wrote a simple visual basic console application to do "half or triple plus one" calculations and the console app works but I would also like to count and display the total number of loops run to get to the solution. Here is my code:
Sub Main()
Dim n As Double
Console.WriteLine("Enter a starting number.")
n = Console.ReadLine()
Do While n <> 1
Do While n > 1
If n Mod 2 = 0 Then
n = n / 2
Else
n = (n * 3) + 1
End If
Console.WriteLine(n)
Loop
If n = 1 Then
Console.WriteLine(
n = Console.ReadLine()
End If
Loop
Console.ReadLine()
End Sub
I can't seem to find anything that counts and displays the number of loops.
If I'm not misunderstanding you, The problem solves Introducing a new variable named j, and the final code results in
Sub Main()
Dim n As Double
Dim j as integer
Console.WriteLine("Enter a starting number.")
n = Console.ReadLine()
j = 0
Do While n <> 1
Do While n > 1
'Every Time i enter in the cicle, the "operation counter" increses by one
j = j + 1
If n Mod 2 = 0 Then
n = n / 2
Else
n = (n * 3) + 1
End If
Console.WriteLine(n)
Loop
If n = 1 Then
Console.WriteLine(
n = Console.ReadLine()
End If
Loop
Console.WriteLine("I make " & j & " operations to get here!")
Console.ReadLine()
End Sub
Sub Main()
Dim n As Double
Console.Write("Enter a starting number: ")
n = Convert.ToDouble(Console.ReadLine())
Console.WriteLine()
Dim loopsCounter As Integer = 0
Do While n > 0
Do While n > 1
loopsCounter += 1
If n Mod 2 = 0 Then
n = n / 2
Else
n = (n * 3) + 1
End If
Console.WriteLine(n)
Loop
Console.WriteLine()
Console.WriteLine(String.Format("{0} loops to result.", loopsCounter))
If n = 1 Then
Console.WriteLine()
Console.Write("Enter another number: ")
n = Convert.ToDouble(Console.ReadLine())
loopsCounter = 0
End If
Loop
Console.ReadLine()
End Sub

Having trouble with Console.Writeline

Can someone explain why the below code won't work? It's supposed to find the average in a group of 15 numbers, then assign a letter grade to them. From what I can tell, the problem is in the If statement, although I can't seem to find what it is that causes the problem.
Module Module1
Sub Main()
Dim Input1 As Integer
Dim Input2 As Integer
Dim Input3 As Integer
Dim Input4 As Integer
Dim Input5 As Integer
Dim Input6 As Integer
Dim Input7 As Integer
Dim Input8 As Integer
Dim Input9 As Integer
Dim Input10 As Integer
Dim Input11 As Integer
Dim Input12 As Integer
Dim Input13 As Integer
Dim Input14 As Integer
Dim Input15 As Integer
Dim Adder As Integer
Dim Divider As Integer
Console.WriteLine("Please enter number 1/15")
Input1 = Console.ReadLine()
Console.WriteLine("Please enter number 2/15")
Input2 = Console.ReadLine()
Console.WriteLine("Please enter number 3/15")
Input3 = Console.ReadLine()
Console.WriteLine("Please enter number 4/15")
Input4 = Console.ReadLine()
Console.WriteLine("Please enter number 5/15")
Input5 = Console.ReadLine()
Console.WriteLine("Please enter number 6/15")
Input6 = Console.ReadLine()
Console.WriteLine("Please enter number 7/15")
Input7 = Console.ReadLine()
Console.WriteLine("Please enter number 8/15")
Input8 = Console.ReadLine()
Console.WriteLine("Please enter number 9/15")
Input9 = Console.ReadLine()
Console.WriteLine("Please enter number 10/15")
Input10 = Console.ReadLine()
Console.WriteLine("Please enter number 11/15")
Input11 = Console.ReadLine()
Console.WriteLine("Please enter number 12/15")
Input12 = Console.ReadLine()
Console.WriteLine("Please enter number 13/15")
Input13 = Console.ReadLine()
Console.WriteLine("Please enter number 14/15")
Input14 = Console.ReadLine()
Console.WriteLine("Please enter number 15/15")
Input15 = Console.ReadLine()
Adder = (Input1) + (Input2) + (Input3) + (Input4) + (Input5) + (Input6) + (Input7) + (Input8) + (Input9) + (Input10) + (Input11) + (Input12) + (Input13) + (Input14) + (Input15)
Divider = Adder / 15
If 100 >= Divider >= 90 Then
Console.WriteLine("Your average Grade is an A, and your average in numbers is {0}", Divider)
ElseIf 89 >= Divider >= 80 Then
Console.WriteLine("Your average Grade is a B, and your average in numbers is {0}", Divider)
ElseIf 79 >= Divider >= 70 Then
Console.WriteLine("Your average Grade is a C, and your average in numbers is {0}", Divider)
ElseIf 69 >= Divider >= 60 Then
Console.WriteLine("Your average Grade is a D, and your average in numbers is {0}", Divider)
ElseIf 59 >= Divider Then
Console.WriteLine("Your average Grade is an F, and your average in numbers is {0}", Divider)
End If
Console.ReadLine()
End Sub
End Module
You can't do the following:
If 100 >= Divider >= 90
I believe that is interpreted to be something like
If (100 >= Divider) >= 90
Where it is comparing a boolean converted to a integer.
To get a check for that range you would need to do:
If (100 >= Divider) AndAlso (Divider >= 90)
In short you have bad syntax with your If Check
As an added note, I would reccomend taking advantage of the strength of conditional logic by skipping the range checks and doing something like:
If Divider >= 90 Then
Console.WriteLine("Your average Grade is an A, and your average in numbers is {0}", Divider)
ElseIf Divider >= 80 Then
Console.WriteLine("Your average Grade is a B, and your average in numbers is {0}", Divider)
ElseIf Divider >= 70 Then
Console.WriteLine("Your average Grade is a C, and your average in numbers is {0}", Divider)
ElseIf Divider >= 60 Then
Console.WriteLine("Your average Grade is a D, and your average in numbers is {0}", Divider)
Else
Console.WriteLine("Your average Grade is an F, and your average in numbers is {0}", Divider)
End If
You made an error in reasoning.
The Divider in your case has a Maximum value of 15. so it's obviously always an F Grade.
If Adder >= 90 Then
Console.WriteLine("Your average Grade is an A, and your average in numbers is {0}", Divider)
ElseIf Adder >= 80 Then
Console.WriteLine("Your average Grade is a B, and your average in numbers is {0}", Divider)
ElseIf Adder >= 70 Then
Console.WriteLine("Your average Grade is a C, and your average in numbers is {0}", Divider)
ElseIf Adder >= 60 Then
Console.WriteLine("Your average Grade is a D, and your average in numbers is {0}", Divider)
ElseIf Adder < 60 Then
Console.WriteLine("Your average Grade is an F, and your average in numbers is {0}", Divider)
End If
Here you go. I just switched the Divider with Adder and erased some non-essentiel stuff.
Hope this is what you are looking for.

How to Loop through a If condition untill the user enter a required value, in Visual Basic

Console.WriteLine("Please Enter the number")
Dim number As Integer = Console.ReadLine()
If (number=< 40) Then
number = number* 10
ElseIf (number=< 150) Then
number= number* 15
Else
number= number* 26
End If
Console.WriteLine(number)
Dim total As Integer
Dim vALUE As Integer
Console.WriteLine("Please, type 1 for x . Type 2 for y. Type 3 z")
vALUE = Console.ReadLine()
If vALUE = 1 Then
Console.WriteLine("x")
total = number* (106 / 100)
ElseIf vALUE = 2
Console.WriteLine("y")
total = number* (112 / 100)
ElseIf uSERvALUE = 3
Console.WriteLine("z")
total = number* (116 / 100)
Else
Console.WriteLine("Sorry please re-enter the value")
vALUE = Nothing
End If
End While
Please tell me how to repeat the if condition. In the else line I have displayed to the user to re-enter the value. Therefore I need to repeat the if condition until the value is 1 or 2 or 3.Please explain how to do I'm a newbie.
You can use infinite While-loop ,and put "Exit While" on the end of conditional statement you agreed , so there are few ways out from infinite loop.
For example , on your code:
Console.WriteLine("Please Enter the number")
Dim number As Integer = Console.ReadLine()
If (number=< 40) Then
number = number* 10
ElseIf (number=< 150) Then
number= number* 15
Else
number= number* 26
End If
Console.WriteLine(number)
Dim total As Integer
Dim vALUE As Integer
'infinite loop until user input 1,2 or 3
While True
Console.WriteLine("Please, type 1 for x . Type 2 for y. Type 3 z")
vALUE = Console.ReadLine()
If vALUE = 1 Then
Console.WriteLine("x")
total = number* (106 / 100)
Exit While 'condition matched , break from While
ElseIf vALUE = 2
Console.WriteLine("y")
total = number* (112 / 100)
Exit While 'condition matched , break from While
ElseIf uSERvALUE = 3
Console.WriteLine("z")
total = number* (116 / 100)
Exit While 'condition matched , break from While
Else
Console.WriteLine("Sorry please re-enter the value")
vALUE = Nothing
End If
End While
'do further more you need

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