1) Write a BASIC program to check any year is leap or not..
2) Write a basic program to display the odd numbers from 40-1
Answers
Answer:
1) #include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
// leap year if perfectly visible by 400
if (year % 400 == 0) {
printf("%d is a leap year.", year);
}
// not a leap year if visible by 100
// but not divisible by 400
else if (year % 100 == 0) {
printf("%d is not a leap year.", year);
}
// leap year if not divisible by 100
// but divisible by 4
else if (year % 4 == 0) {
printf("%d is a leap year.", year);
}
// all other years are not leap year
else {
printf("%d is not a leap year.", year);
}
return 0;
}
output:
Enter a year: 1900
1900 is not a leap year.
b) class JavaExample {
public static void main(String args[]) {
int n = 40;
System.out.print("Odd Numbers from 1 to "+n+" are: ");
for (int i = 1; i <= n; i++) {
if (i % 2 != 0) {
System.out.print(i + " ");
}
}
}
}
output: Odd Numbers from 1 to 40 are 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39