Practical No 3:
P1:Write a program to accept an integer and check if it is even or odd.
Code:
#include <stdio.h>
int main()
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if(num % 2 == 0)
printf("%d is even.", num);
else
printf("%d is odd.", num);
return 0;
P2: Write a program to accept three numbers and check whether the first is between the other
two numbers. Ex: Input 20 10 30. Output: 20 is between 10 and 3
Code:
#include<stdio.h>
int main()
{
int x,y,z;
printf("enter 3 numbers : ");
scanf("%d%d%d",&x,&y,&z);
if(x>=y && x<=z)
printf("%d is between %d and %d",x,y,z);
else
printf("%d is not between %d and %d",x,y,z);
P3: Write a program to accept a number and
check if it is divisible by 5 and 7
#include<stdio.h
int main()
int a;
printf("\n Enter Your number:"); scanf("%d",&a);
if (a%5==0 && a%7==0)
printf("\n %d is divisible by 5 and 7 \n \n",a);
else
printf("\n %d is not divisible by 5 and 7 \n \n",a);
}
return 0;
P4: Write a program, which accepts annual basic salary of an employee and calculates and
displays
the Income tax as per the following rules. ***
Basic: < 1,50,000 Tax = 0
1,50,000 to 3,00,000 Tax = 20%
> 3,00,000 Tax = 30
#include<stdio.h>
#include<math.h>
#include<conio.h>
void main()
float salary,tax;
printf("enter the emp basic salary :");
scanf("%f",&salary);
if(salary<=150000)
tax=0;
printf("tax = %f",tax);
else if(salary>150000 && salary<=300000)
tax=(salary*0.2);
printf("tax = %f",tax);
}
else if(salary>300000)
tax=(salary*0.3);
printf("tax = %f",tax);
P5: Accept a character from the user and check whether the character is a vowel or consonant.
(Hint: a,e,i,o,u, A, E, I, O, U are vowels)
#include <stdio.h>
int main()
char c;
int lowercase_vowel, uppercase_vowel;
printf("Enter an alphabet: ");
scanf("%c", &c);
lowercase_vowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
uppercase_vowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
if (lowercase_vowel || uppercase_vowel)
printf("%c is a vowel.", c);
else
printf("%c is a consonant.", c);
return 0;
}
P6: Write a program to check whether given character is a digit or a character in
lowercase or uppercase alphabet. (Hint ASCII value of digit is between 48 to 58
and Lowercase characters have ASCII values in the range of 97 to122, uppercase is
between 65 and 90)
#include <stdio.h>
int main()
char ch;
// Read a character from the user
printf("Enter a character: ");
scanf("%c", &ch);
if (ch >= '0' && ch <= '9')
printf("The character is a Digit.\n");
else if (ch >= 'A' && ch <= 'Z')
printf("The character is an Uppercase Letter.\n");
else if (ch >= 'a' && ch <= 'z')
printf("The character is a Lowercase Letter.\n");
else
{
printf("The character is not a Digit or a Letter.\n");
return 0;
P7: Accept a character as input and check whether the character is a digit.
(Check if it is in the range ‘0’ to ‘9’ both inclusive)
#include <stdio.h>
int main()
char ch;
// Read a character from the user
printf("Enter a character: ");
scanf("%c", &ch);
// Check if the character is a digit
if (ch >= '0' && ch <= '9')
printf("The character '%c' is a Digit.\n", ch);
else
printf("The character '%c' is not a Digit.\n", ch); }
return 0;
P8: Write a program to accept marks for three subjects. Calculate the average and also display
the class obtained. (Distinction – above Class I – above %, class II –
% to %, pass class – % to % and fail otherwise)
#include <stdio.h>
int main() {
float marks1, marks2, marks3;
float average;
// Input marks for three subjects
printf("Enter marks for subject 1: ");
scanf("%f", &marks1);
printf("Enter marks for subject 2: ");
scanf("%f", &marks2);
printf("Enter marks for subject 3: ");
scanf("%f", &marks3);
// Calculate average
average = (marks1 + marks2 + marks3) / 3;
// Display the average
printf("Average marks: %.2f\n", average);
// Determine and display the class based on average percentage
if (average > 70)
printf("Class: Distinction\n");
else if (average > 60)
printf("Class: First Class\n");
else if (average >= 50)
printf("Class: Second Class\n");
}
else if (average >= 40)
printf("Class: Pass Class\n");
else
printf("Class: Fail\n");
return 0;
P9: Accept any year as input through the keyboard. Write a program to check whether the year
is a leap year or not. (Hint leap year is divisible by 4 and not by 100 or divisible by 400)
#include <stdio.h>
int main() {
int year;
// Input year from user
printf("Enter a year: ");
scanf("%d", &year);
// Check if the year is a leap year
if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
printf("%d is a Leap Year.\n", year);
else
{
printf("%d is not a Leap Year.\n", year);
return 0;
Self Activity
1. Execute the following program for five different values and fill in the adjoining table
main()
int n;
printf(“Enter no.”);
scanf(“%d”,&n);
if(n% ==0)
printf(“divisible);
else
printf(“not divisible ”);
n output
2. Type the above sample program 4 and execute it for the following values.
n Output message
50
100
65
3. Using the sample code 3 above write the complete program to find the maximum of three
numbers and execute it for different set of values.