Lab Practical 3

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

Name: kripa patel

Roll No. : 24itp015


Course Code and Name: 1CS501 Computer Programming

Practical no.:3(a)
Aim: Write a program to take the values for A, B, C of a
quadratic equation
A∗ X2+B∗ X+C=0 and then find all the roots of the
equation. It is guaranteed
that A≠ 0 and that the equation has at least one real root.
Methodology followed:
#include<stdio.h>
#include<math.h>
void main()
{
int a,b,c,x;
float x1,x2,d;
printf("a,b,c are coefficients of quadratic equation");
printf("Enter the value of a:");
scanf("%d",&a);
printf("Enter the value of b:");
scanf("%d",&b);
printf("Enter the value of c:");
scanf("%d",&c);
d=b*b-4*a*c;
printf("\nThe value of discriminant is %.2f",d);
if(d<0)
{
printf("\nThe quadratic equation is imaginary hence has no real
roots.");
}
else if(d==0)
{
printf("\nThe quadratic equation has 2 real and equal roots"); x=-b/2*a;
}
else
{
printf("\nThe quadratic equation has 2 real and distinct roots"); x1=(-
b+sqrt(d))/2*a;
x2=(-b-sqrt(d))/2*a;
printf("\nThe roots are%.2f and %.2f",x1,x2);
}
}

Output:

Practicle no.:3(b)

Aim: In an organization, employees are paid on an hourly


basis. Clerks are
paid 100/hr, Teachers are paid 200/hr and the principal is paid
400/hr. If the
weekly hours exceed 44, then employees 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 the best suitable
control construct
to implement the program.
Methodology followed:
#include<stdio.h>
int main()
{
int cat,hr,rate,pay;
printf("Enter category \n 1. clerk\n 2. Teacher \n 3. Principle\n");
scanf("%d",&cat);
printf("Please enter number of hours:");
scanf("%d",&hr);
if(cat==1)
{
printf("You selected the category clerk\n");
rate=100;
if(hr<=44)
{
pay=rate*hr;
printf("The salary to paid to the clerk is %d",pay);
}
else if(hr>44&&hr<=50)
{
pay=(rate*44)+(rate*(hr-44)*2);
printf("The salary to paid to the clerk is %d",pay);
}
else
{
pay=(rate*44)+(rate*6*2);
printf("The salary to paid to the clerk is %d",pay);
}
}
else if(cat==2)
{
printf("You selected the category teacher\n");
rate=200;
if(hr<=44)
{
pay=rate*hr;
printf("The salary to paid to the teacher is %d",pay);
}
else if(hr>44&&hr<=50)
{
pay=(rate*44)+(rate*(hr-44)*2);
printf("The salary to paid to the teacher is %d",pay);
}
else
{
pay=(rate*44)+(rate*6*2);
printf("The salary to paid to the teacher is %d",pay);
}
}
else if(cat==3)
{
printf("You selected the category principle\n");
rate=400;
if(hr<=44)
{
pay=rate*hr;
printf("The salary to paid to the principle is %d",pay);
}
else if(hr>44&&hr<=50)
{
pay=(rate*44)+(rate*(hr-44)*2);
printf("The salary to paid to the principle is %d",pay);
}
else
{
pay=(rate*44)+(rate*6*2);
printf("The salary to paid to the principle is %d",pay);
}
}
return 0;

Output:

Practicle no.:3(c)
Aim: Ajay and Amit are playing a game with a number X. In one
turn, they
1,2,3,4
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)).
Methodology followed:

#include<stdio.h>

int main()

int x;

printf("Enter the value of x:");

scanf("%d",&x);

if(x%10==0)

printf("X is divisible by 10 in 0 turn");

else if(x*2%10==0)

printf("X is divisible by 10 in 1 turn");


}

else

printf("X is not divisible by 10");

Output:
Practicle no.:3(d)
Aim: 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 message (correct or incorrect).
Methodology followed:
#include<stdio.h>

#include<stdlib.h>

#include<time.h>

void main()

int a,b;

srand(time(0));

a=rand()%10;

printf("Please enter number between 0 to 9:");

scanf("%d",&b);

if(a==b)

printf("You won the game!");


}

else

{
printf("You lost the game");
printf("\nThe correct answer is %d",a);

Output:

Practicle no.:3(e)
Aim: Write a C program to find the grade of a student based
on the
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.
Grade Range of total
marks
A+ 91-100
A 81-90
B+ 71-80
B 61-70
C+ 51-60
C >40
Fail <40

Methodology followed:

#include<stdio.h>

int main()

float ct,tut,se,see,total,lpw,a,b,c,d,e;

printf("Enter marks of Class test:");


scanf("%f",&ct);

printf("Enter marks of Tutorial:");

scanf("%f",&tut);

printf("Enter marks of SE:");

scanf("%f",&se);

printf("Enter marks of SEE:");


scanf("%f",&see);

printf("Enter

marks of LPW:");

scanf("%f",&lpw);

a=12*ct/100;

b=12*tut/100;

c=16*se/100;

d=40*see/100;

e=20*lpw/100;

total=(a+b+c+d+e);

if(total>=90)

printf("You have

A+ grade!"); }

else

if(total>=80&&tot

al<90) {
printf("You have

A grade!"); }

else

if(total>=70&&tot

al<80) {

printf("You have

B+ grade!"); }

else

if(total>=60&&tot

al<70) {

printf("You have

B grade!"); }

else if(total>=50&&total<60)
{

printf("You have C+ grade!");

else if(total>=40&&total<50)

printf("You have C grade!");

else

printf("You have F grade");


}

Output:

You might also like