2024AMB049 Assignment 3
2024AMB049 Assignment 3
1. Write a C program that can take three integer numbers as command line
parameters and prints the largest among them.
//Finding the largest among exactly three numbers using command line arguments.
#include <stdio.h>
#include <stdlib.h>
if (argc != 4) {
printf("Please enter exactly three numbers.\n");
return 1;
}
int x = atoi(argv[1]);
int y = atoi(argv[2]);
int z = atoi(argv[3]);
int largest = x;
if (y > largest) {
largest = y;
}
if (z > largest) {
largest = z;
}
return 0;
}
2. Write a C program that reads integer values as command line parameters
and determines if they are the sides of a right-angled triangle.
#include <stdio.h>
#include <stdlib.h>
if (argc != 4) {
printf("Please enter exactly three numbers.\n");
return 1;
}
int a = atoi(argv[1]);
int b = atoi(argv[2]);
int c = atoi(argv[3]);
if (a * a == b * b + c * c || b * b == c * c + a * a || c * c == a * a + b * b) {
printf("%d, %d, and %d are the sides of a right-angled triangle.\n", a, b, c);
}
else {
printf("%d, %d, and %d are not the sides of a right-angled triangle.\n", a, b, c);
}
return 0;
}
#include <stdio.h>
int main() {
int a;
printf("Enter a number:\t");
scanf("%d", &a);
if ((a & 1) == 0) {
printf("%d is an even number.\n", a);
}
else {
printf("%d is an odd number.\n", a);
}
return 0;
}
4. Write a C program that reads two integer values from the user. Use
conditional operator to check if the smaller integer is a factor of the larger
integer.
// Finding the smaller one among the given two integers and checking if the smaller one is a
factor of the larger one or not.
#include <stdio.h>
int main() {
int a, b;
printf("Enter two numbers:\t");
scanf("%d %d", &a, &b);
return 1;
}
if (a == 0 && b == 0) {
printf("Both integers are zero. Zero can't be a factor of zero.\n");
return 1;
}
if (a > b) {
printf("%d %s a factor of %d.\n", b, (a % b == 0) ? "is" : "is not", a);
}
else {
printf("%d %s a factor of %d.\n", a, (b % a == 0) ? "is" : "is not", b);
}
return 0;
}
//Multiplying and dividing a integer by 4 and 2 respectively using bitwise shift operators.
#include <stdio.h>
int main() {
int a;
printf("Enter a number:\t");
scanf("%d", &a);
return 0;
}