Thursday 10 September 2020

8. Write a QBasic Program to Input n number and print the sum of first n natural numbers.

 8. 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

No comments:

Post a Comment

MCQ Question Bank Microsoft PowerPoint