C PROGRAM ON MATH FUNCTION
C Program to Calculate the Value of sin(x)
*
* C program to find the value of sin(x) using the series
* up to the given accuracy (without using user defined function)
* also print sin(x) using library function.
*/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
void main()
{
int n, x1;
float accuracy, term, denominator, x, sinx, sinval;
printf("Enter the value of x (in degrees) \n");
scanf("%f", &x);
x1 = x;
/* Converting degrees to radians */
x = x * (3.142 / 180.0);
sinval = sin(x);
printf("Enter the accuracy for the result \n");
scanf("%f", &accuracy);
term = x;
sinx = term;
n = 1;
do
{
denominator = 2 * n * (2 * n + 1);
term = -term * x * x / denominator;
sinx = sinx + term;
n = n + 1;
} while (accuracy <= fabs(sinval - sinx));
printf("Sum of the sine series = %f \n", sinx);
printf("Using Library function sin(%d) = %f\n", x1, sin(x));
}
Runtime Test Cases
$ cc pgm14.c -lm
$ a.out
Enter the value of x (in degrees)
60
Enter the accuracy for the result
0.86602540378443864676372317075294
Sum of the sine series = 0.855862
Using Library function sin(60) = 0.866093
$ a.out
Enter the value of x (in degrees)
45
Enter the accuracy for the result
0.70710678118654752440084436210485
Sum of the sine series = 0.704723
Using Library function sin(45) = 0.707179
C Program to Find Quotient and Remainder
/*
* C Program to find the quotient and remainder
*/
#include <stdio.h>
int main()
{
int a,b;
printf("Enter Dividend: ");
scanf("%d",&a);
printf("Enter Divisor: ");
scanf("%d",&b);
// Calculates the quotient
int quotient = a/b;
// Calculates the remainder
int remainder = a%b;
printf("Quotient is : %d\nRemainder is: %d",quotient,remainder);
return 0;
}
Runtime Test Cases
Here is the runtime output of the C program to find the quotient and remainder when the user entered Dividend = “10” and Divisor = “3”.
Enter Dividend: 10
Enter Divisor: 3
Quotient is : 3
Remainder is: 1
C Program to Find Square Root of a Number
/*
* C program to find the square root of a number using sqrt function
*/
#include <stdio.h>
int main()
{
double n;
printf("Enter number: ");
scanf("%lf",&n);
//Calling sqrt function
double square_root=sqrt(n);
//Precision upto 2 decimal places
printf("Number is %0.2lf and its square root is: %0.2lf",n,sqrt(n));
return 0;
}
Run Time Testcases
Testcase 1: In this case, we enter the value “25” as input to find the square root.
Enter number: 25
Number is 25 and its square root is: 5
Testcase 2: In this case, we enter the value “64” as input to find the square root.
Enter number: 64
Number is 64 and its square root is: 8
C Program to Find Simple Interest
*
* C program to find the simple interest
*/
#include<stdio.h>
int main()
{
float principal, rate, time;
// Get user input
printf("Enter the principal amount: ");
scanf("%f",&principal);
printf("Enter the rate of interest: ");
scanf("%f",&rate);
printf("Enter the time: ");
scanf("%f",&time);
float simple_interest, amount;
// Calculate simple interest and amount
simple_interest = ((principal*rate*time)/100);
amount = simple_interest + principal;
// Print the output
printf("Simple Interest = %f \nAmount = %f",simple_interest, amount);
return 0;
}
Runtime Test Cases
Testcase 1: In this case, enter P=7000, R=50, and T=2 as the parameters to calculate simple interest.
Enter the principal amount: 7000
Enter the rate of interest: 50
Enter the time: 2
Simple Interest = 7000.000000
Amount = 14000.000000
Testcase 2: In this case, enter P=4000, R=20, and T=3 as the parameters to calculate simple interest.
Enter the principal amount: 4000
Enter the rate of interest: 20
Enter the time: 3
Simple Interest = 2400.000000
Amount = 6400.000000
Calculator Program in C
*
* C program to simulate a simple calculator to perform arithmetic
* operations like addition, subtraction, multiplication and division
*/
#include <stdio.h>
void main()
{
char operator;
float num1, num2, result;
printf("Simulation of a Simple Calculator\n");
printf("*********************************\n");
printf("Enter two numbers \n");
scanf("%f %f", &num1, &num2);
printf("Enter the operator [+,-,*,/] \n");
scanf("%s", &operator);
if(operator == '+')
result = num1 + num2;
else if(operator == '-')
result = num1 - num2;
else if(operator == '*')
result = num1 * num2;
else if(operator == '/')
result = num1 / num2;
else
printf("Error in operation");
printf("\n%5.2f %c %5.2f = %5.2f\n", num1, operator, num2, result);
}
Runtime Test Cases
Test Case 1: Addition
Simulation of a Simple Calculator
*********************************
Enter two numbers
3 5
Enter the operator [+,-,*,/]
+
3.00 + 5.00 = 8.00
Test Case 2: Multiplication
Simulation of a Simple Calculator
*********************************
Enter two numbers
40 50
Enter the operator [+,-,*,/]
*
40.00 * 50.00 = 2000.00
Test Case 3: Division
Simulation of a Simple Calculator
*********************************
Enter two numbers
400 16
Enter the operator [+,-,*,/]
/
400.00 / 16.00 = 25.00
Test Case 4: Subtraction
Simulation of a Simple Calculator
*********************************
Enter two numbers
6500 4700
Enter the operator [+,-,*,/]
-
5500.00 - 4100.00 = 1400.00
C Program to Find nPr
*
* C program to Calculate the Value of nPr
*/
#include <stdio.h>
void main(void)
{
printf("%d\n", fact(8));
int n, r;
printf("Enter value for n and r\n");
scanf("%d%d", &n, &r);
int npr = fact(n) / fact(n - r);
printf("\n Permutation values is = %d", npr);
}
int fact(int x)
{
if (x <= 1)
return 1;
return x * fact(x - 1);
}
Runtime Test Cases
Output:
$ cc pgm13.c
$ a.out
40320
Enter value for n and r
5 4
Permutation values is = 120
C Program to Find Mean, Variance and Standard Deviation
/*
* C program to input real numbers and find the mean, variance
* and standard deviation
*/
#include <stdio.h>
#include <math.h>
#define MAXSIZE 10
void main()
{
float x[MAXSIZE];
int i, n;
float average, variance, std_deviation, sum = 0, sum1 = 0;
printf("Enter the value of N \n");
scanf("%d", &n);
printf("Enter %d real numbers \n", n);
for (i = 0; i < n; i++)
{
scanf("%f", &x[i]);
}
/* Compute the sum of all elements */
for (i = 0; i < n; i++)
{
sum = sum + x[i];
}
average = sum / (float)n;
/* Compute variance and standard deviation */
for (i = 0; i < n; i++)
{
sum1 = sum1 + pow((x[i] - average), 2);
}
variance = sum1 / (float)n;
std_deviation = sqrt(variance);
printf("Average of all elements = %.2f\n", average);
printf("variance of all elements = %.2f\n", variance);
printf("Standard deviation = %.2f\n", std_deviation);
}
Runtime Test Cases
$ cc pgm23.c -lm
$ a.out
Enter the value of N
5
Enter 5 real numbers
34
88
32
12
10
Average of all elements = 35.20
variance of all elements = 794.56
Standard deviation = 28.19
C Program to Calculate Pow (x,n)
/*
* C program to compute the value of X ^ N given X and N as inputs
*/
#include <stdio.h>
#include <math.h>
long int power(int x, int n);
void main()
{
long int x, n, xpown;
printf("Enter the values of X and N \n");
scanf("%ld %ld", &x, &n);
xpown = power(x, n);
printf("X to the power N = %ld\n", xpown);
}
/* Recursive function to computer the X to power N */
long int power(int x, int n)
{
if (n == 1)
return(x);
else if (n % 2 == 0)
/* if n is even */
return (pow(power(x, n/2), 2));
else
/* if n is odd */
return (x * power(x, n - 1));
}
Runtime Test Cases
$ cc pgm55.c -lm
$ a.out
Enter the values of X and N
2 5
X to the power N = 32