0% found this document useful (0 votes)
15 views11 pages

C Programs for Math Calculations

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)
15 views11 pages

C Programs for Math Calculations

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

Week 4:

a) Write a C program to display the multiplication table of a number using


loops.
b) Write a C program to implement a Menu-driven calculator using switch.
c) Write a C program which is Menu-driven Program to compute the
perimeter and area of the various geometrical
shapes(Square,RectanglemTraingle,and Circle)
d) Develop a menu driven program to compute statistical parameters (using
one dimensional array with functions): a) Mean b) Median c) Variance d)
Standard deviation e).Quit

a. Write a C program to display the multiplication table of a number


using loops.

#include <stdio.h>

main()

int num, i;

printf("Enter a number to display its multiplication table: ");

scanf("%d", &num);

printf("\nMultiplication Table of %d:\n", num);

for (i = 1; i <= 10; i++)

printf("%d x %d = %d\n", num, i, num * i);

}
b. Write a C program to implement a Menu-driven calculator using
switch.
#include <stdio.h>

main()

int choice;

float num1, num2, result;

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): ");

scanf("%d", &choice);

printf("Enter two numbers: ");

scanf("%f %f", &num1, &num2);

switch (choice)

case 1:

result = num1 + num2;

printf("Result = %.2f\n", result);

break;

case 2:

result = num1 - num2;


printf("Result = %.2f\n", result);

break;

case 3:

result = num1 * num2;

printf("Result = %.2f\n", result);

break;

case 4:

if (num2 != 0)

result = num1 / num2;

else {

printf("Error! Division by zero is not allowed.\n");

return 0;

printf("Result = %.2f\n", result);

break;

default:

printf("Invalid choice! Please select between 1 and 4.\n");

}
c. Write a C program which is Menu-driven Program to compute the
perimeter and area of the various geometrical
shapes(Square,RectanglemTraingle,and Circle)

Standard geometric formulas:

 Square:
Area = side², Perimeter = 4 × side
 Rectangle:
Area = l × b, Perimeter = 2(l + b)
 Triangle (assumed equilateral):
Area = ½ × base × height, Perimeter = 3 × base
 Circle:
Area = πr², Circumference = 2πr

#include <stdio.h>

#include <math.h> // For pow() and sqrt()

main()

int choice;

float side, length, breadth, base, height, radius, s, area, perimeter;

do

printf("\n--- MENU ---\n");

printf("1. Square\n");

printf("2. Rectangle\n");

printf("3. Triangle\n");

printf("4. Circle\n");

printf("5. Exit\n");
printf("Enter your choice (1-5): ");

scanf("%d", &choice);

switch(choice)

case 1:

printf("Enter the side of the square: ");

scanf("%f", &side);

area = side * side;

perimeter = 4 * side;

printf("Area of Square = %.2f\n", area);

printf("Perimeter of Square = %.2f\n", perimeter);

break;

case 2:

printf("Enter length and breadth of the rectangle: ");

scanf("%f %f", &length, &breadth);

area = length * breadth;

perimeter = 2 * (length + breadth);

printf("Area of Rectangle = %.2f\n", area);

printf("Perimeter of Rectangle = %.2f\n", perimeter);

break;

case 3:

printf("Enter base and height of the triangle: ");

scanf("%f %f", &base, &height);


area = 0.5 * base * height;

// Assuming it's an equilateral triangle for perimeter

perimeter = 3 * base;

printf("Area of Triangle = %.2f\n", area);

printf("Perimeter of Triangle (equilateral) = %.2f\n", perimeter);

break;

case 4:

printf("Enter the radius of the circle: ");

scanf("%f", &radius);

area = M_PI * radius * radius;

perimeter = 2 * M_PI * radius;

printf("Area of Circle = %.2f\n", area);

printf("Circumference of Circle = %.2f\n", perimeter);

break;

case 5:

printf("Exiting program. Thank you!\n");

break;

default:

printf("Invalid choice! Please enter a valid option.\n");

} while(choice != 5);

}
d) Develop a menu driven program to compute statistical parameters
(using one dimensional array with functions): a) Mean b) Median c)
Variance d) Standard deviation e).Quit
#include <stdio.h>

#include <math.h>

main()

int n, i, j, choice;

float a[100], sum, mean, median, var, sd, temp;

printf("Enter number of elements: ");

scanf("%d", &n);

printf("Enter the elements:\n");

for(i = 0; i < n; i++)

scanf("%f", &a[i]);
do

printf("\n1. Mean\n2. Median\n3. Variance\n4. Standard Deviation\n5.


Quit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch(choice)

case 1: // Mean

sum = 0;

for(i = 0; i < n; i++)

sum += a[i];

mean = sum / n;

printf("Mean = %.2f\n", mean);

break;

case 2: // Median

// Sort array

for(i = 0; i < n - 1; i++) {

for(j = i + 1; j < n; j++) {

if(a[i] > a[j]) {

temp = a[i];

a[i] = a[j];

a[j] = temp;
}

if(n % 2 == 0)

median = (a[n/2 - 1] + a[n/2]) / 2;

else

median = a[n/2];

printf("Median = %.2f\n", median);

break;

case 3: // Variance

sum = 0;

mean = 0;

for(i = 0; i < n; i++)

mean += a[i];

mean /= n;

for(i = 0; i < n; i++)

sum += (a[i] - mean) * (a[i] - mean);

var = sum / n;

printf("Variance = %.2f\n", var);

break;

case 4: // Standard Deviation

sum = 0;

mean = 0;
for(i = 0; i < n; i++)

mean += a[i];

mean /= n;

for(i = 0; i < n; i++)

sum += (a[i] - mean) * (a[i] - mean);

sd = sqrt(sum / n);

printf("Standard Deviation = %.2f\n", sd);

break;

case 5:

printf("Program Ended.\n");

break;

default:

printf("Invalid choice!\n");

} while(choice != 5);

You might also like