C programming serves as a foundational milestone for many individuals navigating the programming landscape. It is an optimal choice for gaining insights into the intricacies of programming and comprehending the internal workings of computer programs.
Given its proximity to low-level programming, venturing into C can be a daunting prospect for beginners unless approached with deliberate practice. However, fear not! At Pivot Edu Unit , we offer a systematic, step-by-step approach to learning and practicing C programming.
Within this programming exercise, our primary focus lies on the fundamentals of C programming. Upon completion, you will have acquired a solid understanding of the basic structure and semantics inherent in C programs. Moreover, you’ll be adept at crafting mathematical programs using the C language.
If-else and Switch Based Questions
If-else based Questions
Write a program to check whether a given number is positive or negative.
#include <stdio.h>
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (number > 0) {
printf("The number is positive.\n");
} else if (number < 0) {
printf("The number is negative.\n");
} else {
printf("The number is zero.\n");
}
return 0;
}
Write a program to find the maximum of two numbers.
#include <stdio.h>
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
if (num1 > num2) {
printf("The maximum is: %d\n", num1);
} else {
printf("The maximum is: %d\n", num2);
}
return 0;
}
Write a program to determine if a given year is a leap year.
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if (year % 4 == 0) {
if (year % 100 != 0) {
printf("%d is a leap year.\n", year);
} else {
if (year % 400 == 0) {
printf("%d is a leap year.\n", year);
} else {
printf("%d is not a leap year.\n", year);
}
}
} else {
printf("%d is not a leap year.\n", year);
}
return 0;
}
Write a program to check if a number is divisible by both 5 and 11.
#include <stdio.h>
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (number % 5 == 0 && number % 11 == 0) {
printf("%d is divisible by both 5 and 11.\n", number);
} else {
printf("%d is not divisible by both 5 and 11.\n", number);
}
return 0;
}
Write a program to find the largest among three numbers.
#include <stdio.h>
int main() {
int num1, num2, num3;
// Input three numbers
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);
// Compare and find the largest without using &&
if (num1 >= num2) {
if (num1 >= num3) {
printf("%d is the largest among the three numbers.\n", num1);
} else {
printf("%d is the largest among the three numbers.\n", num3);
}
} else {
if (num2 >= num3) {
printf("%d is the largest among the three numbers.\n", num2);
} else {
printf("%d is the largest among the three numbers.\n", num3);
}
}
return 0;
}
Write a program to check if a number is even or odd.
#include <stdio.h>
int main() {
int number;
// Input a number
printf("Enter a number: ");
scanf("%d", &number);
// Check if the number is even or odd
if (number % 2 == 0) {
printf("%d is an even number.\n", number);
} else {
printf("%d is an odd number.\n", number);
}
return 0;
}
Write a C program that takes input for electricity unit charges and computes the total electricity bill based on the specified criteria:
No of Units | Price Per Unit |
0-100 | Rs.2 per unit |
100-200 | Rs.2.5 per unit |
200-300 | Rs.3.5 per unit |
300-400 | Rs.5 per unit |
400> | Rs. 6 per unit |
#include <stdio.h>
int main() {
int units;
float pricePerUnit = 2.0;
// Input the number of units
printf("Enter the number of units: ");
scanf("%d", &units);
// Adjust the price per unit based on the specified conditions
if (units > 100) {
pricePerUnit = 2.5;
}
if (units > 200) {
pricePerUnit = 3.5;
}
if (units > 300) {
pricePerUnit = 5.0;
}
if (units > 400) {
pricePerUnit = 6.0;
}
// Calculate and display the total cost
float totalCost = units * pricePerUnit;
printf("Total cost: Rs. %f\n", totalCost);
return 0;
}
Write a C program to input the usage in gallons and calculate the total water bill according to the given conditions:
- For the first 50 gallons, Rs1.00 per gallon.
- For the next 100 gallons, Rs.1.50 per gallon.
- For the next 100 gallons, Rs.2.00 per gallon.
- For gallons above 250, Rs.2.50 per gallon.
- Add an additional 15% surcharge to the final bill.
#include <stdio.h>
int main() {
float gallons, costPerGallon, totalCost;
// Input the usage in gallons
printf("Enter the usage in gallons: ");
scanf("%f", &gallons);
// Set the default cost per gallon to Rs 1.00
costPerGallon = 1.0;
// Adjust the cost per gallon based on the specified conditions
if (gallons > 250) {
costPerGallon = 2.5;
} else if (gallons > 150) {
costPerGallon = 2.0;
} else if (gallons > 50) {
costPerGallon = 1.5;
}
// Calculate the total cost and add a 15% surcharge
totalCost = gallons * costPerGallon;
totalCost += totalCost * 0.15;
// Display the total water bill
printf("Total water bill: Rs %.2f\n", totalCost);
return 0;
}
Write a C program to input the gas consumption in cubic meters and calculate the total cost according to the given conditions:
- For the first 50 cubic meters, Rs.0.75 per cubic meter.
- For the next 100 cubic meters, Rs.1.00 per cubic meter.
- For the next 150 cubic meters, Rs.1.25 per cubic meter.
- For cubic meters above 300, Rs.1.50 per cubic meter.
- Include a 10% tax on the final cost.
#include <stdio.h>
int main() {
float cubicMeters, costPerCubicMeter, totalCost;
// Input the gas consumption in cubic meters
printf("Enter the gas consumption in cubic meters: ");
scanf("%f", &cubicMeters);
// Set the default cost per cubic meter to Rs.0.75
costPerCubicMeter = 0.75;
// Adjust the cost per cubic meter based on the specified conditions
if (cubicMeters > 300) {
costPerCubicMeter = 1.5;
} else if (cubicMeters > 150) {
costPerCubicMeter = 1.25;
} else if (cubicMeters > 50) {
costPerCubicMeter = 1.0;
}
// Calculate the total cost and add a 10% tax
totalCost = cubicMeters * costPerCubicMeter;
totalCost += totalCost * 0.10;
// Display the total gas consumption cost
printf("Total gas consumption cost: Rs%f\n", totalCost);
return 0;
}
Write a C program to input the number of minutes used and calculate the total cost of a mobile plan according to the given conditions:
- For the first 100 minutes, Rs.0.50 per minute.
- For the next 150 minutes, Rs.0.40 per minute.
- For minutes above 250, Rs.0.30 per minute.
- Add an additional flat fee of Rs.5.00 to the final cost.
#include <stdio.h>
int main() {
int minutes;
float costPerMinute, totalCost;
// Input the number of minutes used
printf("Enter the number of minutes used: ");
scanf("%d", &minutes);
// Set the default cost per minute to Rs.0.50
costPerMinute = 0.50;
// Adjust the cost per minute based on the specified conditions
if (minutes > 250) {
costPerMinute = 0.30;
} else if (minutes > 100) {
costPerMinute = 0.40;
}
// Calculate the total cost and add a flat fee of Rs.5.00
totalCost = (minutes * costPerMinute) + 5.00;
// Display the total mobile plan cost
printf("Total mobile plan cost: Rs%f\n", totalCost);
return 0;
}
Write a C program to input the sales made by an employee and calculate the total salary according to the following conditions:
- Sales <= 5000: Bonus is 2% of sales.
- Sales > 5000 and <= 10000: Bonus is 5% of sales.
- Sales > 10000: Bonus is 8% of sales.
#include <stdio.h>
int main() {
float sales, bonus, totalSalary, baseSalary;
// Input the base salary and sales made by an employee
printf("Enter the base salary: ");
scanf("%f", &baseSalary);
printf("Enter the sales made by the employee: ");
scanf("%f", &sales);
// Calculate the bonus based on the specified conditions
if (sales <= 5000) {
bonus = 0.02 * sales;
} else if (sales <= 10000) {
bonus = 0.05 * sales;
} else {
bonus = 0.08 * sales;
}
// Calculate the total salary
totalSalary = baseSalary + bonus;
// Display the total salary
printf("Total salary: Rs %f\n", totalSalary);
return 0;
}
Write a C program to input the age of a person and calculate the ticket price for a movie according to the following conditions:
- Age <= 5 or >= 60: Ticket price is free.
- Age > 5 and <= 12: Ticket price is $5.00.
- Age > 12 and <= 18: Ticket price is $8.00.
- Age > 18: Ticket price is $10.00.
#include <stdio.h>
int main() {
int age;
float ticketPrice;
// Input the age of a person
printf("Enter the age of the person: ");
scanf("%d", &age);
// Calculate the ticket price based on the specified conditions
if (age <= 5) {
ticketPrice = 0.00; // Ticket is free
} else if (age >= 60) {
ticketPrice = 0.00; // Ticket is free
} else if (age > 5 && age <= 12) {
ticketPrice = 5.00;
} else if (age > 12 && age <= 18) {
ticketPrice = 8.00;
} else {
ticketPrice = 10.00;
}
// Display the ticket price
printf("Ticket price: Rs %f\n", ticketPrice);
return 0;
}
Switch Based Questions
Write a C program to print day of week name using day number using switch case.
#include <stdio.h>
int main() {
int dayNumber;
// Input the day number
printf("Enter the day number (1-7): ");
scanf("%d", &dayNumber);
// Check and print the day of the week using switch case
switch (dayNumber) {
case 1:
printf("Day of the week: Sunday\n");
break;
case 2:
printf("Day of the week: Monday\n");
break;
case 3:
printf("Day of the week: Tuesday\n");
break;
case 4:
printf("Day of the week: Wednesday\n");
break;
case 5:
printf("Day of the week: Thursday\n");
break;
case 6:
printf("Day of the week: Friday\n");
break;
case 7:
printf("Day of the week: Saturday\n");
break;
default:
printf("Invalid day number. Please enter a number between 1 and 7.\n");
}
return 0;
}
Write a C program to print name of month accoriding to the month number by taking day number from user using switch case.
#include <stdio.h>
int main() {
int dayNumber;
// Input the day number
printf("Enter the day number (1-7): ");
scanf("%d", &dayNumber);
// Check and print the day of the week using switch case
switch (dayNumber) {
case 1:
printf("Day of the week: Sunday\n");
break;
case 2:
printf("Day of the week: Monday\n");
break;
case 3:
printf("Day of the week: Tuesday\n");
break;
case 4:
printf("Day of the week: Wednesday\n");
break;
case 5:
printf("Day of the week: Thursday\n");
break;
case 6:
printf("Day of the week: Friday\n");
break;
case 7:
printf("Day of the week: Saturday\n");
break;
default:
printf("Invalid day number. Please enter a number between 1 and 7.\n");
}
return 0;
}
Write a C program to input a number and check whether the number is positive, negative or zero using switch case.
#include
int main() {
int number;
// Input the number
printf("Enter a number: ");
scanf("%d", &number);
// Check and print whether the number is positive, negative, or zero using switch-case
switch (number > 0) {
case 1:
printf("The number is positive.\n");
break;
case 0:
switch (number < 0) {
case 1:
printf("The number is negative.\n");
break;
case 0:
printf("The number is zero.\n");
break;
}
break;
}
return 0;
}
Write a C program to design a simple calculator using switch case
#include <stdio.h>
int main() {
int choice;
int num1, num2, result;
// Display menu
printf("Simple Calculator\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
printf("Enter your choice (1-4): ");
// Input choice
scanf("%d", &choice);
// Input two integers
printf("Enter first integer: ");
scanf("%d", &num1);
printf("Enter second integer: ");
scanf("%d", &num2);
// Perform calculation based on user's choice
switch (choice) {
case 1:
result = num1 + num2;
printf("Result: %d\n", result);
break;
case 2:
result = num1 - num2;
printf("Result: %d\n", result);
break;
case 3:
result = num1 * num2;
printf("Result: %d\n", result);
break;
case 4:
if (num2 != 0) {
result = num1 / num2;
printf("Result: %d\n", result);
} else {
printf("Error: Division by zero is undefined.\n");
}
break;
default:
printf("Error: Invalid choice. Please enter a number between 1 and 4.\n");
}
return 0;
}
Write a C program to design a simple calculator using switch case where the input from user needs to be as '+' for addition '-' for substraction e.t.c.
#include <stdio.h>
int main() {
int choice;
int num1, num2, result;
// Display menu
printf("Simple Calculator Menu\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
// Get choice from the user
printf("Enter your choice (1-4): ");
scanf("%d", &choice);
// Get two integer numbers from the user
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
// Perform calculation based on the choice
switch (choice) {
case 1:
result = num1 + num2;
printf("Result: %d\n", result);
break;
case 2:
result = num1 - num2;
printf("Result: %d\n", result);
break;
case 3:
result = num1 * num2;
printf("Result: %d\n", result);
break;
case 4:
if (num2 != 0) {
result = num1 / num2;
printf("Result: %d\n", result);
} else {
printf("Error: Division by zero\n");
}
break;
default:
printf("Error: Invalid choice\n");
}
return 0;
}
Write a C program to input two numbers and determine the maximum among them.
#include <stdio.h>
int main() {
int number;
// Input the number
printf("Enter a number: ");
scanf("%d", &number);
// Check and print whether the number is positive, negative, or zero using switch-case
switch (number > 0) {
case 1:
printf("The number is positive.\n");
break;
case 0:
switch (number < 0) {
case 1:
printf("The number is negative.\n");
break;
case 0:
printf("The number is zero.\n");
break;
}
break;
}
return 0;
}
Write a C program to check whether a number is even or odd using switch case.
#include <stdio.h>
int main() {
int number;
// Input the number
printf("Enter a number: ");
scanf("%d", &number);
// Check whether the number is even or odd using switch-case
switch (number % 2) {
case 0:
printf("%d is an even number.\n", number);
break;
case 1:
printf("%d is an odd number.\n", number);
break;
}
return 0;
}
Write a C program to design a simple banking application with deposite, withdraw and view balance by taking initial balance as Rs.10000 using switch case by providing menu to user.
#include <stdio.h>
int main() {
int choice;
int balance = 0;
int amount;
// Display menu
printf("Simple Banking Application\n");
printf("1. Deposit\n");
printf("2. Withdraw\n");
printf("3. View Balance\n");
printf("4. Exit\n");
// Input choice
printf("Enter your choice (1-4): ");
scanf("%d", &choice);
// Perform operations based on user's choice using switch-case
switch (choice) {
case 1:
// Deposit
printf("Enter the amount to deposit: ");
scanf("%d", &amount);
balance += amount;
printf("Deposit successful. Current balance: %d\n", balance);
break;
case 2:
// Withdraw
printf("Enter the amount to withdraw: ");
scanf("%d", &amount);
if (amount <= balance) {
balance -= amount;
printf("Withdrawal successful. Current balance: %d\n", balance);
} else {
printf("Insufficient funds. Withdrawal unsuccessful.\n");
}
break;
case 3:
// View Balance
printf("Current balance: %d\n", balance);
break;
case 4:
// Exit
printf("Exiting the program. Thank you!\n");
break;
default:
// Invalid choice
printf("Invalid choice. Please enter a number between 1 and 4.\n");
break;
}
return 0;
}