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

Lab assignment 2

The document contains a series of C programming exercises, each demonstrating different programming concepts such as input/output, conditional statements, and basic arithmetic operations. Each question includes code snippets that prompt the user for input and provide output based on the conditions specified. The exercises cover topics like divisors, even/odd numbers, largest/smallest numbers, leap years, triangle validity, worker efficiency, and book return fines.

Uploaded by

yahyakhan3771
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab assignment 2

The document contains a series of C programming exercises, each demonstrating different programming concepts such as input/output, conditional statements, and basic arithmetic operations. Each question includes code snippets that prompt the user for input and provide output based on the conditions specified. The exercises cover topics like divisors, even/odd numbers, largest/smallest numbers, leap years, triangle validity, worker efficiency, and book return fines.

Uploaded by

yahyakhan3771
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Husnain Ali Khan SP24_BCS_048 2B

Q1:

#include<stdio.h>
int main(){
int n1;
int n2;

printf("Enter first number: ");


scanf("%d",&n1);

printf("Enter second number: ");


scanf("%d",&n2);

if(n2%n1==0){
printf("%d is a divisor of %d",n1,n2);
}
else{
printf("%d is not a divisor of %d",n1,n2);
}

return 0;

Q2:

int main() {
int num1, num2;

printf("Enter first number: ");


scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);

if (num2 % num1 == 0) {
printf("%d is a factor of %d\n", num1, num2);
} else {
printf("%d is not a factor of %d\n", num1, num2);
}

return 0;
}
Q3:

#include <stdio.h>

int main() {
int num;

printf("Enter a number: ");


scanf("%d", &num);

if (num % 2 == 0) {
printf("%d is even.\n", num);
} else {
printf("%d is odd.\n", num);
}

return 0;
}

Q4:

#include <stdio.h>

int main() {
int num1, num2;

printf("Enter first number: ");


scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);

if (num1 > num2) {


printf("%d is the larger number.\n", num1);
} else if (num2 > num1) {
printf("%d is the larger number.\n", num2);
} else {
printf("Both numbers are equal.\n");
}

return 0;
}
Q5:

int main() {
int num1, num2;

printf("Enter first number: ");


scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);

if (num1 < num2) {


printf("%d is the smaller number.\n", num1);
} else if (num2 < num1) {
printf("%d is the smaller number.\n", num2);
} else {
printf("Both numbers are equal.\n");
}

return 0;
}
Q6:

#include <stdio.h>

int main() {
int num1, num2, num3;
int largest, smallest;

printf("Enter first number: ");


scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
printf("Enter third number: ");
scanf("%d", &num3);

if (num1 > num2 && num1 > num3) {


printf("%d is the largest\n",num1);
}
else if(num2 > num1 && num2 > num3){
printf("%d is the largest\n",num2);

}
else{
printf("%d is the largest\n",num3);
}

if (num1 < num2 && num1 , num3) {


printf("%d is the smallest\n",num1);
}
else if(num2 < num1 && num2 < num3){
printf("%d is the smallest\n",num2);

}
else{
printf("%d is the smallest\n",num3);
}

return 0;
}
Q7:

#include <stdio.h>

int main() {
int year;

printf("Enter a year: ");


scanf("%d", &year);

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {


printf("%d is a leap year.\n", year);
} else {
printf("%d is not a leap year.\n", year);
}

return 0;
}

Q8:

#include <stdio.h>

int main() {
int side1, side2, side3;

printf("Enter first side: ");


scanf("%d", &side1);
printf("Enter second side: ");
scanf("%d", &side2);
printf("Enter third side: ");
scanf("%d", &side3);

if ((side1 + side2 > side3) && (side1 + side3 > side2) && (side2 + side3 >
side1)) {
printf("The triangle is valid.\n");
} else {
printf("The triangle is not valid.\n");
}

return 0;
}
Q9:

#include <stdio.h>

int main() {
float time;

printf("Enter the time taken by the worker (in hours): ");


scanf("%f", &time);

if (time >= 2 && time < 3) {


printf("The worker is highly efficient.\n");
} else if (time >= 3 && time < 4) {
printf("The worker needs to improve speed.\n");
} else if (time >= 4 && time < 5) {
printf("The worker needs training to improve speed.\n");
} else if (time >= 5) {
printf("The worker has to leave the company.\n");
} else {
printf("Invalid time entered.\n");
}

return 0;
}
Q10:

#include <stdio.h>

int main() {
int daysLate;

printf("Enter the number of days the book is late: ");


scanf("%d", &daysLate);

if (daysLate <= 0) {
printf("The book is returned on time.\n");
} else if (daysLate <= 5) {

printf("The fine is: 50 paisa \n");


}
else if (daysLate <= 10) {

printf("The fine is: 1 rupees\n");


}

else if (daysLate <= 30) {

printf("The fine is: 5 rupees\n");


} else {
printf("Your membership has been cancelled due to returning the book
after 30 days.\n");
}

return 0;
}

You might also like