0% found this document useful (0 votes)
2 views6 pages

C Programs for Math Calculations

The document contains multiple C programs demonstrating basic mathematical operations, including calculating the sum and average of three numbers, converting temperatures between Fahrenheit and Celsius, calculating simple interest, finding the square root of a number, and determining the area of a triangle using Heron's formula. Each program includes user input, calculations, and output formatting. Additionally, explanations and example runs are provided for clarity.

Uploaded by

Chundi Rama
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)
2 views6 pages

C Programs for Math Calculations

The document contains multiple C programs demonstrating basic mathematical operations, including calculating the sum and average of three numbers, converting temperatures between Fahrenheit and Celsius, calculating simple interest, finding the square root of a number, and determining the area of a triangle using Heron's formula. Each program includes user input, calculations, and output formatting. Additionally, explanations and example runs are provided for clarity.

Uploaded by

Chundi Rama
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

Sum and average of 3 numbers

#include <stdio.h>

int main() {

int num1, num2, num3, sum;

float average;

// Input 3 numbers

printf("Enter three numbers: ");

scanf("%d %d %d", &num1, &num2, &num3);

// Calculate sum

sum = num1 + num2 + num3;

// Calculate average

average = sum / 3.0; // use 3.0 to get float result

// Display results

printf("Sum = %d\n", sum);

printf("Average = %.2f\n", average);

return 0;

📌 Explanation:

scanf("%d %d %d", &num1, &num2, &num3); → Reads three integers.

sum = num1 + num2 + num3; → Adds the three numbers.

average = sum / 3.0; → Divides by 3.0 (float) so the result is decimal.


printf("Average = %.2f\n", average); → Prints average up to 2 decimal places.

Conversion of Fahrenheit to Celsius and vice versa

#include <stdio.h>

int main() {

float celsius, fahrenheit;

int choice;

printf("Temperature Conversion Program\n");

printf("1. Fahrenheit to Celsius\n");

printf("2. Celsius to Fahrenheit\n");

printf("Enter your choice (1 or 2): ");

scanf("%d", &choice);

if(choice == 1) {

// Fahrenheit to Celsius

printf("Enter temperature in Fahrenheit: ");

scanf("%f", &fahrenheit);

celsius = (fahrenheit - 32) * 5.0 / 9.0;

printf("Temperature in Celsius = %.2f\n", celsius);

else if(choice == 2) {

// Celsius to Fahrenheit

printf("Enter temperature in Celsius: ");

scanf("%f", &celsius);

fahrenheit = (celsius * 9.0 / 5.0) + 32;

printf("Temperature in Fahrenheit = %.2f\n", fahrenheit);

}
else {

printf("Invalid choice! Please enter 1 or 2.\n");

return 0;

📌 Formulae Used

Fahrenheit → Celsius

 Fahrenheit → Celsius

C=(F−32)×59C = \frac{(F - 32) \times 5}{9}C=9(F−32)×5

 Celsius → Fahrenheit

F=(C×9)5+32F = \frac{(C \times 9)}{5} + 32F=5(C×9)+32

Celsius → Fahrenheit

✅ Example Run:

pgsql

Copy code

Temperature Conversion Program

1. Fahrenheit to Celsius

2. Celsius to Fahrenheit

Enter your choice (1 or 2): 1

Enter temperature in Fahrenheit: 100

Temperature in Celsius = 37.78


Here’s a simple C program to calculate Simple Interest (SI):

📌 Formula

SI=P×R×T100SI = \frac{P \times R \times T}{100}SI=100P×R×T

Where:

 P = Principal amount
 R = Rate of interest (per annum)
 T = Time (in years)

✅ C Program
#include <stdio.h>

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

// Input values
printf("Enter Principal amount: ");
scanf("%f", &principal);

printf("Enter Rate of Interest (in %%): ");


scanf("%f", &rate);

printf("Enter Time (in years): ");


scanf("%f", &time);

// Calculate Simple Interest


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

// Display result
printf("Simple Interest = %.2f\n", simpleInterest);

return 0;
}

💻 Example Run
Enter Principal amount: 5000
Enter Rate of Interest (in %): 10
Enter Time (in years): 2
Simple Interest = 1000.00

C Program: Square Root of a Number


#include <stdio.h>
#include <math.h> // Required for sqrt()

int main() {
double num, result;

// Input
printf("Enter a number: ");
scanf("%lf", &num);

// Check for negative input


if(num < 0) {
printf("Square root of a negative number is not real.\n");
}
else {
// Calculate square root
result = sqrt(num);
printf("Square root of %.2lf = %.2lf\n", num, result);
}

return 0;
}

📌 Explanation

 sqrt(num) → computes the square root.


 Works only for non-negative numbers (for negatives, result is imaginary).
 math.h must be included.
 Use %lf for double in scanf and printf.

💻 Example Run
Enter a number: 25
Square root of 25.00 = 5.00

We can calculate the area of a triangle using Heron’s formula in C.

📌 Heron’s Formula

If the sides of a triangle are a, b, c:

s=(a+b+c)2s = \frac{(a + b + c)}{2}s=2(a+b+c) Area=s(s−a)(s−b)(s−c)\text{Area} = \sqrt{s(s-a)


(s-b)(s-c)}Area=s(s−a)(s−b)(s−c)
✅ C Program
#include <stdio.h>
#include <math.h> // for sqrt()

int main() {
double a, b, c, s, area;

// Input sides of triangle


printf("Enter three sides of the triangle: ");
scanf("%lf %lf %lf", &a, &b, &c);

// Check if valid triangle


if((a + b > c) && (a + c > b) && (b + c > a)) {
// Semi-perimeter
s = (a + b + c) / 2;

// Area using Heron's formula


area = sqrt(s * (s - a) * (s - b) * (s - c));

printf("Area of the triangle = %.2lf\n", area);


}
else {
printf("The given sides do not form a valid triangle.\n");
}

return 0;
}

💻 Example Run
Enter three sides of the triangle: 3 4 5
Area of the triangle = 6.00

You might also like