[go: up one dir, main page]

0% found this document useful (0 votes)
14 views26 pages

3

The document contains a series of C programming exercises focused on conditional statements, including programs for solving quadratic equations, creating a simple calculator, calculating employee salaries based on hours worked, and implementing games. Each exercise includes code snippets and explanations of the theoretical principles used, such as if-else statements and switch-case constructs. The document serves as a practical guide for demonstrating the use of conditional logic in programming.

Uploaded by

nidhibangar2245
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views26 pages

3

The document contains a series of C programming exercises focused on conditional statements, including programs for solving quadratic equations, creating a simple calculator, calculating employee salaries based on hours worked, and implementing games. Each exercise includes code snippets and explanations of the theoretical principles used, such as if-else statements and switch-case constructs. The document serves as a practical guide for demonstrating the use of conditional logic in programming.

Uploaded by

nidhibangar2245
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

DATE : 27/10/22

ROLL NO. : 22CMP075

COURSE CODE AND NAME : 1CS501 COMPUTER PROGRAMMING

PRACTICAL NO. : 3

AIM : C programs to demonstrate use of conditional statements


a.) 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 is not equal to 0 and that
the equation has at least one real root.

#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");
}

else if (opt == '-')


{
printf (" You have selected: Subtraction");
}
else if (opt == '+')
{
printf (" You have selected: Addition");
}
printf (" \n Enter the first number: ");
scanf(" %d", &n1);
printf (" Enter the second number: ");
scanf (" %d", &n2);

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;

printf("enter total hours:");


scanf("%d",&thrs);

ext=thrs-44;

if(thrs<44)

cls=thrs*100;

tes=thrs*200;

prs=thrs*400;

if (thrs>44 && thrs<50)

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;
}

printf("enter whose salary is required:");

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()

int num, guess, tries = 0;

srand(time(0));

num = rand() % 100 + 1;

printf("Guess My Number Game\n\n");

do

printf("Enter a guess between 1 and 100 : ");

scanf("%d", &guess);
tries++;

if (guess > num)

printf("Too high!\n\n");

else if (guess < num) {

printf("Too low!\n\n");

else

{printf("\nCorrect! You got it in %d guesses!\n", tries);

}while (guess != num);

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.

Grade Range of total marks


A+ 91-100
A 81-90
B+ 71-80
B 61-70
C+ 51-60
C >40
Fail <40
#include<stdio.h>

void main()

int marks;

printf("Enter your 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");

else if(marks>=50 && marks<60)

{
printf("Grade C+");

else if(marks>=60 && marks<70)

printf("Grade B");

else if(marks>=70 && marks<80)

printf("Grade B+");

else if(marks>=80 && marks<90)

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.

You might also like