0% found this document useful (0 votes)
10 views33 pages

C Programs for Basic Calculations

The document contains a series of C programs that perform various tasks, such as finding maximum numbers, checking if a number is positive or negative, determining leap years, and validating triangles. Each program includes user input, conditional checks, and outputs the results based on the logic implemented. The programs cover a wide range of programming concepts and problem-solving techniques in C.

Uploaded by

Rakesh Singh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views33 pages

C Programs for Basic Calculations

The document contains a series of C programs that perform various tasks, such as finding maximum numbers, checking if a number is positive or negative, determining leap years, and validating triangles. Each program includes user input, conditional checks, and outputs the results based on the logic implemented. The programs cover a wide range of programming concepts and problem-solving techniques in C.

Uploaded by

Rakesh Singh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

01 write a c program to find maximum between two numbers

#include <stdio.h>

int main() {

int num1, num2;

// Input two numbers from the user

printf("Enter first number: ");

scanf("%d", &num1);

printf("Enter second number: ");

scanf("%d", &num2);

// Compare and find the maximum

if (num1 > num2)

printf("Maximum = %d\n", num1);

else if (num2 > num1)

printf("Maximum = %d\n", num2);

else

printf("Both numbers are equal.\n");

return 0;

}
02 write a c program to find maximum between three numbers
#include <stdio.h>

int main() {

int num1, num2, num3;

// Input three numbers from the user

printf("Enter first number: ");

scanf("%d", &num1);

printf("Enter second number: ");

scanf("%d", &num2);

printf("Enter third number: ");

scanf("%d", &num3);

// Compare and find the maximum

if (num1 >= num2 && num1 >= num3)

printf("Maximum = %d\n", num1);

else if (num2 >= num1 && num2 >= num3)

printf("Maximum = %d\n", num2);

else
printf("Maximum = %d\n", num3);

return 0;}
03 write a c program check whether a number is negative positive or
zero.
#include <stdio.h>

int main() {

int num;

// Input a number from the user

printf("Enter a number: ");

scanf("%d", &num);

// Check if the number is positive, negative or zero

if (num > 0)

printf("The number is positive.\n");

else if (num < 0)

printf("The number is negative.\n");

else

printf("The number is zero.\n");

return 0;

}
04 write a c program check whether a number is divisible by 5 and 11
or not.
#include <stdio.h>

int main() {

int num;

// Input a number from the user

printf("Enter a number: ");

scanf("%d", &num);

// Check divisibility by 5 and 11

if (num % 5 == 0 && num % 11 == 0)

printf("%d is divisible by both 5 and 11.\n", num);

else

printf("%d is not divisible by both 5 and 11.\n", num);

return 0;

}
05 write a c program check whether a number is even or odd.
#include <stdio.h>

int main() {

int num;

// Input from user

printf("Enter an integer: ");

scanf("%d", &num);

// Check if the number is even or odd

if (num % 2 == 0)

printf("%d is even.\n", num);

else

printf("%d is odd.\n", num);

return 0;

}
06 write a c program check whether a year is leap year or not.
#include <stdio.h>

int main() {

int year;

// Input from user

printf("Enter a year: ");

scanf("%d", &year);

// Check if the year is a leap year

if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))

printf("%d is a leap year.\n", year);

else

printf("%d is not a leap year.\n", year);

return 0;

}
07 write a c program check whether a character is alphabet or not.
#include <stdio.h>

int main() {

char ch;

// Input from user

printf("Enter a character: ");

scanf("%c", &ch);

// Check whether the character is an alphabet

if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))

printf("%c is an alphabet.\n", ch);

else

printf("%c is not an alphabet.\n", ch);

return 0;

}
08 write a c program to input any alphabet and check whether it is
vowel or consonant.
#include <stdio.h>

int main() {

char ch;

// Input from user

printf("Enter an alphabet: ");

scanf("%c", &ch);

// Check whether input is an alphabet

if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {

// Check for vowels

if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U' ||

ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')

printf("%c is a vowel.\n", ch);

else

printf("%c is a consonant.\n", ch);

} else {

printf("Invalid input! Please enter an alphabet.\n");

} return 0;}
9 write a c program to input any character and check whether it is
alphabet digit or special character.
#include <stdio.h>

int main() {

char ch;

// Input from user

printf("Enter any character: ");

scanf("%c", &ch);

// Check the type of character

if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))

printf("%c is an alphabet.\n", ch);

else if (ch >= '0' && ch <= '9')

printf("%c is a digit.\n", ch);

else

printf("%c is a special character.\n", ch);

return 0;

}
10 write a c program check whether a character is uppercase or
lowercase alphabet.
#include <stdio.h>

int main() {

char ch;

// Input from user

printf("Enter any character: ");

scanf("%c", &ch);

// Check whether character is uppercase or lowercase

if (ch >= 'A' && ch <= 'Z')

printf("%c is an uppercase alphabet.\n", ch);

else if (ch >= 'a' && ch <= 'z')

printf("%c is a lowercase alphabet.\n", ch);

else

printf("%c is not an alphabet.\n", ch);

return 0;

}
11 write a c program to input week number and print week day.
#include <stdio.h>

int main() {

int week;

// Input from user

printf("Enter week number (1-7): ");

scanf("%d", &week);

// Check and print weekday

switch (week) {

case 1:

printf("Monday\n");

break;

case 2:

printf("Tuesday\n");

break;

case 3:

printf("Wednesday\n");

break;

case 4:
printf("Thursday\n");

break;

case 5:

printf("Friday\n");

break;

case 6:

printf("Saturday\n");

break;

case 7:

printf("Sunday\n");

break;

default:

printf("Invalid input! Please enter a number between 1 and 7.\n");

return 0;

}
12 write a c program to input month number and print number of
days in that month.
#include <stdio.h>

int main() {

int month;

// Input from user

printf("Enter month number (1-12): ");

scanf("%d", &month);

// Check and print number of days

switch (month) {

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12:

printf("31 days\n");

break;
case 4:

case 6:

case 9:

case 11:

printf("30 days\n");

break;

case 2:

printf("28 or 29 days (depending on leap year)\n");

break;

default:

printf("Invalid input! Please enter a number between 1 and 12.\n");

return 0;

}
13. Write a C program to count total number of notes in given
amount.
#include <stdio.h>

int main() {

int amount;

int notes2000, notes500, notes200, notes100, notes50, notes20, notes10,


notes5, notes2, notes1;

// Input amount from user

printf("Enter the amount: ");

scanf("%d", &amount);

// Calculate number of notes for each denomination

notes2000 = amount / 2000;

amount = amount % 2000;

notes500 = amount / 500;

amount = amount % 500;

notes200 = amount / 200;

amount = amount % 200;


notes100 = amount / 100;

amount = amount % 100;

notes50 = amount / 50;

amount = amount % 50;

notes20 = amount / 20;

amount = amount % 20;

notes10 = amount / 10;

amount = amount % 10;

notes5 = amount / 5;

amount = amount % 5;

notes2 = amount / 2;

amount = amount % 2;

notes1 = amount / 1;

// Display result
printf("\nTotal number of notes:\n");

printf("2000 = %d\n", notes2000);

printf("500 = %d\n", notes500);

printf("200 = %d\n", notes200);

printf("100 = %d\n", notes100);

printf("50 = %d\n", notes50);

printf("20 = %d\n", notes20);

printf("10 = %d\n", notes10);

printf("5 = %d\n", notes5);

printf("2 = %d\n", notes2);

printf("1 = %d\n", notes1);

return 0;

}
14. Write a C program to input angles of a triangle and check whether
triangle is valid or not.
#include <stdio.h>

int main() {

float angle1, angle2, angle3, sum;

// Input three angles of the triangle

printf("Enter three angles of a triangle: ");

scanf("%f %f %f", &angle1, &angle2, &angle3);

// Calculate the sum of angles

sum = angle1 + angle2 + angle3;

// Check whether the triangle is valid

if (sum == 180 && angle1 > 0 && angle2 > 0 && angle3 > 0)

printf("The triangle is valid.\n");

else

printf("The triangle is not valid.\n");

return 0;

}
15. Write a C program to input all sides of a triangle and check
whether triangle is valid or not.
#include <stdio.h>

int main() {

float side1, side2, side3;

// Input sides of the triangle

printf("Enter three sides of a triangle: ");

scanf("%f %f %f", &side1, &side2, &side3);

// Check whether triangle is valid using the triangle inequality theorem

if ((side1 + side2 > side3) &&

(side2 + side3 > side1) &&

(side1 + side3 > side2))

printf("The triangle is valid.\n");

else

printf("The triangle is not valid.\n");

return 0;

}
16. Write a C program to check whether the triangle is equilateral,
isosceles or scalene triangle.
#include <stdio.h>

int main() {

float side1, side2, side3;

// Input three sides of a triangle

printf("Enter three sides of a triangle: ");

scanf("%f %f %f", &side1, &side2, &side3);

// Check if the triangle is valid

if ((side1 + side2 > side3) &&

(side2 + side3 > side1) &&

(side1 + side3 > side2)) {

// Determine the type of triangle

if (side1 == side2 && side2 == side3)

printf("The triangle is Equilateral.\n");

else if (side1 == side2 || side2 == side3 || side1 == side3)

printf("The triangle is Isosceles.\n");

else
printf("The triangle is Scalene.\n");

} else {

printf("The triangle is not valid.\n");

return 0;

17. Write a C program to find all roots of a quadratic equation.

#include <stdio.h>

#include <math.h>

int main() {

float a, b, c, discriminant, root1, root2, realPart, imagPart;

// Input coefficients

printf("Enter coefficients a, b and c: ");

scanf("%f %f %f", &a, &b, &c);

// Calculate discriminant

discriminant = b * b - 4 * a * c;
// Check the nature of roots

if (discriminant > 0) {

// Two distinct real roots

root1 = (-b + sqrt(discriminant)) / (2 * a);

root2 = (-b - sqrt(discriminant)) / (2 * a);

printf("Roots are real and different.\n");

printf("Root1 = %.2f\n", root1);

printf("Root2 = %.2f\n", root2);

else if (discriminant == 0) {

// Two equal real roots

root1 = root2 = -b / (2 * a);

printf("Roots are real and equal.\n");

printf("Root1 = Root2 = %.2f\n", root1);

else {

// Complex roots

realPart = -b / (2 * a);

imagPart = sqrt(-discriminant) / (2 * a);

printf("Roots are complex and different.\n");

printf("Root1 = %.2f + %.2fi\n", realPart, imagPart);


printf("Root2 = %.2f - %.2fi\n", realPart, imagPart);

return 0;

}
18. Write a C program to calculate profit or loss.
#include <stdio.h>

int main() {

float costPrice, sellingPrice, profit, loss;

// Input cost price and selling price

printf("Enter Cost Price: ");

scanf("%f", &costPrice);

printf("Enter Selling Price: ");

scanf("%f", &sellingPrice);

// Calculate profit or loss

if (sellingPrice > costPrice) {

profit = sellingPrice - costPrice;

printf("Profit = %.2f\n", profit);

else if (costPrice > sellingPrice) {

loss = costPrice - sellingPrice;

printf("Loss = %.2f\n", loss);

else {
printf("No Profit, No Loss.\n");

return 0;

}
19. Write a C program to input marks of five subjects Physics,
Chemistry, Biology, Mathematics and Computer. Calculate percentage
and grade according to following:

Percentage >= 90%: Grade A

Percentage >= 80%: Grade B

Percentage >= 70%: Grade C

Percentage >= 60%: Grade D

Percentage >= 40%: Grade E

#include <stdio.h>

int main() {

float physics, chemistry, biology, math, computer;

float total, percentage;

// Input marks of five subjects

printf("Enter marks of Physics: ");

scanf("%f", &physics);

printf("Enter marks of Chemistry: ");

scanf("%f", &chemistry);

printf("Enter marks of Biology: ");

scanf("%f", &biology);

printf("Enter marks of Mathematics: ");


scanf("%f", &math);

printf("Enter marks of Computer: ");

scanf("%f", &computer);

// Calculate total and percentage

total = physics + chemistry + biology + math + computer;

percentage = (total / 500) * 100; // assuming each subject is out of 100

// Display percentage

printf("\nTotal Marks = %.2f\n", total);

printf("Percentage = %.2f%%\n", percentage);

// Determine grade based on percentage

if (percentage >= 90)

printf("Grade: A\n");

else if (percentage >= 80)

printf("Grade: B\n");

else if (percentage >= 70)

printf("Grade: C\n");

else if (percentage >= 60)

printf("Grade: D\n");

else if (percentage >= 40)


printf("Grade: E\n");

else

printf("Grade: F (Fail)\n");

return 0;

}
20. Write a C program to input basic salary of an employee and
calculate its Gross salary according to following:

Percentage < 40%: Grade F

Basic Salary <= 10000: HRA 20%, DA = 80%

Basic Salary <= 20000: HRA = 25%, DA = 90%

Basic Salary > 20000: HRA = 30%, DA = 95%

#include <stdio.h>

int main() {

float basic, hra, da, gross;

// Input basic salary

printf("Enter basic salary of the employee: ");

scanf("%f", &basic);

// Calculate HRA and DA based on basic salary

if (basic <= 10000) {

hra = basic * 0.20; // 20% of basic

da = basic * 0.80; // 80% of basic

}
else if (basic <= 20000) {

hra = basic * 0.25; // 25% of basic

da = basic * 0.90; // 90% of basic

else {

hra = basic * 0.30; // 30% of basic

da = basic * 0.95; // 95% of basic

// Calculate gross salary

gross = basic + hra + da;

// Display results

printf("\nBasic Salary = %.2f\n", basic);

printf("HRA = %.2f\n", hra);

printf("DA = %.2f\n", da);

printf("Gross Salary = %.2f\n", gross);

return 0;

}
21. Write a C program to input electricity unit charges and calculate
total electricity bill according to the given
condition:

For first 50 units Rs. 0.50/unit

For next 100 units Rs. 0.75/unit

For next 100 units Rs. 1.20/unit

For unit above 250 Rs. 1.50/unit

An additional surcharge of 20% is added to the bill

#include <stdio.h>

int main() {

float units, amount, totalAmount, surcharge;

// Input electricity units

printf("Enter total electricity units consumed: ");

scanf("%f", &units);

// Calculate electricity bill according to given slabs

if (units <= 50)

amount = units * 0.50;


else if (units <= 150)

amount = (50 * 0.50) + ((units - 50) * 0.75);

else if (units <= 250)

amount = (50 * 0.50) + (100 * 0.75) + ((units - 150) * 1.20);

else

amount = (50 * 0.50) + (100 * 0.75) + (100 * 1.20) + ((units - 250) * 1.50);

// Add 20% surcharge

surcharge = amount * 0.20;

totalAmount = amount + surcharge;

// Display result

printf("\nElectricity Bill Details:\n");

printf("Units Consumed = %.2f\n", units);

printf("Amount Charges = Rs. %.2f\n", amount);

printf("Surcharge (20%%) = Rs. %.2f\n", surcharge);

printf("Total Electricity Bill = Rs. %.2f\n", totalAmount);

return 0;

You might also like