Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 22

Assignment 3:

9. Program to check whether a year is a leap year or not

Iteration 1:

#include <stdio.h>

int main() {

int year;

printf("Enter a year: ");

scanf("%d", &year);

printf("%d is %s leap year\n", year, (year % 4 == 0 && year % 100 != 0) || year % 400 == 0 ? "a" :
"not a");

return 0;

Iteration 2:

#include <stdio.h>

int main() {

int year;

printf("Enter a year: ");

scanf("%d", &year);

if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)

printf("%d is a leap year\n", year);

else

printf("%d is not a leap year\n", year);

return 0;
}

Iteration 3:

#include <stdio.h>

int main() {

int year;

printf("Enter a year: ");

scanf("%d", &year);

switch ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {

case 1:

printf("%d is a leap year\n", year);

break;

default:

printf("%d is not a leap year\n", year);

return 0;

Iteration 4:

#include <stdio.h>

int isLeapYear(int year) {

return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;

int main() {

int year;

printf("Enter a year: ");


scanf("%d", &year);

if (isLeapYear(year))

printf("%d is a leap year\n", year);

else

printf("%d is not a leap year\n", year);

return 0;

Iteration 5:

#include <stdio.h>

int main() {

int year;

do {

printf("Enter a year: ");

scanf("%d", &year);

if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)

printf("%d is a leap year\n", year);

else

printf("%d is not a leap year\n", year);

} while (year != 0);

return 0;

10. Program to find the sign of a product of two numbers

Iteration 1:

#include <stdio.h>
int main() {

int num1, num2, product;

printf("Enter two numbers: ");

scanf("%d %d", &num1, &num2);

product = num1 * num2;

if (product > 0) {

printf("The product is positive\n");

} else if (product < 0) {

printf("The product is negative\n");

} else {

printf("The product is zero\n");

return 0;

Iteration 2:

#include <stdio.h>

int main() {

int num1, num2;

printf("Enter two numbers: ");

scanf("%d %d", &num1, &num2);

if ((num1 * num2) > 0) {

printf("The product is positive\n");

} else if ((num1 * num2) < 0) {

printf("The product is negative\n");


} else {

printf("The product is zero\n");

return 0;

Iteration 3:

#include <stdio.h>

int main() {

int num1, num2;

printf("Enter two numbers: ");

scanf("%d %d", &num1, &num2);

int product = num1 * num2;

if (product > 0) {

printf("The product is positive\n");

} else if (product < 0) {

printf("The product is negative\n");

} else {

printf("The product is zero\n");

return 0;

Iteration 4:

#include <stdio.h>

int main() {
int num1;

int num2;

int product;

printf("Enter two numbers: ");

scanf("%d %d", &num1, &num2);

product = num1 * num2;

if (product > 0) {

printf("The product is positive\n");

} else if (product < 0) {

printf("The product is negative\n");

} else {

printf("The product is zero\n");

return 0;

Iteration 5:

#include <stdio.h>

int main() {

int num1;

int num2;

printf("Enter two numbers: ");

scanf("%d %d", &num1, &num2);

int product;

product = num1 * num2;


if (product > 0) {

printf("The product is positive\n");

} else if (product < 0) {

printf("The product is negative\n");

} else {

printf("The product is zero\n");

return 0;

Assignment 4:

1. Syntax error in if statement

Iteration 1:

#include <stdio.h>

int main() {

int num = 10;

if (num > 5)

printf("num is greater than 5\n");

return 0;

Iteration 2:

#include <stdio.h>

int main() {

int num = 10;

if (num > 5) {
printf("num is greater than 5\n");

return 0;

Iteration 3:

#include <stdio.h>

int main() {

int num = 10;

if (num > 5)

printf("num is greater than 5\n");

else

printf("num is less than or equal to 5\n");

return 0;

Iteration 4:

#include <stdio.h>

int main() {

int num = 10;

if (num > 5) {

printf("num is greater than 5\n");

} else {

printf("num is less than or equal to 5\n");

return 0;
}

Iteration 5:

#include <stdio.h>

int main() {

int num = 10;

printf("num is %s\n", (num > 5) ? "greater than 5" : "less than or equal to 5");

return 0;

2. Syntax error in nested if statement

Iteration 1:

#include <stdio.h>

int main() {

int num = 10;

if (num == 5) {

printf("num is equal to 5\n");

return 0;

Iteration 2:

#include <stdio.h>

int main() {

int num = 10;

if (num == 5) {
printf("num is equal to 5\n");

} else {

printf("num is not equal to 5\n");

return 0;

Iteration 3:

#include <stdio.h>

int main() {

int num = 10;

if (num < 5) {

printf("num is less than 5\n");

} else if (num > 5) {

printf("num is greater than 5\n");

} else {

printf("num is equal to 5\n");

return 0;

Iteration 4:

#include <stdio.h>

int main() {

int num = 10;

switch (num) {

case 5:
printf("num is equal to 5\n");

break;

default:

printf("num is not equal to 5\n");

return 0;

Iteration 5:

#include <stdio.h>

int main() {

int num = 10;

printf("num is %s\n", (num == 5) ? "equal to 5" : "not equal to 5");

return 0;

3. Syntax Error in if else Statement

Iteration 1:

#include <stdio.h>

int main() {

int num = 10;

num > 5 ? printf("num is greater than 5\n") : printf("num is less than or equal to 5\n");

return 0;

Iteration 2:
#include <stdio.h>

int main() {

int num = 10;

switch (num > 5) {

case 1:

printf("num is greater than 5\n");

break;

case 0:

printf("num is less than or equal to 5\n");

break;

return 0;

Iteration 3:

#include <stdio.h>

void printNumber(int num) {

if (num > 5) {

printf("num is greater than 5\n");

} else {

printf("num is less than or equal to 5\n");

int main() {

int num = 10;

printNumber(num);

return 0;
}

Iteration 4:

#include <stdio.h>

#define PRINT_NUMBER(num) \

if (num > 5) { \

printf("num is greater than 5\n"); \

} else { \

printf("num is less than or equal to 5\n"); \

int main() {

int num = 10;

PRINT_NUMBER(num);

return 0;

Iteration 5:

#include <stdio.h>

void printGreater() {

printf("num is greater than 5\n");

void printLessOrEqual() {

printf("num is less than or equal to 5\n");

int main() {

int num = 10;

void (*printFunc[2])() = { printLessOrEqual, printGreater };

int index = num > 5 ? 1 : 0;


printFunc[index]();

return 0;

4. Semantic error in nested if statement

Iteration 1:

#include <stdio.h>

int main() {

int number = 10;

if (number > 5) {

printf("number is greater than 5\n");

} else {

printf("number is greater than or equal to 5\n");

return 0;

Iteration 2:

#include <stdio.h>

int main() {

int num = 10;

if (num >= 5) {

printf("num is greater than or equal to 5\n");

} else {

printf("num is less than 5\n");

return 0;

Iteration 3:
#include <stdio.h>

int main() {

int num = 10;

if (num > 5 && num % 2 == 0) {

printf("num is greater than 5 and even\n");

} else {

printf("num is not greater than 5 or not even\n");

return 0;

Iteration 4:

#include <stdio.h>

int main() {

float num = 10.5;

if (num > 5) {

printf("num is greater than 5\n");

} else {

printf("num is less than or equal to 5\n");

return 0;

Iteration 5:

#include <stdio.h>

int main() {

int num = 10;

switch (num) {
case 5:

case 6:

case 7:

case 8:

case 9:

case 10:

printf("num is greater than or equal to 5\n");

break;

default:

printf("num is less than 5\n");

return 0;

5. Syntax Error in if if else Statement

Iteration 1:

#include <stdio.h>

int main() {

int num = 10;

switch (num) {

case 5:

printf("num is equal to 5\n");

break;

case 10:

printf("num is greater than 5\n");

break;

default:

printf("num is less than 5\n");

break;
}

return 0;

Iteration 2:

#include <stdio.h>

int main() {

int num = 10;

if (num > 5) {

printf("num is greater than 5\n");

} else {

if (num < 5) {

printf("num is less than 5\n");

} else {

printf("num is equal to 5\n");

return 0;

Iteration 3:

#include <stdio.h>

int main() {

int num = 10;

num > 5 ? printf("num is greater than 5\n") : num < 5 ? printf("num is less than 5\n") : printf("num
is equal to 5\n");
return 0;

Iteration 4:

#include <stdio.h>

void checkNum(int num) {

if (num > 5) {

printf("num is greater than 5\n");

} else if (num < 5) {

printf("num is less than 5\n");

} else if (num == 5) {

printf("num is equal to 5\n");

int main() {

int num = 10;

checkNum(num);

return 0;

Iteration 5:

#include <stdio.h>

int main() {

int num = 10;

if (num > 5) {

printf("num is greater than 5\n");

}
if (num < 5) {

printf("num is less than 5\n");

if (num == 5) {

printf("num is equal to 5\n");

return 0;

6. Semantic error in if-else statement

Iteration 1:

#include <stdio.h>

int main() {

int num = 10;

if (num > 5) {

printf("num is greater than 5\n");

} else {

if (num < 5) {

printf("num is less than 5\n");

} else {

printf("num is equal to 5\n");

return 0;

Iteration 2:
#include <stdio.h>

int main() {

int num = 10;

switch (num) {

case 5:

printf("num is equal to 5\n");

break;

case 10:

printf("num is greater than 5\n");

break;

default:

printf("num is less than 5\n");

return 0;

Iteration 3:

#include <stdio.h>

int main() {

int num = 10;

num > 5 ? printf("num is greater than 5\n") :

(num < 5 ? printf("num is less than 5\n") :

printf("num is equal to 5\n"));

return 0;

Iteration 4:
#include <stdio.h>

int main() {

int num = 10;

if (num > 5) {

printf("num is greater than 5\n");

} else {

if (num < 5) {

printf("num is less than 5\n");

} else {

printf("num is equal to 5\n");

return 0;

Iteration 5:

#include <stdio.h>

int main() {

int num = 10;

if (num > 5)

printf("num is greater than 5\n");

else if (num < 5)

printf("num is less than 5\n");

else

printf("num is equal to 5\n");

return 0;
}

You might also like