Here we covered C programming if else program, like year is leap year or not, number is even or odd, mini projects like calculating electricity bill, finding grade of a student, etc., which will help the learner to learn the C programming language very easily. We can also provide PDF format to download that C program. To know What is C programming language?
C Programming if-else Program (PDF)
C Programming if-else Program Download (PDF)
Following are the list of C programming if-else program:
Table of Content
- Check whether a number is Even or Odd.
- Find the Largest of Two Numbers.
- Check year is Leap year or not.
- Check whether number is Positive or Negative.
- Find Grade of a Student.
- Calculate Profit or Loss in a Company.
- Calculate Electricity Bill.
- Calculate Gross Salary of an Employee.
- Find a Number which is divided by 5 and 11.
- Check character is Alphabet or Digit.
- Check character is Vowel or Consonant.
- Create a Menu System like [1] Add, [2] Edit, [3] Delete, [4] Exit.
1. Check whether a number is Even or Odd.
Program
#include<stdio.h> void main() { int num; printf("Enter an Integer Value = "); scanf("%d", &num); if (num%2 == 0) printf("%d is Even Number \n", num); else printf("%d is Odd Number \n", num); }
Output
Enter an Integer Value = 65 65 is Odd Number
2. Find the Largest of Two Numbers.
Program
#include<stdio.h> void main() { int a, b; printf("Enter Two Integer Value = "); scanf("%d %d", &a, &b); if(a>b) { printf("%d is Largest\n", a); } else if(b>a) { printf("%d is Largest\n", b); } else { printf("Both are Equal\n"); } }
Output
Enter Two Integer Value = 1 0 1 is Largest
3. Check year is Leap year or not.
Program
#include<stdio.h> void main() { int year; printf("Enter Year = "); scanf("%d", &year); if (( year%400 == 0)|| (( year%4 == 0 ) &&( year%100 != 0))) printf("%d is a Leap Year", year); else printf("%d is not a Leap Year", year); }
Output
Enter Year = 1775 1775 is not a Leap Year
4. Check whether number is Positive or Negative.
Program
#include<stdio.h> void main() { int num; printf("Enter Integer Number = "); scanf("%d",&num); if (num >= 0) { if (num > 0) printf("%d is Positive", num); else printf("You have entered value zero."); } else printf("%d is Negative", num); }
Output
Enter Integer Number = -4 -4 is Negative
5. Find Grade of a Student.
Program
#include<stdio.h> void main() { int english, chemistry, computers, physics, maths; float total, percentage; printf("Enter the Five Subjects Marks = "); scanf("%d%d%d%d%d", &english, &chemistry, &computers, &physics, &maths); total = english + chemistry + computers + physics + maths; percentage = (total / 500) * 100; printf("Total Marks = %.2f\n", total); printf("Marks Percentage = %.2f", percentage); if(percentage >= 90) { printf("\n Grade A"); } else if(percentage >= 80) { printf("\n Grade B"); } else if(percentage >= 70) { printf("\n Grade C"); } else if(percentage >= 60) { printf("\n Grade D"); } else if(percentage >= 40) { printf("\n Grade E"); } else { printf("\n Fail"); } }
Output
Enter the Five Subjects Marks = 44 66 88 99 33 Total Marks = 330.00 Marks Percentage = 66.00 Grade D
6. Calculate Profit or Loss in a Company.
Program
#include<stdio.h> void main() { float product_cost, sales_amount, amount; printf("Enter Product Cost = "); scanf("%f", &product_cost); printf("\nEnter Sales Price = "); scanf("%f", &sales_amount); if (sales_amount > product_cost) { amount = sales_amount - product_cost; printf("\n Profit Amount = %.4f", amount); } else if(product_cost > sales_amount) { amount = product_cost - sales_amount; printf("\n Loss Amount = %.4f", amount); } else printf("\n No Profit No Loss!"); }
Output
Enter Product Cost = 987 Enter Sales Price = 1000 Profit Amount = 13.0000
7. Calculate Electricity Bill.
Bill Unit Charges:
- First 50 Units Charge is Rs. 0.50/unit.
- For Next 100 Units Charge is Rs. 0.75/unit.
- For Next 100 Units Charge is Rs. 1.20/unit.
- For Unit Above 250 Charge is Rs. 1.50/unit.
- An Additional Surcharge of 20% is added to the bill.
Program
#include<stdio.h> void main() { int unit; float amt, total_amt, sur_charge; printf("Enter total units consumed = "); scanf("%d", &unit); if(unit <= 50) { amt = unit * 0.50; } else if(unit <= 150) { amt = 25 + ((unit-50) * 0.75); } else if(unit <= 250) { amt = 100 + ((unit-150) * 1.20); } else { amt = 220 + ((unit-250) * 1.50); } sur_charge = amt * 0.20; total_amt = amt + sur_charge; printf("Electricity Bill = Rs. %.2f", total_amt); }
Output
Enter total units consumed = 35 Electricity Bill = Rs. 21.00
8. Calculate Gross Salary of an Employee.
Program
#include<stdio.h> void main() { float basic_salary, HRA, DA, gross_salary; printf("Enter the Basic Salary of an Employee = "); scanf("%f", &basic_salary); if (basic_salary <= 10000) { HRA = (basic_salary * 6) / 100; DA = (basic_salary * 10) / 100; } else if (basic_salary <= 20000) { HRA = (basic_salary * 12) / 100; DA = (basic_salary * 20) / 100; } else { HRA = (basic_salary * 18) / 100; DA = (basic_salary * 30) / 100; } gross_salary = basic_salary + HRA + DA; printf("Gross Salary of Employee = %.2f", gross_salary); }
Output
Enter the Basic Salary of an Employee = 4000 Gross Salary of Employee = 4640.00
9. Find a Number which is divided by 5 and 11.
Program
#include<stdio.h> void main() { int num; printf("Enter an Integer Value = "); scanf("%d", &num); if ((num % 5 == 0) && (num % 11 == 0)) printf("%d is Divisible by 5 and 11", num); else printf("%d is Not Divisible by 5 and 11", num); }
Output
Enter an Integer Value = 55 55 is Divisible by 5 and 11
10. Check character is Alphabet or Digit.
Program
#include<stdio.h> void main() { char ch; printf("Enter one Character or Digit = "); scanf("%c", &ch); if( (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') ) { printf("%c is an Alphabet", ch); } else if (ch >= '0' && ch <= '9') { printf("%c is a Digit", ch); } else printf("%c is not an Alphabet, or a Digit", ch); }
Output
Enter one Character or Digit = z z is an Alphabet
11. Check character is Vowel or Consonant.
Program
#include<stdio.h> void main() { char ch; printf("Enter one Alphabet = "); scanf(" %c", &ch); if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') { printf("%c is a Vowel.", ch); } else { printf("%c is a Consonant.", ch); } }
Output
Enter one Alphabet = Y Y is a Consonant.
12. Create a Menu System like [1] Add, [2] Edit, [3] Delete, [4] Exit.
Program
#include<stdio.h> void main() { int ch; printf("[1] ADD"); printf("\n[2] Edit"); printf("\n[3] Delete"); printf("\n[4] Exit"); printf("\nEnter your choice = "); scanf("%d",&ch); if(ch==1) { printf("Add option selected"); } else if(ch==2) { printf("Edit option selected"); } else if(ch==3) { printf("Delete option selected"); } else if(ch==4) { printf("Exit option selected"); } else { printf("Invalid Entry"); } }
Output
[1] ADD [2] Edit [3] Delete [4] Exit Enter your choice = 3 Delete option selected
Note:
- All the programs are compiled and executed in Dev C++ code editor and save with .c extension.
- %.2f is used to print fractional values only up to 2 decimal places. You can also use %f to print fractional values normally up to six decimal places.
More Topics
To Download C Programming Examples Click Here