Thursday 10 September 2020

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

9. Input n number and print the sum square of first n natural numbers.

REM CALCULATE SUM OF SQUARE OF FIRST N NATURAL NUMBERS

CLS

INPUT “ENTER THE NUMBER”; N

S = N * (N + 1) * (2 * N + 1) / 6

PRINT “SUM OF SQUARE OF ”; N; “NUMBERS=”; S

END

 

USING SUB PROCEDURE

 

DECLARE SUB SUMSQ(N)

CLS

INPUT “ENTER THE NUMBER”; N

CALL SUMSQ(N)

END

 

SUB SUMSQ (N)

S = N * (N + 1) * (2 * N + 1) / 6

PRINT “SUM OF SQUARE OF ”; N; “NUMBERS=”; S

END SUB

 

USING FUNCTION PROCEDURE

 

DECLARE FUNCTION SUMSQ(N)

CLS

INPUT “ENTER THE NUMBER”; N

PRINT “SUM OF SQUARE OF ”; N; “NUMBERS=”; SUMSQ (N)

END

 

FUNCTION SUMSQ (N)

S = N * (N + 1) * (2 * N + 1) / 6

SUMSQ = S

END FUNCTION


No comments:

Post a Comment

MCQ Question Bank Microsoft PowerPoint