Saturday, May 30, 2009

Prime Number CODE

Module Module1
Sub Main()
'Dim y As Integer = 10
'While y > 0
' Console.WriteLine("Happy Birthday to you!")
' y = y - 1
'End While
'Dim x As Integer = 0
'While x < 100
' If x Mod 2 = 0 Then 'Mod keeps dividing until there is a remainder
' Console.WriteLine(x)
' End If
' x += 1
'End While
'Console.WriteLine("I have printed out all the " & _
' " even numbers less than 100")
'I want you to print out all the numbers from 5 to 5000
'counting by 5s!
'Count from 3333 to 3 by 3s, going backwards.
Dim counter1 As Integer = 1
Dim stop1 As Integer = 100
'For counter1 = 1 To stop1
' Console.Write("Hello!")
' For a As Integer = 1 To 1000
' Console.Write("Hello!!")
' Next
'Next
'A number is prime if it is only divisible by 1 and itself
'3,5,7...prime numbers never stop
'find out if a number is prime!
'have the user enter in a number..find out if it is a
'prime number ...hint***: You'll need the Mod operator
Dim boolVar1 As Boolean = True
'print out all the prime numbers up to 100!
'Console.WriteLine("Please enter a number")
'Dim numberEntered As Integer = Console.ReadLine
For firstCounter As Integer = 1 To 100
For a As Integer = 2 To (firstCounter / 2) + 1
If firstCounter Mod a = 0 Then
boolVar1 = False
End If
Next
If boolVar1 = True Then
Console.WriteLine(firstCounter & " is a prime number")
End If
boolVar1 = True
Next
Console.ReadLine()
End Sub
End Module

Saturday, May 16, 2009

What did I learn today?

The first VB.NET class I learned...

Here is the code from the first class.

Code from GUI NumberGuesser

Public Class Form1

Private Sub btnGuess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGuess.Click
Dim numberGuessed As Integer = txtNumber.Text
Dim luckyNumber As Integer = 77

If Math.Abs(luckyNumber - numberGuessed) < 10 Then
MsgBox("You are within 10!")
End If
End Sub
End Class

functions

Module Module1

Sub Main()
'do some procedural programming

'user enters a number and gets back the number squared
Console.WriteLine("Please enter a number")
Dim a As Integer = Console.ReadLine
Dim newNumber = a * a 'squared the number
Dim newNumber2 = a ^ 2
Dim newNumber3 = Math.Pow(a, 2)

Console.WriteLine(newNumber)
Console.WriteLine(newNumber2)
Console.WriteLine(newNumber3)


'ask the user for the side of a square. give the user back the perimeter and area.
Console.WriteLine("Please enter the side of a square:")
Dim sideOfSquare As Decimal = Console.ReadLine
Console.WriteLine("The area is " & calculateArea(sideOfSquare))
Console.WriteLine("The perimeter is " & calculatePerimeter(sideOfSquare))
Console.WriteLine("The area of a circle with a radius of 10 is " & calculateAreaOfCircle(10.0))

Console.ReadLine()
End Sub

Function calculatePerimeter(ByVal side As Decimal) As Decimal
Return side * 4
End Function

Function calculateArea(ByVal side As Decimal) As Decimal
Return side * side
End Function

'define a function that calculates the area of a circle given the radius!
Function calculateAreaOfCircle(ByVal radius As Decimal) As Decimal
'pi* r^2
Return Math.PI * (radius ^ 2)
End Function

End Module

Create a blog

  1. Go to www.blogger.com
  2. Create a blog, choose the name you want
  3. First post will be about:
    What is object oriented programming?
    What is a class?
    What is an object?
    What is an attribute?
  4. Publish your blog.
  5. Leave a comment on this blog with your URL!

First VB project

'you will ask the user for the distance they've driven in miles

'you will ask the user for the amount of gas they used

'give the user the miles per gallon of their vehicle!

Code from first example

Module Module1

Sub Main()
Console.WriteLine("Hello World!")
Console.WriteLine("Please enter your name:")
Dim userName As String = Console.ReadLine()
'declaring a variable to hold a String!
Console.WriteLine("Welcome " & userName & " we are glad you are here!")
Console.WriteLine("How many years have you lived?")
Dim ageOfUser As Integer = Console.ReadLine()
Dim userInEightYears As Integer = ageOfUser + 8
Console.WriteLine("In eight years you will be " & userInEightYears & " old")
Console.WriteLine("Someone half your age is " & ageOfUser / 2 & " years old")
Console.WriteLine("Someone twice your age is " & ageOfUser * 2 & " years old")
If ageOfUser > 10 Then
Console.WriteLine("The secret to life is do things you enjoy!")
Else
Console.WriteLine("You already know the secret!")
End If
Console.ReadLine()
'this is conditional logic
End Sub

End Module

Second assignment!

'Making a guessing game that asks the user for a number
'if the number is less than 10, tell them it is bad luck
'if the number is 12, tell them the number is okay
'if the number is 101, tell them they are great people for guessing such a number
'else say thanks for your time