C Programs Using Nested If
1. Check whether a number is negative, positive, or zero:
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num >= 0) {
if (num == 0)
printf("The number is zero.\n");
else
printf("The number is positive.\n");
} else {
printf("The number is negative.\n");
}
return 0;
}
2. Check whether a number is divisible by 5 and 11 or not:
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 5 == 0) {
if (num % 11 == 0)
printf("Divisible by both 5 and 11.\n");
else
printf("Divisible by 5 but not 11.\n");
} else {
printf("Not divisible by 5, so not divisible by both.\n");
}
return 0;
}
3. Input week number and print weekday:
#include <stdio.h>
int main() {
int week;
printf("Enter week number (1-7): ");
scanf("%d", &week);
if (week >= 1) {
if (week <= 7) {
if (week == 1)
printf("Monday\n");
else if (week == 2)
printf("Tuesday\n");
else if (week == 3)
printf("Wednesday\n");
else if (week == 4)
printf("Thursday\n");
else if (week == 5)
printf("Friday\n");
else if (week == 6)
printf("Saturday\n");
else
printf("Sunday\n");
} else {
printf("Invalid week number.\n");
}
} else {
printf("Invalid week number.\n");
}
return 0;
}
4. Input month number and print number of days:
#include <stdio.h>
int main() {
int month;
printf("Enter month number (1-12): ");
scanf("%d", &month);
if (month >= 1) {
if (month <= 12) {
if (month == 2)
printf("28 or 29 days\n");
else if (month == 4 || month == 6 || month == 9 || month == 11)
printf("30 days\n");
else
printf("31 days\n");
} else {
printf("Invalid month number.\n");
}
} else {
printf("Invalid month number.\n");
}
return 0;
}
5. Input angles and check if triangle is valid:
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three angles of triangle: ");
scanf("%d %d %d", &a, &b, &c);
if ((a + b + c) == 180) {
if (a > 0 && b > 0 && c > 0)
printf("Triangle is valid.\n");
else
printf("Invalid angle values.\n");
} else {
printf("Triangle is not valid.\n");
}
return 0;
}
6. Input all sides and check if triangle is valid:
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three sides of triangle: ");
scanf("%d %d %d", &a, &b, &c);
if (a + b > c) {
if (a + c > b) {
if (b + c > a)
printf("Triangle is valid.\n");
else
printf("Triangle is not valid.\n");
} else {
printf("Triangle is not valid.\n");
}
} else {
printf("Triangle is not valid.\n");
}
return 0;
}
7. Input marks and calculate percentage & grade:
#include <stdio.h>
int main() {
int p, c, b, m, comp;
float perc;
printf("Enter marks in Physics, Chemistry, Biology, Math, Computer: ");
scanf("%d %d %d %d %d", &p, &c, &b, &m, &comp);
perc = (p + c + b + m + comp) / 5.0;
if (perc >= 90)
printf("Grade A\n");
else {
if (perc >= 80)
printf("Grade B\n");
else {
if (perc >= 70)
printf("Grade C\n");
else {
if (perc >= 60)
printf("Grade D\n");
else {
if (perc >= 40)
printf("Grade E\n");
else
printf("Grade F\n");
}
}
}
}
return 0;
}
8. Input basic salary and calculate gross salary:
#include <stdio.h>
int main() {
float basic, hra, da, gross;
printf("Enter basic salary: ");
scanf("%f", &basic);
if (basic <= 10000) {
hra = basic * 0.20;
da = basic * 0.80;
} else {
if (basic <= 20000) {
hra = basic * 0.25;
da = basic * 0.90;
} else {
hra = basic * 0.30;
da = basic * 0.95;
}
}
gross = basic + hra + da;
printf("Gross Salary = %.2f\n", gross);
return 0;
}