Find the GCD and LCM of Two
Integers
#include
#include
void main()
{
int num1, num2, gcd, lcm, remainder, numerator, denominator;
clrscr();
printf("Enter two numbers\n");
scanf("%d %d", &num1, &num2);
if (num1 > num2)
{
numerator = num1;
denominator = num2;
}
else
{
numerator = num2;
denominator = num1;
}
remainder = num1 % num2;
while (remainder != 0)
{
numerator = denominator;
denominator = remainder;
remainder = numerator % denominator;
}
gcd = denominator;
lcm = num1 * num2 / gcd;
printf("GCD of %d and %d = %d\n", num1, num2, gcd);
printf("LCM of %d and %d = %d\n", num1, num2, lcm);
getch();
}
Output
Enter two numbers
30
40
GCD of 30 and 40 = 30
LCM of 30 and 40 = 40
Calculate the Value of sin(x)
#include
#include
#include
#include
void main()
{
int n, x1;
float accuracy, term, denominator, x, sinx, sinval;
clrscr();
printf("Enter the value of x (in degrees) \n");
scanf("%f", &x);
x1 = x;
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));
getch();
}
Output
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
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
Calculate the Value of cos(x)
#include
#include
#include
#include
void main()
{
int n, x1;
float accuracy, term, denominator, x, cosx, cosval;
clrscr();
printf("Enter the value of x (in degrees) \n");
scanf("%f", &x);
x1 = x;
x = x * (3.142 / 180.0);
cosval = cos(x);
printf("Enter the accuracy for the result \n");
scanf("%f", &accuracy);
term = 1;
cosx = term;
n = 1;
do
{
denominator = 2 * n * (2 * n - 1);
term = -term * x * x / denominator;
cosx = cosx + term;
n = n + 1;
} while (accuracy <= fabs(cosval - cosx));
printf("Sum of the cosine series = %f\n", cosx);
printf("Using Library function cos(%d) = %f\n", x1, cos(x));
getch();
}
Output
Enter the value of x (in degrees)
60
Enter the accuracy for the result
0.86602
Sum of the cosine series = 0.451546
Using Library function cos(60) = 0.499882
Enter the value of x (in degrees)
45
Enter the accuracy for the result
0.7071
Sum of the cosine series = 0.691495
Using Library function cos(45) = 0.707035
No comments:
Post a Comment