Вы находитесь на странице: 1из 26

1|Page

Q1. Write a Program to Generate Prime Numbers till n numbers where n is entered by the user

'Program Name: Find all Primes from given Range Option Explicit Private Sub cmdFind_Click() If Me.txtStartRange.Text <> " " And Me.txtEndRange.Text <> " " Then FindPrimeInRange CInt(txtStartRange.Text), CInt(txtEndRange.Text) Else MsgBox "Invalid Range", vbExclamation, "Invalid Range" End If End Sub Private Sub txtEndRange_Validate(KeepFocus As Boolean) If Not IsNumeric(Me.txtEndRange.Text) Then KeepFocus = True MsgBox "Please enter only Numeric Values", vbInformation + vbDefaultButton1, _ "Invalid Input" End If End Sub Private Sub txtStartRange_Validate(KeepFocus As Boolean) If Not IsNumeric(Me.txtStartRange.Text) Then KeepFocus = True MsgBox "Please enter only Numeric Values", vbInformation + vbDefaultButton1, _ "Invalid Input" End If End Sub Private Sub FindPrimeInRange(number1 As Integer, number2 As Integer) Dim iCtr As Integer Dim i As Integer i = number1 If number1 > number2 Then MsgBox "Start Range cannot be greater than End Range", vbInformation, "Invalid Range"

2|Page
Me.txtStartRange.Text = " " Me.txtEndRange.Text = " " Me.txtStartRange.SetFocus Exit Sub End If If number1 = number2 Then If isPrime(number1) = True Then txtOutput.Text = "Number " & number1 & " is PRIME" Exit Sub Else txtOutput.Text = number1 & " is NOT PRIME" Exit Sub End If End If txtOutput.Text = "Primes : " For i = number1 To number2 Step 1 If isPrime(i) = True Then txtOutput.Text = txtOutput.Text & i & " " End If Next i End Sub ' Function to Test wether given number is Prime or Not Private Function isPrime(ByVal value As Integer) As Boolean Dim i As Integer Dim flag As Boolean flag = False If value = 0 Then flag = False: GoTo decision If value = 1 Then flag = True: GoTo decision For i = 2 To value / 2 Step 1 'value - 1 Step 1 If value Mod i = 0 Then flag = False Exit For Else flag = True End If Next decision: If flag = False Then isPrime = False Else isPrime = True End If End Function

Q2. Write a program to calculate sum of square of N natural numbers like 12 + 22 = 5

' Description: Program to print sum of squares Private Sub cmdSumOfSquares_Click() SumOfSquare (CDbl(Me.txtInput.Text)) End Sub Private Sub txtInput_KeyPress(KeyAscii As Integer) If KeyAscii = 8 Then Exit Sub If KeyAscii < 49 Or KeyAscii > 57 Then KeyAscii = 0 End If End Sub Private Dim Dim Dim Sub SumOfSquare(ByVal number As Integer) sum As Long i As Integer count As Integer

i = 1 ' for natural numbers While (i <= number) sum = sum + MyPow(CLng(i), 2) i = i + 1 Wend Me.txtOutput.Text = sum End Sub Private Function MyPow(ByVal number As Long, ByVal power As Long) As Long Dim result As Long Dim i As Integer If power = 0 Then result = 1: GoTo Label1 result = 1 For i = 1 To power Step 1 result = result * number Next i Label1: MyPow = result End Function

3. Write a program to accept a number from the user and find out the single digit total for that number.

' Description: To print the single digit total of the number Private Sub cmdClick_Click() Me.txtOutput.Text = CDbl(SumOfDigits(Me.txtInput.Text)) End Sub Private Sub txtInput_Validate(KeepFocus As Boolean) If Not IsNumeric(Me.txtInput.Text) Then KeepFocus = True MsgBox "Please insert only Numeric values", vbExclamation + vbDefaultButton1, "Invalid Input" End If End Sub Private Dim Dim Dim Function SumOfDigits(ByVal iNumber As Double) As Double iRemainder As Integer iQuotient As Long iSum As Long

If (iNumber > 0 And iNumber <= 9) Then SumOfDigits = iNumber Else Do iRemainder = iNumber Mod 10 iQuotient = iNumber \ 10 iSum = iSum + iRemainder iNumber = iQuotient Loop While (iNumber <> 0) SumOfDigits = SumOfDigits(iSum) End If End Function

Q4. Write a program to sort n numbers in Ascending and Descending orders.

' Description: Sort n numbers in Ascending and Descending Option Explicit Dim ArrList() As Integer Dim ListCount As Integer Dim flag As Boolean Private Sub cmdAddNumbers_Click() If Me.txtInput.Text <> "" Then lstInput.AddItem (Me.txtInput.Text) Me.lblNumberOfElements.Caption = Me.lstInput.ListCount Me.txtInput.Text = "" Me.txtInput.SetFocus End If End Sub Private Sub cmdAscSort_Click() flag = True

AddDataInArray ' sub proc to Add elements to array Me.lstOutput.Clear For i = 0 To Me.lstInput.ListCount - 1 Step 1 Me.lstOutput.AddItem (CStr(ArrList(i))) Next i End Sub Private Sub cmdDscSort_Click() flag = False ' sort in descending AddDataInArray ' sub proc to Add elements to array Me.lstOutput.Clear For i = 0 To Me.lstInput.ListCount - 1 Step 1 Me.lstOutput.AddItem (CStr(ArrList(i))) Next i End Sub Private Sub txtInput_KeyPress(KeyAscii As Integer) If KeyAscii = 8 Then Exit Sub If KeyAscii < 47 Or KeyAscii > 57 Then KeyAscii = 0 End If End Sub Private Sub AddDataInArray() Dim i As Integer ReDim ArrList(Me.lstInput.ListCount) For i = 0 To Me.lstInput.ListCount - 1 Step 1 ArrList(i) = CInt(Me.lstInput.List(i)) Next i ArrList = MySort(ArrList, flag) End Sub Private Function MySort(arr() As Integer, Optional SortType As Boolean) As Integer() Dim i As Integer Dim j As Integer Dim length As Integer length = Me.lstInput.ListCount - 1 For i = 0 To length - 1 Step 1 For j = i + 1 To length Step 1 If SortType = True Then If arr(i) > arr(j) Then Swap arr(i), arr(j) End If Else If (arr(i) < arr(j)) Then Swap arr(i), arr(j) End If End If Next j Next i MySort = arr End Function

Private Function Swap(number1 As Integer, number2 As Integer) Dim temp As Integer temp = number1 number1 = number2 number2 = temp End Function

Q6. Write a program to find out the factorial of n number using recursive function

' Description: Factorial of a Number Private Function factorial(ByVal number As Double) As Double Dim temp As Long If (number = 1) Then factorial = number Else factorial = number * factorial(number - 1) End If factorial = number End Function Private Sub cmdFactorial_Click() Me.txtOutput.Text = factorial(CLng(Me.txtInput.Text)) End Sub

Q9. Write a program to Calculate Compound Interest

' Description: Program to calculate compound interest Option Explicit Private Dim Dim Dim Dim Sub cmdCalculate_Click() prAmt As Double time As Long rate As Double result As Double

prAmt = Me.txtPrAmt.Text time = Me.txtTime.Text rate = Me.txtRate.Text result = prAmt * pow(1 + (rate / 100), time) Me.txtResult.Text = result lblInterest.Caption = result - prAmt End Sub Private Sub txtPrAmt_Validate(Cancel As Boolean) If Len(Me.txtPrAmt.Text) = 0 Then Cancel = True End If End Sub Private Sub txtRate_Validate(Cancel As Boolean) If Len(Me.txtRate.Text) = 0 Then Cancel = True End If End Sub Private Sub txtTime_Validate(Cancel As Boolean) If Len(Me.txtTime.Text) = 0 Then Cancel = True End If End Sub Private Function pow(ByVal number As Double, ByVal power As Long) As Double Dim result As Double

Dim i As Integer If power = 0 Then result = 1: GoTo Label1 result = 1 For i = 1 To power Step 1 result = result * number Next i Label1: pow = result End Function

Q11. Write a program to check whether the given string is a palindrome or not

' Description: Program to check a string for Palindrome Option Explicit Private Dim Dim Dim Dim Function CheckForPalindrome(ByVal str As String) As Boolean i As Integer j As Integer strLen As Integer result As Boolean

strLen = Len(str) result = False i = 1 j = Len(str) Do While (i <> j) If Mid$(str, i, 1) <> Mid$(str, j, 1) Then result = False Exit Do Else

i = i + 1 j = j - 1 result = True End If Loop CheckForPalindrome = result End Function Private Sub cmdCheck_Click() If CheckForPalindrome(Me.txtInput.Text) Then Me.txtOutput.Text = "Is a Palindrome" Else Me.txtOutput.Text = "Is Not a Palindrome" End If End Sub

Q12. Program that implements 10 String Functions

' Description: Program to implement 10 String functions Option Explicit Dim s1 As String Dim s2 As String

Dim ret As Integer Private Sub cmdAsc_Click() Me.txtAscOutput.Text = Asc(CStr(Me.txtAsc.Text)) End Sub Private Sub cmdInStr_Click() Me.lblInStrOutput.Caption = InStr(Me.txtInStrStrtFrm.Text, _ Me.txtInStr.Text, _ Me.txtInStrSrchChar.Text) End Sub Private Sub cmdLCase_Click() Me.txtLCaseOutput.Text = LCase(Me.txtLCase.Text) End Sub Private Sub cmdLeft_Click() Me.lblLeftOutput.Caption = Left(CStr(Me.txtLeft.Text), _ CLng(Me.txtLeftNoOfChars)) End Sub Private Sub cmdLen_Click() Me.txtLenOutput.Text = Len(Me.txtLen.Text) End Sub Private Sub cmdMid_Click() Me.lblMidOutput.Caption = Mid(CStr(Me.txtMid.Text), _ CLng(Me.txtMidStart.Text), _ CInt(Me.txtMidLen.Text)) End Sub Private Sub cmdRight_Click() Me.lblRightOutput.Caption = Right(CStr(Me.txtRight.Text), _ CLng(Me.txtRightNoOfChars)) End Sub Private Sub cmdStrComp_Click() s1 = CStr(Me.txtStrComp_1.Text) s2 = CStr(Me.txtStrComp_2.Text) ret = StrComp(s1, s2, vbTextCompare) If ret = 0 Then Me.lblStrCompOutput.Caption = "1 = 2" ElseIf ret = -1 Then Me.lblStrCompOutput.Caption = "1 < 2" ElseIf ret = 1 Then Me.lblStrCompOutput.Caption = "1 > 2" End If End Sub

Private Sub cmdString_Click() Me.lblStringOutput.Caption = String(CLng(Me.txtStrNo2Rpt.Text), _ CStr(Me.txtString.Text)) End Sub Private Sub cmdUCase_Click() Me.txtUCaseOutput.Text = UCase(Me.txtUCase.Text) End Sub

Q15. Write a program to implement calculator

' Description: Program to implement Simple Calculator Option Explicit Dim ClearDisplay As Boolean Dim operand1 As Double Dim operand2 As Double Dim operator As String Private Sub cmdAdd_Click() operand1 = CDbl(lblOutput.Caption) operator = "+" lblOperator.Caption = "+" lblOutput.Caption = "" End Sub Private Sub cmdBackSpace_Click() If Len(lblOutput.Caption) <> 0 Then lblOutput.Caption = Mid$(lblOutput.Caption, 1, Len(lblOutput.Caption) - 1) End If End Sub Private Sub cmdClearDisplay_Click()

lblOutput.Caption = "" ClearDisplay = True End Sub Private Sub cmdDivide_Click() operand1 = CDbl(lblOutput.Caption) operator = "/" lblOperator.Caption = "/" lblOutput.Caption = "" End Sub Private Sub cmdEqual_Click() operand2 = CDbl(lblOutput.Caption) If operator = "+" Then lblOutput.Caption = operand1 + operator = "" ElseIf operator = "-" Then lblOutput.Caption = operand1 operator = "" ElseIf operator = "*" Then lblOutput.Caption = operand1 * operator = "" ElseIf operator = "/" And operand2 lblOutput.Caption = operand1 / operator = "" End If ClearDisplay = True End Sub Private Sub cmdMultiply_Click() operand1 = CDbl(lblOutput.Caption) operator = "*" lblOperator.Caption = "*" lblOutput.Caption = "" End Sub Private Sub cmdNumber_Click(Index As Integer) If ClearDisplay Then Me.lblOutput.Caption = "" ClearDisplay = False End If lblOutput.Caption = lblOutput.Caption + Me.cmdNumber(Index).Caption End Sub Private Sub cmdOneByX_Click() If CDbl(lblOutput.Caption) <> 0 Then lblOutput.Caption = 1 / CDbl(lblOutput.Caption) End If ClearDisplay = True End Sub Private Sub cmdPercentage_Click() operand2 = CDbl(lblOutput.Caption) If operator = "+" Then lblOutput.Caption = operand1 + (operand1 * (operand2 / 100)) operator = "" ElseIf operator = "-" Then lblOutput.Caption = operand1 - (operand1 * (operand2 / 100)) operator = "" ElseIf operator = "*" Then

operand2 operand2 operand2 <> 0 Then operand2

lblOutput.Caption = operand1 * (operand1 / 100) operator = "" ElseIf operator = "/" And operand2 <> 0 Then lblOutput.Caption = operand1 / (operand1 / 100) operator = "" End If ClearDisplay = True End Sub Private Sub cmdPlusMinus_Click() lblOutput.Caption = -CDbl(lblOutput.Caption) ClearDisplay = True End Sub Private Sub cmdReset_Click() operand1 = 0 operand2 = 0 operator = "" lblOperator.Caption = "" lblOutput.Caption = "" End Sub Private Sub cmdSqrt_Click() lblOutput.Caption = Math.Sqr(CDbl(lblOutput.Caption)) ' square root End Sub Private Sub cmdSubtract_Click() operand1 = CDbl(lblOutput.Caption) operator = "-" lblOperator.Caption = "-" lblOutput.Caption = "" End Sub Private Sub Form_Load() ClearDisplay = True Me.cmdBackSpace.Enabled = False End Sub Private Sub lblOutput_Change() If Len(lblOutput.Caption) = 0 Then Me.cmdBackSpace.Enabled = False End If If Len(lblOutput.Caption) <> 0 And ClearDisplay = False Then Me.cmdBackSpace.Enabled = True End If End Sub Private Sub mnuCopyToClipBoard_Click() If Len(lblOutput.Caption) <> 0 Then Clipboard.SetText lblOutput.Caption Else MsgBox "Nothing to Copy", vbOKOnly + vbInformation, "Copy to Clipboard" End If End Sub Q16. Write a program to Display an Image according to the Day

' Description: To display an image according to the Day of the Week Dim WeekDay As Integer Private Sub Timer1_Timer() WeekDay = DatePart("w", Now) Dim pictures As Variant pictures = Array(App.Path & "\images\sunday.jpg", _ App.Path & "\images\monday.jpg", _ App.Path & "\images\tuesday.jpg", _ App.Path & "\images\wednesday.jpg", _ App.Path & "\images\thursday.jpg", _ App.Path & "\images\friday.jpg", _ App.Path & "\images\saturday.jpg") Select Case WeekDay Case 1: ' SUNDAY Picture1.Picture = LoadPicture(pictures(0)) Me.lblDayOfTheWeek.Caption = "Sunday" Case 2: ' MONDAY Picture1.Picture = LoadPicture(pictures(1)) Me.lblDayOfTheWeek.Caption = "Monday" Case 3: ' TUESDAY Picture1.Picture = LoadPicture(pictures(2)) Me.lblDayOfTheWeek.Caption = "Tuesday" Case 4: ' WEDNESDAY Picture1.Picture = LoadPicture(pictures(3)) Me.lblDayOfTheWeek.Caption = "Wednesday" Case 5: ' THURSDAY Picture1.Picture = LoadPicture(pictures(4)) Me.lblDayOfTheWeek.Caption = "Thursday" Case 6: ' FRIDAY

Picture1.Picture = LoadPicture(pictures(5)) Me.lblDayOfTheWeek.Caption = "Friday" Case 7: ' SATURDAY Picture1.Picture = LoadPicture(pictures(6)) Me.lblDayOfTheWeek.Caption = "Saturday" End Select End Sub

Q17. Write a program to change image every few seconds

Option Explicit ' Description: Program to demonstrate moving pictures ' which change periodically ( Animation)

Private Sub Form_Load() Timer1.Enabled = False Timer2.Enabled = False End Sub Private Sub Picture1_Click() Me.lblMessage.Left = 4320 Me.lblMessage.Top = 0 Me.lblMessage.Visible = False Timer1.Enabled = True End Sub Private Sub Picture1_DblClick() frmAbout.Show 1, Me End Sub Private Sub Picture1_KeyPress(KeyAscii As Integer) If KeyAscii = 27 Then End End If End Sub Private Sub Timer1_Timer() Static iCtr As Integer Dim frames As Variant frames = Array(App.Path & "\ball_frames\fr0.jpg", _ App.Path & "\ball_frames\fr1.jpg", _ App.Path & "\ball_frames\fr2.jpg", _ App.Path & "\ball_frames\fr3.jpg", _ App.Path & "\ball_frames\fr4.jpg", _ App.Path & "\ball_frames\fr5.jpg", _ App.Path & "\ball_frames\fr6.jpg", _ App.Path & "\ball_frames\fr7.jpg", _ App.Path & "\ball_frames\fr8.jpg", _ App.Path & "\ball_frames\fr9.jpg", _ App.Path & "\ball_frames\fr10.jpg", _ App.Path & "\ball_frames\fr11.jpg", _ App.Path & "\ball_frames\fr12.jpg", _ App.Path & "\ball_frames\fr13.jpg", _ App.Path & "\ball_frames\fr14.jpg", _ App.Path & "\ball_frames\fr15.jpg", _ App.Path & "\ball_frames\fr16.jpg", _ App.Path & "\ball_frames\fr17.jpg", _ App.Path & "\ball_frames\fr18.jpg") If iCtr > 18 Then iCtr = 0 Me.Timer1.Enabled = False Me.lblMessage.Visible = True Me.Timer2.Enabled = True Exit Sub End If Me.Picture1.Picture = LoadPicture(frames(iCtr)) iCtr = iCtr + 1 End Sub

Private Sub Timer2_Timer() Static i As Integer Static j As Integer If i > 200 Then Me.Timer2.Enabled = False i = 0 j = 0 Exit Sub End If Me.lblMessage.Left = Me.lblMessage.Left - i Me.lblMessage.Top = Me.lblMessage.Top + j i = i + 10 If j < 100 Then j = j + 10 End Sub

Q18. Program to change the background color of the form using scrollbars Output

Option Explicit ' Description : Using scrollbars changing the background colors of ' the form Private Sub Form_Load() Call ColorValues End Sub Private Sub HS_BLUE_Change() Call ColorValues End Sub Private Sub HS_BLUE_Scroll() Call ColorValues End Sub Private Sub HS_GREEN_Change() Call ColorValues End Sub Private Sub HS_GREEN_Scroll() Call ColorValues End Sub Private Sub HS_RED_Change() Call ColorValues End Sub

Private Sub HS_RED_Scroll() Call ColorValues End Sub Private Sub ColorValues() Me.BackColor = RGB(Me.HS_RED.Value, Me.HS_GREEN.Value, Me.HS_BLUE.Value) Me.Label1.BackColor = RGB(Me.HS_RED.Value, Me.HS_GREEN.Value, Me.HS_BLUE.Value) Me.Label2.BackColor = RGB(Me.HS_RED.Value, Me.HS_GREEN.Value, Me.HS_BLUE.Value) Me.Label3.BackColor = RGB(Me.HS_RED.Value, Me.HS_GREEN.Value, Me.HS_BLUE.Value) End Sub

Q19. Program to Demonstrate Menus and Popup Menus

Option Explicit ' Description: Adding and Removing Menu items from a Popup as well as ' from a Regular menu Private Sub cmdAddNewMenuItem_Click() If Me.txtItemName.Text = "" Then Exit Sub Static intItemCount As Integer intItemCount = intItemCount + 1 Load mnuFileItems(intItemCount) mnuFileItems(intItemCount).Caption = Me.txtItemName.Text mnuFileItems(initemcount).Tag = Me.txtItemName.Text mnuFileItems(intItemCount).Visible = True Me.txtItemName.Text = "" Me.txtItemName.SetFocus End Sub Private Sub cmdAddNewPopupItem_Click() If Me.txtInputPopupMenuName.Text = "" Then Exit Sub Static intItemCount As Integer intItemCount = intItemCount + 1 Load mnuPopupMessage(intItemCount) mnuPopupMessage(intItemCount).Caption = Me.txtInputPopupMenuName.Text mnuPopupMessage(intItemCount).Visible = True Me.txtInputPopupMenuName.Text = "" Me.txtInputPopupMenuName.SetFocus End Sub Private Sub cmdRemoveAnItem_Click() Dim intCount As Integer intCount = Me.mnuFileItems.Count - 1 If intCount = 0 Then MsgBox "No items to remove": Exit Sub MsgBox "Item >> " + Me.mnuFileItems(intCount).Caption + " << Removed" Unload Me.mnuFileItems(intCount) End Sub Private Sub cmdRemovePopupItem_Click() Dim intCount As Integer intCount = Me.mnuPopupMessage.Count - 1 If intCount = 0 Then MsgBox "No items to remove": Exit Sub MsgBox "Item >> " + Me.mnuPopupMessage(intCount).Caption + " << Removed" Unload Me.mnuPopupMessage(intCount) End Sub

Private Sub Frame1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) If Button = vbRightButton Then Me.PopupMenu Me.mnuPopup, vbPopupMenuLeftAlign End If End Sub Private Sub mnuAbout_Click() frmAbout.Show 1, Me End Sub Private Sub mnuFileExit_Click() End End Sub Private Sub mnuFileItems_Click(Index As Integer) MsgBox "You clicked item: " + mnuFileItems(Index).Caption End Sub Private Sub mnuFileNew_Click() MsgBox "Hey! Stop doing that! :)" End Sub Private Sub mnuPopupExit_Click() End End Sub Private Sub mnuPopupMessage_Click(Index As Integer) MsgBox "You clicked a popup item : " + mnuPopupMessage(Index).Caption End Sub Private Sub txtInputPopupMenuName_GotFocus() Me.cmdAddNewPopupItem.Default = True End Sub Private Sub txtItemName_GotFocus() Me.cmdAddNewMenuItem.Default = True End Sub

Q 20.

User defined type with Dynamic array

Private Type employee id As Integer name As String sal As Single End Type Dim udt() As employee Dim total As Integer Private Sub cmdprint_Click() lstdisplay.Visible = True lstdisplay.Clear Dim temp As String Dim i As Integer temp = "Id Name lstdisplay.AddItem (temp) temp = "" lstdisplay.AddItem (temp) For i = 0 To total - 1 temp = udt(i).id & " Salary"

" & udt(i).name & " " & udt(i).sal lstdisplay.AddItem (temp) Next i

End Sub

Private Sub cmdsubmit_Click() udt(total).id = txtid.Text udt(total).name = txtname.Text udt(total).sal = txtsalary.Text total = total + 1 ReDim Preserve udt(UBound(udt) + 1) txtid.Text = "" txtname.Text = "" txtsalary.Text = "" End Sub Private Sub Form_Load() ReDim udt(0) total = 0 End Sub

Q 21. Changing picture of picture box and form color automatically.

Dim t As Integer Private Sub Timer1_Timer() If t = 0 Then Picture1.Picture = LoadPicture("C:\Documents and Settings\Future\Desktop\1.jpg") Me.BackColor = vbBlue t = 1 ElseIf t = 1 Then Picture1.Picture = LoadPicture("C:\Documents and Settings\Future\Desktop\2.jpg") Me.BackColor = vbYellow t = 2 ElseIf t = 2 Then Picture1.Picture = LoadPicture("C:\Documents and Settings\Future\Desktop\3.jpg") Me.BackColor = vbGreen t = 0 End If End Sub

Q.22 Program to implement cut, copy and paste buttons for text and pictures.

Private Sub cuttext_Click() Clipboard.Clear Clipboard.SetText (txtsource.SelText) txtsource.SelText = "" End Sub Private Sub copytext_Click() Clipboard.Clear Clipboard.SetText (txtsource.SelText) End Sub Private Sub pastetext_Click() txttarget.SelText = Clipboard.GetText() Clipboard.Clear End Sub Private Sub cutpic_Click() Clipboard.Clear Clipboard.SetData picsource.Picture picsource.Picture = LoadPicture("") End Sub Private Sub copypic_Click() Clipboard.Clear Clipboard.SetData picsource.Picture End Sub Private Sub pastepic_Click() pictarget.Picture = Clipboard.GetData() Clipboard.Clear End Sub