The document contains a series of C programming exercises focused on using if-else statements to solve various problems. Each exercise includes a code snippet that checks conditions such as even or odd numbers, positive or negative values, and eligibility for voting. Additionally, it covers topics like grading, triangle types, and simple calculations.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
9 views6 pages
C If Else Solutions Class9 Level
The document contains a series of C programming exercises focused on using if-else statements to solve various problems. Each exercise includes a code snippet that checks conditions such as even or odd numbers, positive or negative values, and eligibility for voting. Additionally, it covers topics like grading, triangle types, and simple calculations.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6
Solutions: C Programming if-else Practice
1. Check Even or Odd
#include <stdio.h> int main() { int num; printf("Enter a number: "); scanf("%d", &num); if (num % 2 == 0) printf("Even\n"); return 0; } 2. Check if Number is Positive #include <stdio.h> int main() { int num; printf("Enter a number: "); scanf("%d", &num); if (num > 0) printf("Positive number\n"); return 0; } 3. Greater or Smaller #include <stdio.h> int main() { int a, b; printf("Enter two numbers: "); scanf("%d %d", &a, &b); if (a > b) printf("First number is greater\n"); else printf("Second number is greater\n"); return 0; } 4. Pass or Fail #include <stdio.h> int main() { int marks; printf("Enter marks: "); scanf("%d", &marks); if (marks >= 33) printf("Pass\n"); else printf("Fail\n"); return 0; } 5. Check Divisibility #include <stdio.h> int main() { int num; printf("Enter a number: "); scanf("%d", &num); if (num % 5 == 0) printf("Divisible by 5\n"); else printf("Not divisible by 5\n"); return 0; } 6. Grade Calculator #include <stdio.h> int main() { int marks; printf("Enter marks: "); scanf("%d", &marks); if (marks >= 90) printf("Grade A\n"); else if (marks >= 70) printf("Grade B\n"); else if (marks >= 50) printf("Grade C\n"); else if (marks >= 33) printf("Grade D\n"); else printf("Fail\n"); return 0; } 7. Largest of Three Numbers #include <stdio.h> int main() { int a, b, c; printf("Enter three numbers: "); scanf("%d %d %d", &a, &b, &c); if (a >= b && a >= c) printf("Largest is %d\n", a); else if (b >= a && b >= c) printf("Largest is %d\n", b); else printf("Largest is %d\n", c); return 0; } 8. Check for Leap Year #include <stdio.h> int main() { int year; printf("Enter year: "); scanf("%d", &year); if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) printf("Leap Year\n"); else printf("Not a Leap Year\n"); return 0; } 9. Check if Number is Multiple of 3 #include <stdio.h> int main() { int num; printf("Enter number: "); scanf("%d", &num); if (num % 3 == 0) printf("Multiple of 3\n"); else printf("Not a multiple of 3\n"); return 0; } 10. Voting Eligibility #include <stdio.h> int main() { int age; printf("Enter age: "); scanf("%d", &age); if (age >= 18) printf("Eligible to vote\n"); else printf("Not eligible to vote\n"); return 0; } 11. Check if Number is Zero, Positive or Negative #include <stdio.h> int main() { int num; printf("Enter a number: "); scanf("%d", &num); if (num > 0) printf("Positive\n"); else if (num < 0) printf("Negative\n"); else printf("Zero\n"); return 0; } 12. Check Triangle Type #include <stdio.h> int main() { int a, b, c; printf("Enter three angles: "); scanf("%d %d %d", &a, &b, &c); if (a + b + c == 180) { if (a == b && b == c) printf("Equilateral Triangle\n"); else if (a == b || b == c || a == c) printf("Isosceles Triangle\n"); else printf("Scalene Triangle\n"); } else { printf("Not a valid triangle\n"); } return 0; } 13. Day of the Week #include <stdio.h> int main() { int day; printf("Enter day number (1-7): "); scanf("%d", &day); if (day == 1) printf("Sunday\n"); else if (day == 2) printf("Monday\n"); else if (day == 3) printf("Tuesday\n"); else if (day == 4) printf("Wednesday\n"); else if (day == 5) printf("Thursday\n"); else if (day == 6) printf("Friday\n"); else if (day == 7) printf("Saturday\n"); else printf("Invalid day\n"); return 0; } 14. Check if Number is Between Two Values #include <stdio.h> int main() { int num; printf("Enter number: "); scanf("%d", &num); if (num >= 10 && num <= 50) printf("Number is between 10 and 50\n"); else printf("Not in range\n"); return 0; } 15. Find the Smallest of Two Numbers #include <stdio.h> int main() { int a, b; printf("Enter two numbers: "); scanf("%d %d", &a, &b); if (a < b) printf("%d is smaller\n", a); else printf("%d is smaller\n", b); return 0; } 16. Electricity Bill Calculator #include <stdio.h> int main() { int units; printf("Enter units: "); scanf("%d", &units); if (units <= 100) printf("Bill: Rs. %d\n", units * 2); else if (units <= 200) printf("Bill: Rs. %d\n", units * 3); else printf("Bill: Rs. %d\n", units * 5); return 0; } 17. Check Divisibility by 3 and 7 #include <stdio.h> int main() { int num; printf("Enter number: "); scanf("%d", &num); if (num % 3 == 0 && num % 7 == 0) printf("Divisible by both 3 and 7\n"); else printf("Not divisible by both\n"); return 0; } 18. Check if Character is a Vowel #include <stdio.h> int main() { char ch; printf("Enter a character: "); scanf(" %c", &ch); if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') printf("Vowel\n"); else printf("Not a vowel\n"); return 0; } 19. Check if Year is Century Year #include <stdio.h> int main() { int year; printf("Enter year: "); scanf("%d", &year); if (year % 100 == 0) printf("Century year\n"); else printf("Not a century year\n"); return 0; } 20. Simple Calculator #include <stdio.h> int main() { int a, b; char op; printf("Enter two numbers: "); scanf("%d %d", &a, &b); printf("Enter operator (+ - * /): "); scanf(" %c", &op); if (op == '+') printf("Result = %d\n", a + b); else if (op == '-') printf("Result = %d\n", a - b); else if (op == '*') printf("Result = %d\n", a * b); else if (op == '/') printf("Result = %d\n", a / b); else printf("Invalid operator\n"); return 0; }