Thursday, October 10, 2013

ALGORITHM TO FIND SUM OF FIRST N NUMBERS USING FOR AND WHILE


Method 1 (FOR)

              STEP 1.       Set I <-- 0, N <-- 0, Sum <-- 0
STEP 2.       Read N
STEP 3.       Repeat for I = 0, 1, … N – 1
  Begin
Sum <-- Sum + I
  End
       STEP 4.       Print Sum


Method 2(WHILE)

STEP 1.       Set I <-- 0, N <-- 0, Sum <-- 0
STEP 2.       Read N
STEP 3.      while (I<=N-1)
  Begin
Sum <-- Sum + I
I<-- I+1
  End
STEP 4.       Print Sum 



1 comment: