QBasic Programming

Input n number and print the sum of first n natural numbers

REM CALCULATE SUM OF FIRST N NATURAL NUMBERS
CLS
INPUT “ENTER THE NUMBER”; N
S = N * (N + 1) / 2
PRINT “SUM OF”; N; “NUMBERS=”; S
END

USING SUB PROCEDURE

DECLARE SUB SUM(N)
CLS
INPUT “ENTER THE NUMBER”; N
CALL SUM (N)
END

SUB SUM (N)
S = N * (N + 1) / 2
PRINT “SUM OF”; N; “NUMBERS=”; S
END SUB

USING FUNCTION PROCEDURE
DECLARE FUNCTION SUM(N)
CLS
INPUT “ENTER THE NUMBER”; N
PRINT “SUM OF”; N; “NUMBERS=”; SUM (N)
END
FUNCTION SUM (N)
S = N * (N + 1) / 2
SUM = S
END FUNCTION

Display sum of half of any two given numbers (Prev Lesson)
(Next Lesson) Input n number and print the sum square of first n natural numbers
Back to QBasic Programming

No Comments

Post a Reply

Course Curriculum

error: Content is protected !!