In the previous section we discussed the basics of C variables. Now let us take a look at some real-life application Problems.
1. Write a C program using variables to simulate a bank account where the user can deposit and withdraw money and display the final balance. Take initial balance as 5000.
#include
int main() {
double balance = 5000.0, deposit, withdraw;
printf("Enter deposit amount: ");
scanf("%lf", &deposit);
balance = balance + deposit;
printf("Enter withdrawal amount: ");
scanf("%lf", &withdraw);
if (withdraw <= balance)
balance = balance - withdraw;
else
printf("Insufficient balance\n");
printf("Final Balance = %.2lf\n", balance);
return 0;
}
// Enter deposit amount: 5000
// Enter withdrawal amount: 2000
// Final Balance = 8000.00
2. Calculate the Gross Salary of an employee if their Basic Salary is 25,000, where HRA(House Rent Allowance) is 20% and DA(Dearness Allowance) is 10% of the basic.
console.log( 'Code is Poetry' );#include
int main() {
float basic, hra, da, gross;
printf("Enter basic salary: ");
scanf("%f", &basic);
hra = 0.20 * basic;
da = 0.10 * basic;
gross = basic + hra + da;
printf("Gross Salary = %.2f\n", gross);
return 0;
}
//Enter basic salary: 25000
// Gross Salary = 32500.00
3.Write a C program using variables to calculate the percentage of a student from three subject marks.
#include
int main() {
int s1, s2, s3;
float percentage;
printf("Enter the marks of 3 subjects: ");
scanf("%d %d %d", &s1, &s2, &s3);
percentage = (s1 + s2 + s3) / 3.0;
printf("Percentage = %.2f\n", percentage);
return 0;
}
// Enter the marks of 3 subjects: 85 84 65
// Percentage = 78.00
4. Write a C program to convert temperature from Celsius to Fahrenheit using variables.
#include
int main() {
float celsius, fahrenheit;
printf("Enter temperature in Celsius: ");
scanf("%f", &celsius);
fahrenheit = (celsius * 9 / 5) + 32;
printf("Temperature in Fahrenheit = %.2f\n", fahrenheit);
return 0;
}
// Enter temperature in Celsius: 28
// Temperature in Fahrenheit = 82.40
5. Calculate the electricity bill for a household that consumed 150 units, where the first 100 units are charged at $1.50 each and any additional units are charged at $2.50 each.
#include
int main() {
int units;
float bill;
printf("Enter units consumed: ");
scanf("%d", &units);
if (units <= 100)
bill = units * 1.5;
else
bill = 100 * 1.5 + (units - 100) * 2.5;
printf("Total Bill = %.2f\n", bill);
return 0;
}
// Enter units consumed: 150
// Total Bill = 275.00
6. Write a C program using variables to calculate the area of a rectangle.
#include
int main() {
float length, width, area;
printf("Enter length and width: ");
scanf("%f %f", &length, &width);
area = length * width;
printf("Area of Rectangle = %.2f\n", area);
return 0;
}
// Enter length and width: 50 25
// Area of Rectangle = 1250.00
7. Write a C program using variables and constants to convert rupees to dollars.
#include
int main() {
float rupees, dollars;
const float rate = 0.012;
printf("Enter amount in rupees: ");
scanf("%f", &rupees);
dollars = rupees * rate;
printf("Amount in Dollars = %.2f\n", dollars);
return 0;
}
// Enter amount in rupees: 200
// Amount in Dollars = 2.40
8. Write a C program using variables to calculate remaining mobile data after usage and display whether the data pack is ACTIVE or EXHAUSTED.
#include
int main() {
float totalData, usedData, remainingData;
printf("Enter total data (GB):");
scanf("%f", &totalData);
printf("Enter used data (GB):");
scanf("%f", &usedData);
remainingData=totalData- usedData;
if (remainingData > 0)
printf("Remaining Data = %.2f GB\nStatus: ACTIVE\n", remainingData);
else
printf("Remaining Data = 0.00 GB\nStatus: EXHAUSTED\n");
return 0;
}
// Enter total data (GB): 2
// Enter used data (GB): 1.5
// Remaining Data = 0.50 GB
// Status: ACTIVE
9. Write a C program to count the number of times a function has been executed by using a static variable.
#include
void counter() {
static int count = 0;
count++;
printf("Function called %d times\n", count);
}
int main() {
counter();
counter();
counter();
return 0;
}
//Function called 1 times
// Function called 2 times
// Function called 3 times