0% found this document useful (0 votes)
21 views4 pages

2024AMB049 Assignment 3

Uploaded by

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

2024AMB049 Assignment 3

Uploaded by

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

Name: Imon Haque

Department: Aerospace Engineering & Applied Mechanics

Enrollment Number: 2024AMB049

Group No: VIII

Lab Assignment: 3 Date: 07/09/2024

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>

int main (int argc, char*argv[]) {

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

printf("The largest number is %d.\n", largest);

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.

//Finding if the given sides are of a right-angled triangle or not.

#include <stdio.h>
#include <stdlib.h>

int main (int argc, char*argv[]) {

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

3. Write a C program to check whether an integer entered by the user is even


or odd using a bitwise operator.

//Finding if the number is odd or even.

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

if (((a > b) && (b == 0)) || ((b > a) && (a == 0))) {


printf("The smaller integer is zero. Division by zero is not possible.\n");

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

5. Write a C program that takes an integer as input. Use shift operation to


multiply it by 4 and divide it by 2.

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

int multiply_by_4 = a<<2;


int divide_by_2 = a>>1;

printf("%d multiplied by 4 is %d.\n", a, multiply_by_4);


printf("%d divided by 2 is %d.\n", a, divide_by_2);

return 0;
}

You might also like