0% found this document useful (0 votes)
6 views

Program bài tập

The document contains multiple C and C++ programs that perform various tasks including arithmetic operations on integers, solving quadratic equations, calculating powers using loops, matrix multiplication, finding maximum values, and rearranging matrix elements based on divisibility by 5. Each program includes user input, calculations, and output display. The code snippets demonstrate fundamental programming concepts such as loops, conditionals, and array manipulation.

Uploaded by

ngocthanh2821
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Program bài tập

The document contains multiple C and C++ programs that perform various tasks including arithmetic operations on integers, solving quadratic equations, calculating powers using loops, matrix multiplication, finding maximum values, and rearranging matrix elements based on divisibility by 5. Each program includes user input, calculations, and output display. The code snippets demonstrate fundamental programming concepts such as loops, conditionals, and array manipulation.

Uploaded by

ngocthanh2821
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

1.

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;

// Get input from the user


printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &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);

// Check if division is possible


if (num2 != 0) {
int quotient = num1 / num2;
int remainder = num1 % num2;
printf("Quotient: %d\n", quotient);
printf("Remainder: %d\n", remainder);
} else {
printf("Quotient: Undefined (division by 0)\n");
printf("Remainder: Undefined (division by 0)\n");
}

return 0;
}

2. Write a C code to solve a quadratic polynomial ax2+bx+c=0 (input a,b,c)

#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);

// Handle the case where a == 0 (not a quadratic equation)


if (a == 0) {
if (b == 0) {
if (c == 0) {
printf("The equation has infinitely many solutions.\n");
} else {
printf("The equation has no solution.\n");
}
} else {
x1 = -c / b;
printf("The equation has a single solution: x = %.2lf\n", x1);
}
} else {
// Calculate the discriminant
delta = b * b - 4 * a * 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);

// Handle the case where the exponent y is negative


if (y < 0) {
printf("The exponent must be a non-negative integer.\n");
return 1; // Exit the program with an error code
}

// Calculate x^y using a while loop


int i = 0;
while (i < y) {
result *= x;
i++;
}

// Print the result


printf("%d raised to the power of %d is %lld\n", x, y, result);

return 0;
}

4. Write a program to:


- Enter two matrixes from keyboard.
- Multiple the two matrixes.
- Print out the resulted matrix.
#include <iostream>
using namespace std;

void inputMatrix(int matrix[][100], int rows, int cols, string matrixName) {


cout << "Enter matrix " << matrixName << " (" << rows << "x" << cols << "):" << endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << "Enter element [" << i << "][" << j << "]: ";
cin >> matrix[i][j];
}
}
}

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];
}
}
}
}

void printMatrix(int matrix[][100], int rows, int cols, string matrixName) {


cout << "Matrix " << matrixName << ":" << endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
}

int main() {
int matrix1[100][100], matrix2[100][100], result[100][100];
int rows1, cols1, rows2, cols2;

cout << "Enter the number of rows for matrix 1: ";


cin >> rows1;
cout << "Enter the number of columns for matrix 1: ";
cin >> cols1;

cout << "Enter the number of rows for matrix 2: ";


cin >> rows2;
cout << "Enter the number of columns for matrix 2: ";
cin >> 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;
}

inputMatrix(matrix1, rows1, cols1, "1");


inputMatrix(matrix2, rows2, cols2, "2");

multiplyMatrices(matrix1, matrix2, result, rows1, cols1, cols2);

printMatrix(result, rows1, cols2, "result");

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);

// Handle the case where the exponent is negative


if (y < 0) {
printf("Error: The exponent must be a non-negative integer.\n");
return 1; // Exit the program with an error code
}

// Calculate x^y using a while loop


int i = 0;
while (i < y) {
result *= x;
i++;
}
// Print the result
printf("%d^%d = %lld\n", x, y, result);

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;

// Input three numbers


std::cout << "Enter three numbers: ";
std::cin >> num1 >> num2 >> num3;

// Find the maximum value


double max_value = std::max({num1, num2, num3});
std::cout << "Maximum value: " << max_value << std::endl;

// Input two more numbers


std::cout << "Enter two more numbers: ";
std::cin >> num4 >> num5;

// Calculate the average of five numbers


double average = (num1 + num2 + num3 + num4 + num5) / 5.0;
std::cout << "Average value: " << average << std::endl;

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 compare(const void *a, const void *b) {


return (*(int *)a - *(int *)b);
}

int main() {
int n;

printf("Enter matrix size (n): ");


scanf("%d", &n);

int matrix[n][n];
int elements[n * n];
int count = 0;

printf("Enter matrix elements:\n");


for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &matrix[i][j]);
elements[count++] = matrix[i][j];
}
}

printf("Elements divisible by 5:\n");


for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] % 5 == 0) {
printf("%d ", matrix[i][j]);
}
}
}
printf("\n");

qsort(elements, count, sizeof(int), compare);

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;
}

You might also like