Tuesday 6 October 2015

Class 8-Prog(Swap)

Pg :103  Q4




           

Swapping means exchanging . Here the values of 2 variables should be exchanged.

Imagine you have 2 jars with different liquids. You need to swap the liquids. What will you do? You will take one more jar to do this. Same technique we are applying here too.

We need the third variable to swap the given values.

Class 8 - Operators


Class 8 -Sample Programs

1. Assign  three numbers and find the average

Private Sub Command1_Click()
Dim a, b, c As Integer  ...................... Step 1 (Decaration)

Dim avg As Integer     ....................... Step 1
a = 2                            ........................ Step 2 (Assign)

b = 3                            ........................ Step 2
c = 14                          ........................ Step 2

avg = (a + b + c) / 3    ........................ Step 3 (Calculation)
Print avg                     ........................ Step 4  (Display )
End Sub


The answer for the above program is (14+2+3)=19/3=6.33333
but we will get 6, because the data type of avg is integer,which will give only whole number.

To get the correct answer, we have to change the data type.

Private Sub Command1_Click()
Dim a, b, c As Integer
Dim avg As double

a = 2
b = 3
c = 14

avg = (a + b + c) / 3
Print avg
End Sub


2. Accept the side of a square, and display its perimeter.

Perimeter=4 x side

Private Sub Command1_Click()
Dim side ,peri As Integer ..........  (Decaration)

side=val(text1.text)          ........... (Input)
peri=4 * side                    ........... (Calculation)
Print peri                          ........... (Display )

End Sub

3. Area of rectangle = Length x breadth

Private Sub Command1_Click()
Dim l,b,area As Integer ..........  (Decaration)

l = val(text1.text)          ........... (Input)
b = val(text2.text)
area = l*b                      ........... (Calculation)
Print area                       ........... (Display )

End Sub

In the above program, we should use two text boxes to accept length and breadth from the user. * should be used for multiplication symbol.

4. Volume of a sphere = 4/3 pi r3


 
Private Sub Command1_Click()
Dim r As Integer ..........  (Decaration)

Dim vol as double .......( The answer will be in decimal since we are multiplying by 3.14)
r = val(text1.text)          ........... (Input)
vol = 4/3 * 3.14 *r*r*r     ........... (Calculation)
Print vol                      ........... (Display )

End Sub

We cannot use the symbol for pi, so we have to use the value which is 3.14.
Instead of r*r*r we can also use r^3

5. Volume of Cylinder = pi r2 h

Private Sub Command1_Click()
Dim r,h As Integer 
Dim vol as double .......( The answer will be in decimal since we are multiplying by 3.14)
r = val(text1.text)         
h = val(text2.text)vol =  3.14 *r*r*h    OR ( 3.14* r^2 *h)
Print vol                  
End Sub