0% found this document useful (0 votes)
12 views12 pages

Rehearsal

Uploaded by

Minh Tâm
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)
12 views12 pages

Rehearsal

Uploaded by

Minh Tâm
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/ 12

EX1:

#include <stdio.h>
#include <math.h>

int main() {
double X, Y, F;

printf("Enter X: ");
scanf("%lf", &X);
printf("Enter Y: ");
scanf("%lf", &Y);

F = fabs(pow(X, 2) + pow(Y, 2) - 5 * X * Y) + Y * (5 - X);

printf("%.2lf\n", F);

return 0;
}

EX2:

#include <stdio.h>

int main() {
int n, A = 0, B = 0, C, D = 0;
int array[10];

printf("Enter the number of elements (0 < n < 10): ");


scanf("%d", &n);

printf("Enter %d elements: ", n);


for (int i = 0; i < n; i++) {
scanf("%d", &array[i]);
if (array[i] < 0) {
printf("ERROR\n");
return 1;
}
}

for (int i = 0; i < n; i++) {


if (array[i] % 4 == 1)
A += array[i];
else if (array[i] % 4 == 2)
B += array[i];
}

C = 2 * A + B;
for (int i = 0; i < n; i++) {
if (array[i] > C)
D++;
}
printf("%d %d %d %d\n", A, B, C, D);

return 0;
}

EX3:

#include <stdio.h>

int main() {
int a, b;

printf("Enter two integers: ");


scanf("%d %d", &a, &b);

if (a < 0 && b >= 0)


printf("%d\n", a);
else if (a >= 0 && b < 0)
printf("%d\n", b);
else if (a < 0 && b < 0)
printf("%d\n", a + b);
else if (a >= 0 && b >= 0)
printf("%d\n", 2 * a);

return 0;
}

EX4:

#include <stdio.h>

int main() {
int N, M = 0, S = 0;
int array[100];

printf("Enter the number of elements in array (1 <= N <=


100): ");
scanf("%d", &N);

printf("Enter %d positive integers: ", N);


for (int i = 0; i < N; i++) {
scanf("%d", &array[i]);
if (array[i] % 2 == 0 && array[i] % 3 == 0)
M++;
else if (array[i] % 2 != 0 && array[i] % 3 == 0)
S += array[i];
}

if (M > 5)
printf("%d\n", 2 * S);
else if (M >= 1 && M <= 5)
printf("%d\n", S);
else if (M == 0)
printf("%d\n", S);

return 0;
}

EX5

#include <stdio.h>

int main() {
int N, M = 0, S = 0;
int array[100];

printf("Enter the number of elements in array (1 <= N <=


100): ");
scanf("%d", &N);

printf("Enter %d positive integers: ", N);


for (int i = 0; i < N; i++) {
scanf("%d", &array[i]);
if (array[i] % 2 == 0 && array[i] % 3 == 0)
M++;
else if (array[i] % 2 != 0 && array[i] % 3 == 0)
S += array[i];
}

if (M > 5)
printf("%d\n", 2 * S);
else if (M >= 1 && M <= 5)
printf("%d\n", S);
else if (M == 0)
printf("%d\n", S);

return 0;
}

EX6:

#include <stdio.h>

int largest_power_of_two(int n) {
if (n < 1)
return 0;

int power = 1;
while (power <= n) {
power *= 2;
}
return power / 2;
}

int main() {
int m;

printf("Enter a positive integer m: ");


scanf("%d", &m);

if (m < 1) {
printf("Invalid\n");
return 1;
}

printf("%d\n", largest_power_of_two(m));

return 0;
}

EX7:
#include <stdio.h>

// Function to find the maximum of two integers


int find_maximum(int a, int b) {
return (a > b) ? a : b;
}

int main() {
int a, b;

// Input
printf("Enter two integers: ");
scanf("%d %d", &a, &b);

// Output the maximum of two integers using the function


printf("%d\n", find_maximum(a, b));

return 0;
}

EX8:

#include <stdio.h>

int find_maximum(int a, int b, int c) {


int max = a;
if (b > max)
max = b;
if (c > max)
max = c;
return max;
}

int main() {
int a, b, c;

printf("Enter three integers: ");


scanf("%d %d %d", &a, &b, &c);

printf("%d\n", find_maximum(a, b, c));

return 0;
}

EX9:

#include <stdio.h>
#include <math.h>

struct Point {
double x;
double y;
};

double distance(struct Point A, struct Point B) {


return sqrt(pow((B.x - A.x), 2) + pow((B.y - A.y), 2));
}

int main() {
struct Point A, B;

printf("Enter coordinates of point A (x y): ");


scanf("%lf %lf", &A.x, &A.y);
printf("Enter coordinates of point B (x y): ");
scanf("%lf %lf", &B.x, &B.y);

printf("Distance between points A and B: %.2lf\n", distance(A, B));

return 0;
}

EX10:
#include <stdio.h>

struct Point {
double x;
double y;
};

int main() {
struct Point lower_left, upper_right, point;
printf("Enter coordinates of lower left endpoint of rectangle (x y): ");
scanf("%lf %lf", &lower_left.x, &lower_left.y);
printf("Enter coordinates of upper right endpoint of rectangle (x y): ");
scanf("%lf %lf", &upper_right.x, &upper_right.y);

if (lower_left.x > upper_right.x || lower_left.y > upper_right.y) {


printf("Invalid\n");
return 1;
}

printf("Enter coordinates of another point (x y): ");


scanf("%lf %lf", &point.x, &point.y);

if (point.x >= lower_left.x && point.x <= upper_right.x &&


point.y >= lower_left.y && point.y <= upper_right.y) {
printf("1\n"); // Point falls within the rectangle
} else {
printf("0\n"); // Point does not fall within the rectangle
}

return 0;
}

You might also like