Computer for SEE and NEB

It is a complete SEE and NEB solution for computer science. It includes Computer Fundamentals, Database (SQL), Programming in C QBASIC, CSS, JavaScript, and PHP for beginners.

Breaking

Post Top Ad

Your Ad Spot

Wednesday, November 30, 2022

QBASIC program to input a year and display whether input year is a leap year or not

 QBASIC program to input a year and display whether input year is a leap year or not

 

QBASIC program to input a year and display whether input year is a leap year or not



Introduction:

QBASIC is an old but still popular programming language that has been used to teach computer programming to students for decades. One of the classic exercises in QBASIC is to write a program that takes an input year from the user and determines whether that year is a leap year or not. In this article, we will discuss how to write a QBASIC program to input a year and display whether input year is a leap year or not.

What is leap Year?

First, let's briefly explain what a leap year is. A leap year is a year that is divisible by 4, except for years that are divisible by 100 but not divisible by 400. For example, 2000 was a leap year because it is divisible by 4 and 400, but 1900 was not a leap year because it is divisible by 4 and 100, but not divisible by 400.


Coding:

CLS
INPUT "Enter year"; y
IF (y MOD 4 = 0) AND (y MOD 100 <> 0) OR (y MOD 100 = 0 AND y MOD 400 = 0) THEN
PRINT y; "is a leap year"
ELSE
PRINT y; "is not a leap year"
END IF
END


By using SUB procedure ( SUB…END SUB)

DECLARE SUB leap ()

CLS
CALL leap
END

SUB leap
INPUT "Enter year"; y
IF (y MOD 4 = 0) AND (y MOD 100 <> 0) OR (y MOD 100 = 0 AND y MOD 400 = 0) THEN
PRINT y; "is a leap year"
ELSE
PRINT y; "is not a leap year"
END IF
END SUB



By using FUNCTION procedure (FUNCTION…END FUNCTION)

DECLARE FUNCTION leap$ (y)
CLS
INPUT "Enter year:"; y
PRINT y; leap$(y)
END

FUNCTION leap$ (y)
IF (y MOD 4 = 0) AND (y MOD 100 <> 0) OR (y MOD 100 = 0 AND y MOD 400 = 0) THEN
leap$ = "is a leap year"
ELSE
leap$ = "is not a leap year"
END IF
END FUNCTION

 

Output:




Conclusion:

In conclusion, writing a QBASIC program to determine whether a year is a leap year is a classic exercise in computer programming. By using a combination of IF and MOD statements, you can easily check whether an input year is a leap year or not. This program is a great way to learn the basics of QBASIC programming and is still relevant today, despite being an older language.


No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages