Ex-2 Record
Ex-2 Record
20/9/24
Aim:
To implement the solutions of the problems using c language control statements.
1. Read the age of a candidate and determine whether he is eligible to vote or not.
Source code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int age;
printf("Enter the age:");
scanf("%d",&age);
if (age>=18)
{
printf("The candidate is eligible for voting");
}else{
printf("The candidate is not eligible for voting");
}
return 0;
}
24I360
Output:
2. Accept the height of a person in centimeter and categorize them based on below:
Source code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
float height;
printf("Enter the height of the person in centimeter:");
scanf("%f",&height);
if(height<150){
printf("Dwarf");
}
else if(height==150){
printf("Average height");
}
else if(height>150){
printf("Tall");
}
return 0;
}
24I360
Output:
3. Read year, month number from the user and print number of days in that month.
Source code:
include <stdio.h>
#include <stdlib.h>
int main()
{
int year,m;
printf("Enter the year:");
scanf("%d",&year);
printf("Enter the month:");
scanf("%d",&m);
if(m==1 || m==3 || m==5 || m==7 || m==8 || m==10 || m==12){
printf("this month has 31 days");
}
if(year%4==0 && year%100==0 &&m==2){
printf("this month has 29 days");
}else if(m==2){
24I360
printf("this month has 28 days");
}
if(m==4 || m==6 || m==9 || m==11){
printf("this month has 30 days");
}
return 0;
}
Output:
Source code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
24I360
float a,b,c;
printf("Enter the three sides of the triangle:\n");
scanf("%f %f %f",&a,&b,&c);
if(a==b && b==c && c==a){
printf("It is a equilateral triangle");
}else if(a==b || a==c || c==a){
printf("It is a isosceles triangle");
}else{
printf("it is a scalene triangle");
}
return 0;
}
Output:
24I360
5.Find all roots of a quadratic equation.
Source code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
float a,b,c,d,r1,r2,i,r;
printf("Enter the coefficient of the quadratic function:\n");
scanf("%f %f %f",&a,&b,&c);
d=(b*b)-(4*a*c);
i=sqrt(-d)/(2*a);
if(d>0){
r1=(-b+(sqrt(d)))/2*a;
r2=(-b-(sqrt(d)))/2*a;
printf("two distinct complex root exist: %.2f and %.2f",r1,r2);
}else if(d==0){
r=-(b/(2*a));
printf("one distinct complex root exist: %.2f",r);
}else{
r=-b/(2*a);
printf("two distinct complex root exist: %.2f + i%.2f and %.2f - i%.2f",r,i,r,i);
}
return 0;
}
24I360
Output:
Source code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
short int x,y;
printf("Enter the value of X and Y:\n");
scanf("%hd %hd",&x,&y);
if(x>0 && y>0){
printf("%hd and %hd lies in 1st quadrant",x,y);
}else if(x<0 && y>0){
24I360
printf("%hd and %hd lies in 2nd quadrant",x,y);
}else if(x<0 && y<0){
printf("%hd and %hd lies in 3rd quadrant",x,y);
}else{
printf("%hd and %hd lies in 4th quadrant",x,y);
}
return 0;
}
Output:
24I360
7. Determine eligibility for admission to a professional course based on the following
criteria:
Marks in Maths >=65
Marks in Phy >=55
Marks in Chem>=50
Total in all three subject >=190 or Total in Math and Physics >=140.
Source code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int m,p,c,t,mp;
printf("Enter the mark for maths:");
scanf("%d",&m);
printf("Enter the mark for physics:");
scanf("%d",&p);
printf("Enter the mark for chemistry:");
scanf("%d",&c);
t=m+p+c;
printf("Total marks is %d\n",t);
mp=m+p;
printf("Total marks in maths and physics is %d\n",mp);
if (m>=65 && p>=55 && c>=50 && t>=190 || mp>=140){
printf("The candidate is eligible");
} else{
printf("The candidate is not eligible");
}
return 0;
}
24I360
Output:
Source code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
float t;
24I360
printf("Enter the temperature in centigrade:");
scanf("%f",&t);
if(t<0){
printf("Freezing weather");
}else if (t>=0 && t<10){
printf("Very cold weather");
}else if (t>=10 && t<20){
printf("cold weather");
}else if (t>=20 && t<30){
printf("normal weather");
}else if (t>=30 && t<40){
printf("hot weather");
}else {
printf("Very hot");
}
return 0;
}
Output:
24I360
9. Input marks of five subjects Physics, Chemistry, Biology, Mathematics and
Computer. Calculate percentage and grade according to following:
Percentage >= 90% : Grade A
Percentage >= 80% : Grade B
Percentage >= 70% : Grade C
Percentage >= 60% : Grade D
Percentage >= 40% : Grade E
Percentage < 40% : Grade F
Source code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,b,c,d,e;
double av,p;
printf("enter the marks for five subjects:\n");
scanf("%d %d %d %d %d",&a,&b,&c,&d,&e);
av=(a+b+c+d+e);
p=(av/500)*100;
if (p>=90){
printf("Grade A");
}else if (p>=80){
printf("Grade B");
}else if (p>=70){
printf("Grade C");
}else if (p>=60){
printf("Grade D");
24I360
}else if (p>=50){
printf("Grade E");
}else{
printf("Grade F");
}
return 0;
}
Output:
24I360
10. Input basic salary of an employee and calculate its Gross salary according to
following:
Basic Salary <= 10000 : HRA = 20%, DA = 80%
Basic Salary <= 20000 : HRA = 25%, DA = 90%
Basic Salary > 20000 : HRA = 30%, DA = 95%
Source code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
float t;
printf("Enter the temperature in centigrade:");
scanf("%f",&t);
if(t<0){
printf("Freezing weather");
}else if (t>=0 && t<10){
printf("Very cold weather");
}else if (t>=10 && t<20){
printf("cold weather");
}else if (t>=20 && t<30){
printf("normal weather");
}else if (t>=30 && t<40){
printf("hot weather");
}else {
printf("Very hot");
}
return 0;
}
24I360
Output:
11. Input electricity unit charges and calculate total electricity bill according to the
given condition:
For first 50 units Rs. 0.50/unit
For next 100 units Rs. 0.75/unit
For next 200 units Rs. 1.20/unit
For unit above 250 Rs. 1.50/unit
An additional surcharge of 20% is added to the bill
Source code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int u;
float a,s;
printf("Enter the unit:");
scanf("%d",&u);
if(u<=50){
a=u*0.50;
s=a*0.2;
24I360
a=a+s;
printf("total amount:%.2f",a);
}
if(u>50 && u<=150){
a=25+(u-50)*0.75;
s=a*0.2;
a=a+s;
printf("total amount:%.2f",a);
}
if(u>150 && u<=350){
a=25+75+((u-150)*1.2);
s=a*0.2;
a=a+s;
printf("total amount:%.2f",a);
}
if(u>350){
a=25+75+240+((u-350)*1.5);
s=a*0.2;
a=a+s;
printf("total amount:%.2f",a);
}
return 0;
}
Output:
24I360
12. Create menu driven calculator that performs basic arithmetic operations (add,
subtract, multiply and divide) using switch case. The calculator should input two numbers and
an operator from user. It should perform operation according to the operator entered and must
take input in given format:
number1 operator number2
Source code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
float n1,n2;
char a;
printf("Enter number 1:");
scanf("%f",&n1);
printf("\nEnter operator:");
scanf("%s",&a);
printf("\n.Enter number 2:");
scanf("%f",&n2);
if(a=='+'){
printf("\n%.2f",n1+n2);
}
24I360
else if(a=='-'){
printf("\n%.2f",n1-n2);
}
else if(a=='*'){
printf("\n%.2f",n1*n2);
}
else if(a=='/'){
printf("\n%.2f",n1/n2);
}
else{
printf("INVALID OPERATOR");
}
return 0;
}
Output:
Result:
All the solutions are implemented using c language successfully.
24I360
24I360