HW10
HW10
#include <stdio.h>
#include <math.h>
#define PI 3.14159265
int main ()
{
double x, ret, val;
x = 60.0;
val = PI / 180.0;
ret = cos( x * val );
printf("The cosine of %lf is ", x);
printf("%lf degrees\n", ret);
x = 90.0;
val = PI / 180.0;
ret = cos( x*val );
printf("The cosine of %lf is ", x);
printf("%lf degrees\n", ret);
return(0);
}
2. Write a program with the function to compute Maclaurin series of sine with x real, n
integer being input. Do every check appropriately.
3 5 n
x x x
sin ( x )=x− + −…+
3! 5! n!
#include<stdio.h>
#include<math.h>
// Function computing factorial
int fact(int value)
{
// Initailize factorial result as 1
int result = 1;
// Looping till the value
for(int p=1;p<=value;p++)
{
result *= p;
}
// return the factorial result
return result;
}
// Main Function
void main()
{
// Declaring the variables
int n;
float x,temp,ans=0;
// Taking user input of real value of x
printf("Enter the value of x: ");
scanf("%f",&x);
// Taking user input of n
printf("Enter the value of n: ");
scanf("%d",&n);
// Storing the input taken into temp
temp = x;
// Converting the input value into radians
x = x*(3.14159265/180);
// Looping till the value of n
for(int a=1,b=1;a<=n;a+=1,b+=2)
{
// In case the term is even
if(a%2==0)
{
ans -= (pow(x,b)/fact(b));
}
// In case the term is odd
else
{
ans += (pow(x,b)/fact(b));
}
}
// Printing the result
printf("sin(%0.2f): %f",temp,ans);
}
3. Write a program with the function to compute Maclaurin series of e x with x real, n
integer being input. Do every check appropriately.
n i 2 3 n
x x x x
e x =∑ =1+ x + + + …+
i=0 i! 2 ! 3! n!
4. Write a program with the function to compute Maclaurin series of e x with x real, ε
real (epsilon: absolute error, it must be 0 <ε < 1 ) being input. Do every check
appropriately.
n
xi x2 x3
e x =∑ =1+ x + + + …
i=0 i! 2 ! 3!
5. Write a program with the function to solve the quadratic equation, ax2 + bx + c = 0,
with its three real coefficients being input. The function must give all cases including
complex roots. Do every check appropriately.
#include <stdio.h>
#include <math.h>
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2*a);
root2 = (-b - sqrt(discriminant)) / (2*a);
printf("The roots are %lf and %lf\n", root1, root2);
} else if (discriminant == 0) {
root1 = -b / (2*a);
printf("The root is %lf\n", root1);
} else {
double real_part = -b / (2*a);
double imaginary_part = sqrt(-discriminant) / (2*a);
printf("The roots are %lf + %lfi and %lf - %lfi\n", real_part, imaginary_part,
real_part, imaginary_part);
}
}
int main() {
double a, b, c;
printf("Enter the coefficients of the quadratic equation: ");
scanf("%lf %lf %lf", &a, &b, &c);
if (a == 0) {
printf("Error: a cannot be zero\n");
return 1;
}
solve_quadratic_equation(a, b, c);
return 0;
}
6. Input 3 values, check if they satisfy the condition to form a triangle. Use the function
for checking.
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter the values of a, b, and c: ");
scanf("%d%d%d", &a, &b, &c);
if (isTriangle(a, b, c)) {
triangleType(a, b, c);
} else {
printf("These values do not form a triangle\n");
}
return 0;
}
7. Input a decimal integer value. Print out its binary notation. Attention to 2’s
compliment notation for the binary string.
#include <stdio.h>
int main() {
int decimal;
printf("Enter a decimal integer value: ");
scanf("%d", &decimal);
printf("Binary notation: ");
decimalToBinary(decimal);
printf("\n");
return 0;
}
8. Input a decimal arbitrary value (negative, positive, real, …). Print out its binary
notation. Attention to 2’s compliment notation for the binary string. Please use IEEE 754
standard to do this topic.
#include <stdio.h>
#include <stdint.h>
// Extract the sign bit, exponent, and mantissa from the union
uint64_t signBit = value.u >> 63;
uint64_t exponent = (value.u >> 52) & 0x7FF;
uint64_t mantissa = value.u & 0xFFFFFFFFFFFFF;
int main() {
double decimal;
printf("Enter a decimal arbitrary value: ");
scanf("%lf", &decimal);
printf("Binary notation in IEEE 754 format: ");
decimalToBinary(decimal);
printf("\n");
return 0;
}
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
if (n < 0) {
printf("Error: Input must be a positive integer.\n");
return 1;
}
int result = factorial(n);
printf("%d! = %d\n", n, result);
return 0;
}
2. S = 1 – 2 + 3 – 4 + …. – n
#include <stdio.h>
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
if (n < 0) {
printf("Error: Input must be a positive integer.\n");
return 1;
}
int result = seriesSum(n);
printf("Sum of the series 1 - 2 + 3 - 4 + ... - n = %d\n", n, result);
return 0;
}
3. S = x – x2 + x3 - … + xn
#include <stdio.h>
#include <math.h>
// Function to compute the sum of the series S = x - x^2 + x^3 - ... + x^n
double seriesSum(double x, int n) {
double sum = 0;
for (int i = 1; i <= n; i++) {
double term = pow(x, i); // Compute the i-th term of the series
if (i % 2 == 1) {
sum += term; // Add the term to the sum if i is odd
} else {
sum -= term; // Subtract the term from the sum if i is even
}
}
return sum;
}
int main() {
double x;
int n;
printf("Enter a real number x: ");
scanf("%lf", &x);
printf("Enter a positive integer n: ");
scanf("%d", &n);
if (n < 0) {
printf("Error: n must be a positive integer.\n");
return 1;
}
double result = seriesSum(x, n);
printf("Sum of the series S = %lf - %lf^2 + %lf^3 - ... + %lf^%d = %lf\n", x, x, x, x, n,
result);
return 0;
}
4. S = 1! – 2! + 3! - … + n!
#include <stdio.h>
int main() {
int n;
printf("Enter a positive integer n: ");
scanf("%d", &n);
if (n < 0) {
printf("Error: n must be a positive integer.\n");
return 1;
}
int result = seriesSum(n);
printf("Sum of the series S = 1! - 2! + 3! - ... + %d! = %d\n", n, result);
return 0;
}
Part 3.
14.6. What is the output of the following program?
#include <stdio.h>
int Multiply(int d, int b);
int d = 3;
int main()
{
int a, b, c;
int e = 4;
a=1;
b = 2;
c = Multiply(a, b);
printf("%d %d %d %d %d\n", a, b, c, d, e);
int Multiply(int d7 int b)
{
int a;
a=2;
b = 3;
return (a * b);
}
Answer: 1 2 6 3 4
14.8. What is the output of the following code? Explain why the function
Swap behaves the way it does.
int main()
{
int x = 1 ;
int y = 2;
Swap(x, y);
printf("x = %d y - %d\n", x, y);
}
void Swap(int y, int x)
{
int temp
temp = x;
x = y;
y = temp;
}
Answer: x = 1 y = 2
The intended swapping of the x and y variables by the Swap function does not occur.
This is due to the fact that the function modifies the copies of the original variables x
and y rather than the originals since it accepts the inputs y and x by value. The original
variables outside of the function are unaffected by the changes performed to the copies.
To put it another way, when the Swap function is used with the values x=1 and y=2, two
new variables, y and x, are created with the values x=1 and y=2, respectively. The copies'
values are then switched, but the original variables x and y from the main function are
unaffected. the printf statement is used, it is therefore It prints the original x and y
values when run in the main function.
14.13. Write a function to print out an integer value in base 4 (using only the
digits 0, 1, 2, 3). Use this function to write a program that reads two
integers from the keyboard and displays both numbers and their sum in
base 4 on the screen.
#include <stdio.h>
int main() {
int num1, num2, sum;
return 0;
}