Program bài tập
Program bài tập
Write a program that reads two integers from the user then displays their sum,
product, difference, quotient and remainder.
#include <stdio.h>
int main() {
int num1, num2;
// Perform calculations
int sum = num1 + num2;
int product = num1 * num2;
int difference = num1 - num2;
// Display results
printf("Sum: %d\n", sum);
printf("Product: %d\n", product);
printf("Difference: %d\n", difference);
return 0;
}
#include <stdio.h>
#include <math.h>
int main() {
double a, b, c;
double delta, x1, x2;
// Input coefficients
printf("Enter coefficient a: ");
scanf("%lf", &a);
printf("Enter coefficient b: ");
scanf("%lf", &b);
printf("Enter coefficient c: ");
scanf("%lf", &c);
if (delta > 0) {
x1 = (-b + sqrt(delta)) / (2 * a);
x2 = (-b - sqrt(delta)) / (2 * a);
printf("The equation has two distinct real roots:\n");
printf("x1 = %.2lf\n", x1);
printf("x2 = %.2lf\n", x2);
} else if (delta == 0) {
x1 = -b / (2 * a);
printf("The equation has one repeated root: x = %.2lf\n", x1);
} else {
double realPart = -b / (2 * a);
double imaginaryPart = sqrt(-delta) / (2 * a);
printf("The equation has two complex roots:\n");
printf("x1 = %.2lf + %.2lfi\n", realPart, imaginaryPart);
printf("x2 = %.2lf - %.2lfi\n", realPart, imaginaryPart);
}
}
return 0;
}
3. Write a C program that uses the statements in the preceding exercise to calculate x
raised to the y power. The program should have a while iteration control statement.
#include <stdio.h>
int main() {
int x, y;
long long result = 1;
// Ask the user to enter the base (x) and the exponent (y)
printf("Enter the base (x): ");
scanf("%d", &x);
printf("Enter the exponent (y): ");
scanf("%d", &y);
return 0;
}
void multiplyMatrices(int matrix1[][100], int matrix2[][100], int result[][100], int rows1, int
cols1, int cols2) {
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
result[i][j] = 0;
for (int k = 0; k < cols1; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
}
int main() {
int matrix1[100][100], matrix2[100][100], result[100][100];
int rows1, cols1, rows2, cols2;
if (cols1 != rows2) {
cout << "Cannot multiply the two matrices. The number of columns of matrix 1 must be
equal to the number of rows of matrix 2." << endl;
return 0;
}
return 0;
}
5. Write a C program that uses the statements in the preceding exercise to calculate x
raised to the y power. The program should have a while ỉnteration control startement.
#include <stdio.h>
int main() {
int x, y;
long long result = 1;
// Ask the user to enter the base (x) and exponent (y)
printf("Enter base (x): ");
scanf("%d", &x);
printf("Enter exponent (y): ");
scanf("%d", &y);
return 0;
}
6. write the program to input 3 double numbers. Find and Sprint the maximum valve of
the above numbers. Calculate the averwage value of 5 numbers and print the results.
#include <iostream>
#include <algorithm> // For std::max
int main() {
double num1, num2, num3, num4, num5;
return 0;
}
7. Write s program to input matrix with n rows and n columns. Print matrix elemets
divisible by 5 and rearrange the martrix in the following order
a. Elements are arrange by 5 are returned to the beginning trhe matrix.
b. The elements are arrange in order form smallest to largest, both divsible by 5 and not
divisible 5.
Print using matrix.
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
int matrix[n][n];
int elements[n * n];
int count = 0;
printf("Rearranged Matrix:\n");
int index = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
matrix[i][j] = elements[index++];
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
return 0;
}