1)
#include <stdio.h>
#include <math.h>
int main() {
double x, y;
printf("Enter a number (let's call it x): ");
scanf("%lf", &x);
printf("Enter a second number (y, for the power function): ");
scanf("%lf", &y);
printf("\n--- Math Functions for x = %.4f and y = %.4f ---\n", x, y);
printf("Power (pow): \t\t\t%.4f ^ %.4f = %.4f\n", x, y, pow(x, y));
if (x >= 0) {
printf("Square Root (sqrt): \t\tsqrt(%.4f) = %.4f\n", x, sqrt(x));
} else {
printf("Square Root (sqrt): \t\tUndefined for negative numbers.\n");
}
printf("Exponential (exp): \t\te ^ %.4f = %f\n", x, exp(x));
if (x > 0) {
printf("Natural Log (log): \t\tln(%.4f) = %.4f\n", x, log(x));
// log10() is the common logarithm (base 10)
printf("Common Log (log10): \t\tlog10(%.4f) = %.4f\n", x, log10(x));
} else {
printf("Logarithms (log, log10): \tUndefined for non-positive numbers.\n");
}
printf("\n--- Treating x = %.4f as an angle in radians ---\n", x);
printf("Sine (sin): \t\t\tsin(%.4f) = %.4f\n", x, sin(x));
printf("Cosine (cos): \t\t\tcos(%.4f) = %.4f\n", x, cos(x));
printf("Tangent (tan): \t\t\ttan(%.4f) = %.4f\n", x, tan(x));
return 0;
}
2)
#include <stdio.h>
#include <math.h>
int main() {
double a, b, c, discriminant, root1, root2, realPart, imagPart;
printf("Enter coefficients a, b, and c: ");
scanf("%lf %lf %lf", &a, &b, &c);
discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("Roots are real and distinct.\n");
printf("Root 1 = %.2lf and Root 2 = %.2lf\n", root1, root2);
}
else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
printf("Roots are real and equal.\n");
printf("Root 1 = Root 2 = %.2lf\n", root1);
}
else {
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
printf("Roots are complex and different.\n");
printf("Root 1 = %.2lf + %.2lfi and Root 2 = %.2lf - %.2lfi\n", realPart,
imagPart, realPart, imagPart);
}
return 0;
}
5)
#include <stdio.h>
int main() {
int var1, var2, temp;
printf("Enter the value for the first variable (var1): ");
scanf("%d", &var1);
printf("Enter the value for the second variable (var2): ");
scanf("%d", &var2);
printf("\n--- Before Swapping ---\n");
printf("var1 = %d\n", var1);
printf("var2 = %d\n", var2);
temp = var1;
var1 = var2;
var2 = temp;
printf("\n--- After Swapping ---\n");
printf("var1 = %d\n", var1);
printf("var2 = %d\n", var2);
return 0;
}
6)
#include <stdio.h>
void printBinary(int n) {
if (n > 1) {
printBinary(n / 2);
}
printf("%d", n % 2);
}
int main() {
int num, shift_amount;
printf("Enter an integer: ");
scanf("%d", &num);
printf("Enter the number of bits to shift: ");
scanf("%d", &shift_amount);
int left_shifted = num << shift_amount;
int right_shifted = num >> shift_amount;
printf("\n--- Original Number ---\n");
printf("Decimal: %d\n", num);
printf("Binary: ");
printBinary(num);
printf("\n");
printf("\n--- Left Shift (<<) ---\n");
printf("%d << %d = %d\n", num, shift_amount, left_shifted);
printf("Binary: ");
printBinary(left_shifted);
printf("\n");
printf("\n--- Right Shift (>>) ---\n");
printf("%d >> %d = %d\n", num, shift_amount, right_shifted);
printf("Binary: ");
printBinary(right_shifted);
printf("\n");
return 0;
}
7)
#include <stdio.h>
int main() {
int a, b, max;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
max = (a > b) ? a : b;
printf("The larger number is: %d\n", max);
return 0;
}
8)
#include <stdio.h>
int main() {
printf("Size of char: %zu byte\n", sizeof(char));
printf("Size of int: %zu bytes\n", sizeof(int));
printf("Size of long int: %zu bytes\n", sizeof(long int));
printf("Size of float: %zu bytes\n", sizeof(float));
printf("Size of double: %zu bytes\n", sizeof(double));
return 0;
}
9)
#include <stdio.h>
int main() {
int dividend = 25;
int divisor = 4;
double quotient;
quotient = (double)dividend / divisor;
printf("The result of %d / %d is: %lf\n", dividend, divisor, quotient);
return 0;
}