Practical 2
Practical 2
STATEMENTS
1. Write a C program that accepts a year and finds whether year entered
is a leap year or not.
Input:-
#include <stdio.h>
void main()
int year;
scanf("%d", &year);
if (year%4==0) {
} else {
Output:-
void main() {
int age;
scanf("%d", &age);
} else {
} else {
Output:-
Enter your age: 19
#include <stdio.h>
void main()
int number;
scanf("%d", &number);
if (number % 2 == 0) {
} else {
Output:-
Enter a number: 5
5 is an odd number.
4. Write a C program to check whether number entered is divisible by 3
or not.
Input:-
#include <stdio.h>
void main() {
int number;
scanf("%d", &number);
if (number % 3 == 0)
else {
Output:-
Enter a number: 57
57 is divisible by 3.
5. Write a C program to check whether number entered is divisible by 7
or not.
Input:-
#include <stdio.h>
void main()
int number;
scanf("%d", &number);
if (number % 7 == 0)
else
Output:-
Enter a number: 49
49 is divisible by 7.
6. Write a C program to check whether number entered is divisible by 3
or 5 or both.
Input:-
#include <stdio.h>
void main()
int number;
scanf("%d", &number);
else if (number % 3 == 0)
else if (number % 5 == 0)
else
Output:-
Enter a number: 42
void main() {
int number;
scanf("%d", &number);
if (number > 0)
else
Output:-
Enter a number: 5
5 is a positive number.
8. Write a C program to check greater among two numbers.
Input:-
#include <stdio.h>
void main()
int a, b;
scanf("%d", &a);
scanf("%d", &b);
if (a > b)
else if (a < b)
else
Output:-
Enter the first number: 5
6 is greater than 5.
9. Write a C program to check greater among three numbers.
Input:-
#include <stdio.h>
void main()
int a,b,c;
scanf("%d", &a);
scanf("%d", &b);
scanf("%d", &c);
else
Output:-
Enter the first number: 8
Enter the second number: 9
Enter the third number: 7
9 is the greatest among the three numbers.
10. Write a C program to read marks for a student from user and grade
the student according to the following
Marks Grades
Above 80 O
70-79 A+
60-69 A
55-59 B+
50-54 B
45-49 C
40-45 D
Below 40 Fail
Input:-
#include <stdio.h>
void main()
int marks;
scanf("%d", &marks);
printf("Grade: O\n");
printf("Grade: A+\n");
printf("Grade: A\n");
}
else if (marks >= 55 && marks <= 59)
printf("Grade: B+\n");
printf("Grade: B\n");
printf("Grade: C\n");
printf("Grade: D\n");
else
printf("Grade: Fail\n");
Output:-
Enter the marks: 65
Grade: A
11. Write a C program to compute the roots of a quadratic equation
ax2+bx+c=0 .
The roots are given by equation. (-b√b2-4ac)/2a
Input:-
#include<stdio.h>
void main()
float a,b,c,d,r1,r2;
scanf("%f%f%f",&a,&b,&c);
d=b*b-4*a*c;
if(d==0)
r1=-b/(2*a);
r2=-b/(2*a);
else if(d>0)
r1=(-b+sqrt(d))/(2*a);
r2=(-b-sqrt(d))/(2*a);
else
}
Output:-
void main() {
float bill;
scanf("%d", &customerNumber);
scanf("%d", &units);
bill = 100;
} else {
Customer Number: 20
void main() {
scanf("%d", &math);
scanf("%d", &physics);
scanf("%d", &chemistry);
if ((math >= 60 && physics >= 60 && chemistry >= 60 && totalAllSubjects >= 200) ||
} else {
}
Output:-
Enter marks in Mathematics: 76
14. Write a C program to enter month number between 1 and 12 and print
the correct month name otherwise print the appropriate error message.
(switch – case)
Input:-
#include <stdio.h>
int main() {
char choice;
float num1, num2, result;
printf("Enter operation choice (A/B/C/D/E): ");
scanf(" %c", &choice);
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
switch(choice) {
case 'A':
result = num1 + num2;
printf("Result of addition: %.2f\n", result);
break;
case 'B':
result = num1 - num2;
printf("Result of subtraction: %.2f\n", result);
break;
case 'C':
result = num1 * num2;
printf("Result of multiplication: %.2f\n", result);
break;
case 'D':
if (num2 != 0) {
result = num1 / num2;
printf("Result of division: %.2f\n", result);
} else {
printf("Error: Division by zero is not allowed.\n");
}
break;
case 'E':
if (num2 != 0) {
result = (int)num1 % (int)num2;
printf("Result of modulus: %d\n", (int)result);
} else {
printf("Error: Modulus by zero is not allowed.\n");
}
break;
default:
printf("Error: Invalid choice. Please enter A, B, C, D, or E.\n");
break;
}
}
Output:-
Enter operation choice (A/B/C/D/E): A
void main() {
int day;
scanf("%d", &day);
switch(day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
case 5:
printf("Friday\n");
break;
case 6:
printf("Saturday\n");
break;
case 7:
printf("Sunday\n");
break;
default:
printf("Error: Invalid day number. Please enter a number between 1 and 7.\n");
break;
Output:-
Enter weekday number (1-7): 5
Friday
int main() {
char choice;
switch(choice) {
case 'A':
break;
case 'B':
break;
case 'C':
break;
case 'D':
if (num2 != 0) {
} else {
break;
case 'E':
if (num2 != 0) {
} else {
break;
default:
break;
}
Output:-
Enter operation choice (A/B/C/D/E): C
17. Write a C program for the menu driven which perform any one
operation specified below. (switch – case)
1. Check whether no. is odd or even.
2. Check whether no. is positive, negative or 0.
3. Check whether no. is divisible by 5 or not.
Otherwise display the appropriate error message.
Input:-
#include <stdio.h>
void main() {
printf("Menu:\n");
scanf("%d", &choice);
scanf("%d", &num);
switch(choice) {
case 1:
if (num % 2 == 0) {
} else {
break;
case 2:
if (num > 0) {
} else {
break;
case 3:
if (num % 5 == 0) {
} else {
break;
default:
break;
Output:-
Menu:
void main() {
int choice;
printf("Menu:\n");
scanf("%d", &choice);
scanf("%f", &length);
scanf("%f", &width);
switch(choice) {
case 1:
break;
case 2:
break;
default:
break;
}
Output:-
Menu:
Input:-
#include <stdio.h>
void main() {
int choice;
printf("Menu:\n");
Enter a number: 29
20. Write a C program to read two numbers from the user and read the
choice between 1 & 3 and do the following: (switch – case)
1. Greater among of two numbers
2. Smallest among of two numbers
Input:-
#include <stdio.h>
void main() {
int choice;
printf("Menu:\n");
scanf("%d", &choice);
scanf("%f", &num1);
scanf("%f", &num2);
switch(choice) {
case 1:
} else {
break;
case 2:
} else {
break;
default:
break;
}
Output:-
Menu:
1. Find the greater of the two numbers
2. Find the smallest of the two numbers
Enter your choice (1/2): 1
Enter the first number: 9
Enter the second number: 5
The greater number is: 9.00