# IF ELSE and Loops Practice Problems in VB with Solution

IF THEN and ELSE statement and Loops Practice Problems in VB with Solution ,Visual basic Programming
Practice Problems:

1. Determine the sum of the first ‘n’ numbers (without using a formula) where ‘n’ is taken as input from a Text Box.

1. Request a number and display asterisks in the following format. The number you input should be equal to the number of stars in a line and the number of lines to be printed..
For example: Input = 4
Every even line is shifted one space to check if the line is even use the ‘%’ operator which will return you 0 for even numbers when divided by 2 i.e
n % 2=0 for even
n % 2=1 for odd
Output in a picture box:

****
****
****
****
3.Write a program to display all the numbers between 1 and “n” that are perfect squares.

The value of “n” will be input by the user. You may use any value to input the value.

Perfect square are the numbers whose square root can be computed for e.g if n=10
then we have 1,4,9 as perfect squares up to 10
There can be many ways to find solution to this problem  one way is

Int (sqr(x))=sqr(x)
If the above statement is true, the number is perfect square.

SOLUTION:

Problem 2 solution

Private Sub Command1_Click()

Dim row As Byte
Dim col As Byte
Dim n As Byte

n = Val(Text1.Text)
row = 1
Do While row <= n
col = 1
If row Mod 2 <> 0 Then
Picture1.Print " ";
End If
Do While col <= n
Picture1.Print "*";
col = col + 1
Loop
row = row + 1
Picture1.Print ""
Loop

End Sub

Problem 3 solution

Private Sub Command1_Click()
Dim n As Integer
Dim index As Integer
n = Val(Text1.Text)
index = 1
Do While index <= n
If Int(Sqr(index)) = Sqr(index) Then
Picture1.Print index
End If
index = index + 1
Loop
End Sub

Problem 3 solution

Private Sub Command1_Click()
Dim n As Integer
Dim index As Integer
n = Val(Text1.Text)
index = 1
Do
If Int(Sqr(index)) = Sqr(index) Then
Picture1.Print index
End If
index = index + 1
Loop Until index > n
End Sub • Title : IF ELSE and Loops Practice Problems in VB with Solution
• Posted by :
• Date : 11:03
• Labels :

• Blogger Comments
• Facebook Comments