3
3
PRACTICAL NO. : 3
#include<stdio.h>
#include<math.h>
int main()
{
double a, b, c, discriminant, root1, root2, realpart , imgpart;
printf("enter coefficients a, b, c:");
scanf("%lf %lf %lf", &a, &b, &c);
discriminant = b*b-4*a*c;
if (discriminant>0)
{
root1 = (-b+sqrt(discriminant))/(2*a);
root2 = (-b-sqrt(discriminant))/(2*a);
printf ("root1 =%.2lf and root2 =%.2lf",root1,root2);
}
else if (discriminant==0)
{
root1=root2= -b/(2*a);
printf("root1 = root2 = %.2lf:",root1);
}
else
{
realpart = -b/(2*a);
imgpart = sqrt(-discriminant)/ (2*a);
printf("root1 =%.2lf+%.2lfi and root2 =%.2lf-%2lfi",realpart,imgpart,realpart,imgpart);
}
return 0;
}
Theoretical Principles used : Initialize variable name and then use
discriminant formula after that use conditional is statement and then instruct
what to do then use else if statement for other instructions by using different
formula we will get the desired results.
b.) Write a C program to make a simple calculator using the
following:
❖ If…….else if
#include<stdio.h>
int main()
{
float a,b;
char ope;
printf("enter a number a \n");
scanf("%f",&a);
printf("enter a number b \n");
scanf("%f",&b);
printf("enter the arithmetic operator,+,-,*,/\n");
scanf("%c",&ope);
if (ope=='+')
{
printf("%f",a+b);
}
if (ope=='-')
{
printf("%f",a-b);
}
if (ope=='*')
{
printf("%f",a*b);
}
if (ope=='/')
{
printf("%f",a/b);
}
return 0;
}
Theoretical Principles used : Firstly ask the user to input the operation he/she wants
to perform then take values of two number on which the operations are to be
performed then with the help of if else assign addition , subtraction , multiplication
and division and finally we made a simple calculator with just if and else statements.
❖ Switch-case
#include <stdio.h>
int main()
{
char opt;
int n1, n2;
float res;
printf (" Choose an operator(+,-, *, /) to perform the operation in C Calculator \n ");
scanf ("%c", &opt);
if (opt == '/' )
{
printf (" You have selected: Division");
}
else if (opt == '*')
{
printf (" You have selected: Multiplication");
}
switch(opt)
{
case '+':
res = n1 + n2;
printf (" Addition of %d and %d is: %.2f", n1, n2, res);
break;
case '-':
res = n1- n2;
printf (" Subtraction of %d and %d is: %.2f", n1, n2, res);
break;
case '*':
res = n1 * n2;
printf (" Multiplication of %d and %d is: %.2f", n1, n2, res);
break;
case '/':
if (n2 == 0)
{
printf (" \n Divisor cannot be zero. Please enter another value ");
scanf ("%d", &n2);
}
res = n1 / n2;
printf (" Division of %d and %d is: %.2f", n1, n2, res);
break;
default:
printf (" Something is wrong!! Please check the options ");
}
return 0;
}
Theoretical Principles used : First ask the user for the kind of operator he wants to
use then ask for two numbers and then with the help of switch case operator
switch the operator and assign the case that if its + then do addition so on and so
forth by this we made a simple calculator using switch case.
c.) In an organisation, employees are paid on hourly basis.
Clerks are paid 100/hr, Teachers are paid 200/hr and
Principal is paid 400/hr. If the weekly hours exceed 44, then
employee should be paid 2 times their regular pay for the
overtime. Write a C program to compute the weekly salary
of the employee and also the program should take care that
the employee should not be paid for hours beyond 50 in a
week. Use best suitable control construct to implement the
program.
#include<stdio.h>
int main()
int cls,thrs,ext,prs,tes;
char person,clerk,principal,teacher;
ext=thrs-44;
if(thrs<44)
cls=thrs*100;
tes=thrs*200;
prs=thrs*400;
cls=44*100+ext*200;
tes=44*200+ext*400;
prs=44*100+ext*800;
if(thrs>50)
cls=44*100+6*200;
tes=44*200+6*400;
prs=44*400+6*800;
}
getchar();
scanf("%c",&person);
switch(person)
case'x':
printf("%d",cls);
break;
case'y':
printf("%d",tes);
break;
case'z':
printf("%d",prs);
break;
return 0;
}
Theoretical Principles used : First initialize all the variables then ask the user
for the number of hours he worked then ask whether he is a clerk, teacher or
principal then with if statement first give its salary of less than44 hours then
between 44 to 50 then exceeding 50 . State all the salary according to
question and you will get the salary according to number of hours you
worked.
d.) Ajay and Amit are playing a game with a number X. In one
turn, they can multiply X by 2. The goal of the game is to
make X divisible by 10. Write a C program to find the
number of turns necessary to win the game (it may be
possible to win in zero turn, 1 turn or it might be impossible
(-1 turns)).
#include<stdio.h>
int main()
{
int a;
int i=0;
scanf("%d",&a);
if(a%5!=0)
{
printf("number of turn to win -1");
}
if(a%5==0)
{
while (a%10!=0)
{
a=a*2;
i++;
}
printf("number of turn to win %d",i);
}
return 0;
}
e.) Write a program to implement a simple number guessing
game. Program should generate an integer randomly and
ask the user to guess the integer. Based on the number
guessed, it should display the appropriate messasge
(correct or incorrect).
#include <stdio.h>
#include<stdlib.h>
int main()
srand(time(0));
do
scanf("%d", &guess);
tries++;
printf("Too high!\n\n");
printf("Too low!\n\n");
else
return 0;
}
Theoretical Principles used : Initialize number and guess and put tries equal
to 0 then with help of do print the statement and tries++ now with if else
statement print some conditions while guess is not equal to number.
f.) Write a C program to find the grade of a student based on
following policy. Class test: 12% weightage,
Tutorial:12%,SE:16%LPW:20%,SEE:40%. Grade is decided
based on the below range of total marks.
void main()
int marks;
scanf("%d",&marks);
if(marks<0 || marks>100)
printf("Wrong Entry");
else if(marks<40)
printf("Fail");
else if(marks>40)
printf("Grade C");
{
printf("Grade C+");
printf("Grade B");
printf("Grade B+");
printf("Grade A");
else
printf("Grade A+");
return 0;
}
Theoretical Principles used : First scan the marks given by user then with the
help of if and else statement print some condition of grade according to
marks and then display the desired results.