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 to read the cost of one item and the quantity purchased. Calculate and display the total cost and average cost per item using arithmetic operators.
#include
int main() {
int quantity;
float price, total, average;
printf("Enter price of one item: ");
scanf("%f", &price);
printf("Enter quantity: ");
scanf("%d", &quantity);
total = price * quantity;
average = total / quantity;
printf("Total cost = %.2f\n", total);
printf("Average cost = %.2f\n", average);
return 0;
}
// Enter price of one item: 200
// Enter quantity: 65
// Total cost = 13000.00
// Average cost = 200.00
2. Write a C program to read two integers and check whether the first number is greater than, less than, or equal to the second using relational operators.
#include
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
if (a > b)
printf("First number is greater\n");
else if (a < b)
printf("Second number is greater\n");
else
printf("Both numbers are equal\n");
return 0;
}
// Enter two numbers: 25 46
// Second number is greater
3. Write a C program to initialize a bank balance and update it after deposit and withdrawal using assignment operators (+=, -=).
#include
int main() {
float balance = 10000, deposit, withdraw;
printf("Enter deposit amount: ");
scanf("%f", &deposit);
balance += deposit;
printf("Enter withdrawal amount: ");
scanf("%f", &withdraw);
balance -= withdraw;
printf("Final Balance = %.2f\n", balance);
return 0;
}
// Enter deposit amount: 50000
// Enter withdrawal amount: 2000
// Final Balance = 58000.00
4. Write a C program to simulate a visitor counter where the count increases when a visitor enters and decreases when a visitor exits using increment and decrement operators.
#include
int main() {
int count = 0;
count++; // visitor enters
count++; // visitor enters
count--; // visitor exits
printf("Current visitors = %d\n", count);
return 0;
}
// Current visitors = 1
5. Write a C program to read two integers and perform bitwise AND, OR and XOR operations and display the results.
#include
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("AND = %d\n", a & b);
printf("OR = %d\n", a | b);
printf("XOR = %d\n", a ^ b);
return 0;
}
// Enter two numbers: 56 32
// AND = 32
// OR = 56
// XOR = 24
6. Write a C program to read a number and check whether it is positive or negative using the conditional operator.
#include
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
(num >= 0) ? printf("Positive\n") : printf("Negative\n");
return 0;
}
// Enter a number: 54
// Positive
7. Write a C program to read an integer and check whether the number is even or odd using the modulus operator.
#include
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0)
printf("Even number\n");
else
printf("Odd number\n");
return 0;
}
// Enter a number: 55
// Odd number
8. Write a C program to display the size of different data types (int, float, double, char) using the sizeof operator.
#include
int main() {
printf("Size of int = %lu bytes\n", sizeof(int));
printf("Size of float = %lu bytes\n", sizeof(float));
printf("Size of double = %lu bytes\n", sizeof(double));
printf("Size of char = %lu byte\n", sizeof(char));
return 0;
}
// Size of int = 4 bytes
// Size of float = 4 bytes
// Size of double = 8 bytes
// Size of char = 1 byte