Practical 2
Practical 2
COMPUTER PROGRAMMING
BY : JAI DAMWANI
ROLL NO : 20124042
BRANCH : A4 IT
Q1 Write a C program to check whether a number is ODD or EVEN, using if else and switch.
#include <stdio.h>
int main()
{
int n;
printf("Enter a number\n");
scanf("%d",&n);
if(n%2==0)
printf("Number is Even\n");
else
printf("Number is Odd\n");
int k=n%2;
switch(k)
{
case 0: printf("Number is Even\n");break;
case 1: printf("Number is Odd\n");break;
}
return 0;
}
OUTPUT:
Q2 WAP to check whether a character is VOWEL or CONSONANT using switch statement.
#include <stdio.h>
int main()
{
char a;
printf("Enter a character\n");
scanf("%c",&a);
switch(a)
{
case 'i':
case 'o':
case 'e':
case 'u':
case 'a':printf("It is a vowel\n"); break;
default: printf("It is a consonant\n");
}
return 0;
}
OUTPUT:
Q3 a) WAP to find the largest among three numbers using (i) nested if statements, (ii) else if
#include <stdio.h>
int main()
{
int a,b,c;
printf("Enter Three Numbers\n");
scanf("%d%d%d",&a,&b,&c);
//Using Nested If
if(a>b)
{
if(a>c)
printf("%d is largest\n",a);
else
printf("%d is largest\n",c);
}
else
{if(b>c)
printf("%d is largest\n",b);
else
printf("%d is largest\n",c);
}
//using Else If
if(a>=b&&a>=c)
printf("%d is largest\n",a);
else if(b>a&&b>c)
printf("%d is largest\n",b);
else
printf("%d is largest\n",c);
#include <stdio.h>
int main()
{
char a;
printf("Enter a character\n");
scanf("%c",&a);
int b=(int)a;
if(b>=48&&b<=57)
{
if(b%2==0&&b%5==0)
printf("It is divisible by 2 and 5\n");
else
printf("It is not divisible by 2 and 5\n");
}
else
{
if(b>=65&&b<=90)
printf("It is in Uppercase\n");
else
{
printf("It is in Lowercase\n");
if(a=='a'||a=='e'||a=='i'||a=='o'||a=='u')
printf("It is a Vowel\n");
else
printf("It is a Consonant\n");
}
}
return 0;
}
OUTPUT:
Q4 Write a program to print the month name corresponding to the digit enter from the user using
switch statement. For example if user enter 1 then print “January” on 2 print “February”… and so on.
#include <stdio.h>
int main()
{
int a;
printf("Enter a number between 1 and 12\n");
scanf("%d",&a);
switch(a)
{
case 1:printf("January\n");break;
case 2:printf("February\n");break;
case 3:printf("March\n");break;
case 4:printf("April\n");break;
case 5:printf("May\n");break;
case 6:printf("June\n");break;
case 7:printf("July\n");break;
case 8:printf("August\n");break;
case 9:printf("September\n");break;
case 10:printf("October\n");break;
case 11:printf("November\n");break;
case 12:printf("December\n");break;
default: printf("Enter valid number between 1 and 12\n");
}
return 0;
}
OUTPUT:
Q5 WAP a read a three digits number from the keyboard and check whether it is a palindrome or not
#include <stdio.h>
#include <math.h>
int main()
{
int a;
printf("Enter a three digit number\n");
scanf("%d",&a);
int n=a;int no=0,sum=0;
int k=n;
while(n)
{
n=n/10;
no++;
}
no--;
while(k)
{
sum=sum+(k%10)*pow(10,no);
no--;
k=k/10;
}
if(sum==a)
printf("It is a Palindrome Number\n");
else
printf("It is not a Palindrome Number\n");
return 0;
}
OUTPUT:
Q6: WAP to find the eligibility of admission of a student for a professional institute based on the
following criteria: Marks in Mathematics >=65, Marks in Physics >=55, Marks in Chemistry >=60,
Total in all three subjects >=190 or Total in Mathematics and Chemistry >=130 (Hint: Input: marks
obtained in Physics: 55, marks obtained in Chemistry: 61 marks obtained in Mathematics: 72,
Output: The candidate is eligible for admission.)
#include <stdio.h>
int main()
{ int ph,ch,ma;
printf("Enter marks of Physics\n");
scanf("%d",&ph);
printf("Enter marks of Chemistry\n");
scanf("%d",&ch);
printf("Enter marks of Maths\n");
scanf("%d",&ma);
if(ph>=55&&ch>=60&&ma>=65&&(ph+ch+ma>=190||ma+ch>=130))
printf("The candidate is ELIGIBLE for admission\n");
else
printf("The candidate is NOT ELIGIBLE for admission\n");
return 0;
}
OUTPUT:
Q7: Write a C program to input marks of five subjects and calculate the percentage. Also Calculate
Grade according to the following:
Percentage>= 90% A
Percentage>=80% B
Percentage>=70% C
Percentage>=60% D
Percentage>=40% E
Percentage<40% Fail
#include <stdio.h>
int main()
{ int a,b,c,d,e;
printf("Enter marks of five subjects out of hundred\n");
scanf("%d %d %d %d %d",&a,&b,&c,&d,&e);
float per=(a+b+c+d+e)/5.0;
if(per>=90)
printf("Grade : A\n");
else if(per>=80)
printf("Grade : B\n");
else if(per>=70)
printf("Grade : C\n");
else if(per>=60)
printf("Grade : D\n");
else if(per>=40)
printf("Grade : E\n");
else
printf("Grade : FAIL\n");
return 0;
}