Skip to content

Week1: Sequential Programming in C

Q1. Average and Percentage

Question:
Write a C program to accept the marks obtained by a student in 5 courses and calculate the average and percentage. (Consider maximum marks in a course is 100).

Program:

#include <stdio.h>

int main() {
    float marks1, marks2, marks3, marks4, marks5;
    float sum, average, percentage;

    printf("Enter the marks obtained in 5 courses:\n");
    scanf("%f %f %f %f %f", &marks1, &marks2, &marks3, &marks4, &marks5);

    sum = marks1 + marks2 + marks3 + marks4 + marks5;

    average = sum / 5.0;
    percentage = (sum / 500.0) * 100;

    printf("Average: %.2f\n", average);
    printf("Percentage: %.2f%%\n", percentage);

    return 0;
}
````

---

## Q2. Fahrenheit to Celsius

**Question:**
Write a C program that reads a temperature value in Fahrenheit from the user, converts it to Celsius using the formula
`celsius = (Fahrenheit - 32) * 5 / 9`
and displays the converted temperature in Celsius.

**Program:**

```c
#include <stdio.h>

int main() {
    float fh, cl;
    printf("Enter temperature in Fahrenheit: ");
    scanf("%f", &fh);
    cl = (fh - 32) * 5 / 9;
    printf("Temperature in Celsius: %.2f\n", cl);
    return 0;
}

Q3. Total Salary Calculation

Question: The total salary of any employee is a combination of basic, HRA, and allowance. The HRA is 40% of Basic. The basic and allowance will be accepted by the user. Write a C program to calculate the total salary and display it.

Program:

#include <stdio.h>

int main() {
    float basic, allowance, hra, total_salary;

    printf("Enter the basic salary: ");
    scanf("%f", &basic);

    printf("Enter the allowance: ");
    scanf("%f", &allowance);

    hra = 0.40 * basic;
    total_salary = basic + hra + allowance;

    printf("Total salary: %.2f\n", total_salary);

    return 0;
}

Q4. Simple Interest Calculation

Question: Write a C program that reads the principal amount, rate of interest, and time in years from the user, calculates the simple interest using the formula simple_interest = (principal * rate * time) / 100 and displays the result.

Program:

#include <stdio.h>

int main() {
    float principal, rate, time, simple_interest;

    printf("Enter the principal amount: ");
    scanf("%f", &principal);

    printf("Enter the rate of interest: ");
    scanf("%f", &rate);

    printf("Enter the time in years: ");
    scanf("%f", &time);

    simple_interest = (principal * rate * time) / 100;

    printf("Simple interest: %.2f\n", simple_interest);

    return 0;
}

Comments