Walk Lightly on this PLANET and yet leave such a FOOTPRINT that cannot be erased for thousands of Years..!!!
Visit Codstech for Cyber Security related Posts !

Visitors

Friday, July 22, 2011

VB-54 : Sum of n numbers

Write an event procedure to find the sum of all numbers from 1 to selected value. Use a horizontal scroll bar to set the maximum value.

sum=      (n+1)*n


Refer the JAVA program Sum of the digit of a number , to compare with VB program.

                 
(A)Program Design : 


(B) Property(Controls Used) :
  • 2 Labels(label1 & label2) as Captions  Sum of 1 to Number  and   =
  • 2 other Labels also needed (name lbl_num and lbl_sum) for storing the value of n and the sum from 1 to n
  • Horizontal Scroll Bar (Name-HScroll1,Min-1,Max-100,LargeChange-5)

(C) Attaching Code to the Object :




Private Sub HScroll1_Change()
lbl_num.Caption = HScroll1.Value
lbl_sum.Caption = (HScroll1.Value + 1) * HScroll1.Value / 2
End Sub



(D) Output :
Move the Scroll Bar from 1 to 100 , and for each value of it, we will get the sum.


Here, we moved the Scroll bar from 1 to 39 , and the sum of the numbers from 1 to 39 is 780
Note that,  lbl_num  is for storing the number 39 and  lbl_sum is for storing the number 780.
That is , one for the number and the other for the result.
Refer Sum of the first n terms of the series also.

====================================================================
=========================================================