Logic Building with C If-Else: Practice Problems & Solutions

In the previous section we discussed the basics of C if – else Statements. Now let us take a look at some real-life application Problems.

 

1. Student Grade & Scholarship System

A university wants to automate its result processing system.
Write a C program to read the marks of five subjects for a student, calculate the average, and display the grade based on the following conditions:

  • Average >= 85 -> Grade A and Scholarship Eligible

  • Average >= 60 -> Grade B

  • Average >= 40 -> Grade C

  • Otherwise -> Fail

				
					#include <stdio.h>
int main() {
    int i, marks[5], sum = 0;
    float avg;

    for(i = 0; i < 5; i++) {
        printf("Enter marks %d: ", i+1);
        scanf("%d", &marks[i]);
        sum += marks[i];
    }
    avg = sum / 5.0;

    if (avg >= 85)
        printf("Grade A - Scholarship Eligible");
    else if (avg >= 60)
        printf("Grade B");
    else if (avg >= 40)
        printf("Grade C");
    else
        printf("Fail");

    return 0;
}

// Enter marks 1: 52
// Enter marks 2: 72
// Enter marks 3: 63
// Enter marks 4: 58
// Enter marks 5: 74
// Grade B

				
			

2. Bank Loan Eligibility System

A bank provides loans only to customers who satisfy certain conditions. Write a C program to check loan eligibility based on the following criteria:

  • Their age must be between 24 and 65
  • Should have a monthly salary of ₹25,000 or above
  • Credit score must be 500 or more

Display whether the loan is approved or rejected along with the reason.

				
					#include <stdio.h>

int main() {
    int age, credit;
    float salary;

    printf("Enter age, salary, credit score: ");
    scanf("%d %f %d", &age, &salary, &credit);

    if (age >= 24 && age <= 65) {
        if (salary >= 25000 && credit >= 500)
            printf("Loan Approved");
        else
            printf("Loan Rejected: Financial Criteria Not Met");
    } else {
        printf("Loan Rejected: Age Criteria Not Met");
    }

    return 0;
}

// Enter age, salary, credit score: 25 20000 800
// Loan Rejected: Financial Criteria Not Met

				
			

3. Electricity Bill Calculator

An electricity board calculates the monthly bill based on unit consumption as follows:

  • First 100 units -> ₹2 per unit
  • Next 100 units -> ₹3 per unit
  • Above 200 units -> ₹5 per unit

Write a C program to read the number of units consumed and calculate the total electricity bill.

				
					#include <stdio.h>

int main() {
    int units;
    float bill;

    printf("Enter units consumed: ");
    scanf("%d", &units);

    if (units <= 100)
        bill = units * 2;
    else if (units <= 200)
        bill = 100 * 2 + (units - 100) * 3;
    else
        bill = 100 * 2 + 100 * 3 + (units - 200) * 5;

    printf("Total Bill = %.2f", bill);
    return 0;
}

// Enter units consumed: 155
// Total Bill = 365.00

				
			

4. Vehicle Insurance Premium System

An insurance company calculates vehicle insurance premiums based on the type of vehicle and age of the vehicle.

Vehicle Type:

  • 1 -> Car
  • 2 -> Bike

Premium rules vary based on whether the vehicle’s age is less than or equal to 5 years or more than 5 years.
Write a C program to display the insurance premium amount.

				
					#include <stdio.h>

int main() {
    int type, age;

    printf("Enter vehicle type (1-Car, 2-Bike): ");
    scanf("%d", &type);

    printf("Enter vehicle age: ");
    scanf("%d", &age);

    if (type == 1) {
        if (age <= 5)
            printf("Premium: ₹5000");
        else
            printf("Premium: ₹8000");
    } else if (type == 2) {
        if (age <= 5)
            printf("Premium: ₹2000");
        else
            printf("Premium: ₹4000");
    } else {
        printf("Invalid vehicle type");
    }

    return 0;
}

// Enter vehicle type (1-Car, 2-Bike): 1
// Enter vehicle age: 6
// Premium: ₹8000

				
			

5. Login System with Limited Attempts

Design a simple login authentication system in C.
The user is allowed a maximum of three attempts to enter the correct password.

  • If the correct password is entered, display Login Successful
  • If all three attempts fail, display Account Locked

Take the default password as 1234. It should only take numbers as inut.

				
					#include <stdio.h>

int main() {
    int password, attempts = 3;

    while(attempts > 0) {
        printf("Enter password: ");
        
        if (scanf("%d", &password) != 1) {
            printf("Invalid input! Numbers only.\n");
            scanf("%*s"); 
            continue;
        }

        if (password == 1234) {
            printf("Login Successful\n");
            return 0;
        } else {
            attempts--; 
            if (attempts > 0) {
                printf("Wrong password. %d attempts left\n\n", attempts);
            }
        }
    }

    printf("Account Locked\n");
    return 0;
}

// Enter password: 8520
// Wrong password. 2 attempts left

// Enter password: 6541
// Wrong password. 1 attempts left

// Enter password: 1234
// Login Successful

				
			

6. Hospital Patient Triage System

A hospital uses an automated triage system to assess patient condition.
Write a C program to classify a patient as:

  • Critical if heart rate > 120 or oxygen level < 90
  • Moderate if heart rate > 100
  • Stable otherwise

The program should read heart rate and oxygen level and display the patient’s condition.

				
					#include <stdio.h>

int main() {
    int heartRate, oxygen;

    printf("Enter heart rate and oxygen level: ");
    scanf("%d %d", &heartRate, &oxygen);

    if (heartRate > 120 || oxygen < 90)
        printf("Critical Condition");
    else if (heartRate > 100)
        printf("Moderate Condition");
    else
        printf("Stable Condition");

    return 0;
}

// Enter heart rate and oxygen level: 80 100
// Stable Condition

				
			

7. Online Shopping Cart Billing System

Question:
An online shopping platform provides discounts based on the total cart value.

  • Total >= ₹10,000 -> 15% discount
  • Total >= ₹5,000 -> 10% discount

Write a C program to read the number of items and price of each item, calculate the total amount, apply discount if applicable, and display the final payable amount.

				
					#include <stdio.h>

int main() {
    int items, i;
    float price, total = 0;

    printf("Enter number of items: ");
    scanf("%d", &items);

    for(i = 1; i <= items; i++) {
        printf("Enter price of item %d: ", i);
        scanf("%f", &price);
        total += price;
    }

    if (total >= 10000)
        total *= 0.85;
    else if (total >= 5000)
        total *= 0.90;

    printf("Final Amount = %.2f", total);
    return 0;
}

// Enter number of items: 5
// Enter price of item 1: 15
// Enter price of item 2: 50
// Enter price of item 3: 85
// Enter price of item 4: 120
// Enter price of item 5: 35
// Final Amount = 305.00

				
			

8. Traffic Violation Detection System

A traffic monitoring system checks for violations based on vehicle speed and helmet usage.

  1. Speed limit is 80 km/h
  2. Helmet status:
  • 1 -> Worn
  • 0 -> Not worn

Write a C program to detect and display the type of violation such as overspeeding, helmet violation, heavy fine or no violation.

				
					#include <stdio.h>

int main() {
    int speed, helmet;

    printf("Enter speed and helmet status (1-Worn, 0-Not): ");
    scanf("%d %d", &speed, &helmet);

    if (speed > 80 && helmet == 0)
        printf("Heavy Fine");
    else if (speed > 80)
        printf("Overspeeding Fine");
    else if (helmet == 0)
        printf("Helmet Violation");
    else
        printf("No Violation");

    return 0;
}

// Enter speed and helmet status (1-Worn, 0-Not): 85
// 1
// Overspeeding Fine