You are on page 1of 41

Computer Concepts and Programming [CE143] 21DCS060

CHAROTAR UNIVERSITY OF SCIENCE & TECHNOLOGY


DEVANG PATEL INSTITUTE OF ADVANCE TECHNOLOGY & RESEARCH

Computer Science & Engineering


Subject Code and Name: CE143 Computer Concepts and Programming
Semester: I
Academic year: 2021-2022

DEPSTAR(CSE) 1
Computer Concepts and Programming [CE143] 21DCS060

No. Aim of the Practical


Set – 1
1.1 Write a C program that will output this passage by Michael Singer. Make sure
your
output looks exactly as shown here (including spacing, line breaks, punctuation,
and
the title and author). Use Required Escape Sequence and ASCII Value.
Outcome:

Note:
There are three shapes in the output: Smiling Face, Diamond & Heart.
The ASCII Value for Smiling face is 1.
The ASCII Value for Diamond is 4.
The ASCII Value for Heart is 3.
Also draw flowchart and write algorithm.
Try this example on Turbo C or Code blocks only.

Algorithm
Step 1: Start.
Step 2: Find the ASCII values for smiling face, diamond and heart.
Step 3: Print the given paragraph with proper spacing line break and
punctuation.
Step 4: End.

Flow Chart

Start

DEPSTAR(CSE) 2
Computer Concepts and Programming [CE143] 21DCS060

Print the given


paragraph and border
with help of ascii values.

End

Code
#include<stdio.h>
main()
{
printf("\n %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c
%c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c
%c",1,4,3,1,4,3,1,4,3,1,4,3,1,4,3,1,4,3,1,4,3,1,4,3,1,4,3,1,4,3,1,4,3,1,4,3,1);
printf("\n %c \"If you are resisting something, you are feeding it. \t \t %c",4,4);
printf("\n %c \t Any energy you fight, you are feeding it. \t \t \t %c",3,3);
printf("\n %c \t \t If you are pushing something away, \t \t \t %c",1,1);
printf("\n %c \t \t \t You are inviting it to stay.\" by Michael Singer. %c", 4,4);
printf("\n %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c
%c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c
",1,4,3,1,4,3,1,4,3,1,4,3,1,4,3,1,4,3,1,4,3,1,4,3,1,4,3,1,4,3,1,4,3,1,4,3,1);

Output Screenshot

DEPSTAR(CSE) 3
Computer Concepts and Programming [CE143] 21DCS060

Question – Answer
1. Have you learnt about ASCII values for different symbols other than smile,
diamond and heart? If yes, then mention any 5 ASCII symbols and their
values in tabular format.
Ans. Yes I learnt about ASCII values for different symbols here are few of them
which I learnt:

Sr. No Symbol ASCII Values


1 @ 64
2 ? 63
3 ^ 94
4 ; 59
5 • 149

DEPSTAR(CSE) 4
Computer Concepts and Programming [CE143] 21DCS060

1.2 Write your bio-data using Escape Sequences. And you have to take your Basic
Information as user input. It should contain the following content. It should contain
the following content.
Expected Outcome:
Draw flowchart, write Algorithm and program for given scenario. Also attach
screenshot of output.

Algorithm
Step 1: Start.
Step 2: Print design of #= using printf.
Step 3: Print BIO DATA with having centre alignment with help if \t
escaped sequence.
Step 4: Print your basic information, education qualification.
Step 5: Use 149 as ASCII value for •.
Step 6: Print other information and Thank you at end.
Step 7: End

Flow chart

Start

Print #=
design using
printf.

Print your basic


information and
education
qualification

Use 149 as
ASCII value for •

DEPSTAR(CSE) 5
Computer Concepts and Programming [CE143] 21DCS060

Print other
information and
thank you at end

End

Code
#include<stdio.h>
main()
{
printf("\n
#======#======#======#======#======#======#======#======#======#
======#======#======#");
printf("\n \t\t\t\t BIO-DATA");

printf("\n#======#======#======#======#======#======#======#======#
======#======#======#======#");
printf("\n \t\t\t Basic Information");
printf("\n \t\t\t ---------------------");
printf("\n \t\t\t Name : Aarshi Nakrani");
printf("\n \t\t\t Address : D 1401, Brahmani Society, New Nikol");
printf("\n \t\t\t Mobile Number: +919099135555");
printf("\n \t\t\t Gender : Female");
printf("\n \t\t\t Date of Birth: 27/05/2003");
printf("\n \t");
printf("\n \t\t\t Education Qualification");
printf("\n \t\t -------------------------------------------------");
printf("\n\t %c SSC Percentage: 81 \t Name of School: H.B.Kapadia \t Passing year:
2019",149);
printf("\n\t %c HSC Percentage: 85 \t Name of School: H.B.Kapadia \t Passing year:
2021",149);
printf("\n \t");
printf("\n \t\t\t Other Information");
printf("\n \t\t -------------------------------------------------");
printf("\n \t\t Technical skills: C, HTML");

DEPSTAR(CSE) 6
Computer Concepts and Programming [CE143] 21DCS060

printf("\n \t\t Hobbies : Reading, watching movies");


printf("\n
#======#======#======#======#======#======#======#======#======#
======#======#======#");
printf("\n \t\t\t\t THANK YOU");
printf("\n
#======#======#======#======#======#======#======#======#======#
======#======#======#");
}
Output screenshot

Question – Answer
What is the purpose of using escape sequences? Answer in one or two statements.
Mention any 5 escape sequences used regularly along with their purpose.
Ans. Escape sequences are typically used to specify actions such as carriage returns and
tab movements.

Sr. No. Escape Sequence Purpose


1 \t Horizontal Tab
2 \n New Line
3 \a Alarm or beep
4 \\ Backslash
5 \” Double Quote

DEPSTAR(CSE) 7
Computer Concepts and Programming [CE143] 21DCS060

SET 2
In a town, the percentage of men is 52. The percentage of total literacy is 48. If total
2.1 percentage of literate men is 35 of the total population, write a program to find the
total number of illiterate men and women if the population of the town is 80,000.

Algorithm

Step 1: Start.
Step 2: Population p = 80000.
Step 3: Total men m = (52*p)/100.
Step 4: Total women w = p - m
Step 5: Total literacy l = (48*p)/100.
Step 6: Total literate men lm = (35*p)/100.
Step 7: Total literate women lw = l – lm.
Step 8: Total illiterate men ilm = m – lm.
Step 9: Total illiterate women ilw = w – lw.
Step 10: End.

Flow Chart

Start

Input

p = 80000

m=(52*p)/100;

w = p-m

l = (48*p)/100

lm = (35*p)/100

lw = l – lm

ilm = m – lm

ilw = w -lw

End

DEPSTAR(CSE) 8
Computer Concepts and Programming [CE143] 21DCS060

Code
#include<stdio.h>
main()
{
int p,m,w,l,lm,lw,ilm,ilw;
p = 80000;
m = (52*p) / 100;
w = p - m;
l = (48*p)/100;
lm = (35*p) / 100;
lw = l - lm;
ilm = m - lm;
ilw = w - lw;

printf("\n Total population %d",p);


printf("\n Number of literate (Men + Women) %d",l);
printf("\n Number of men %d",m);
printf("\n Number of literate men %d",lm);
printf("\n Number of illiterate men %d",ilm);
printf("\n Number of women %d",w);
printf("\n Number of literate women %d",lw);
printf("\n Number of illiterate women %d",ilw);
}
Output screenshot

Sr.No Get Outcome Value


1 Total population 80000
2 Number of literate 38400
(Men + Women)
3 Number of Men 41600
4 Number of literate men 28000
5 Number of illiterate men 13600
6 Number of women 38400
7 Number of literate women 10400
8 Number of illiterate women 28000

DEPSTAR(CSE) 9
Computer Concepts and Programming [CE143] 21DCS060

Question – Answer
Has this scenario helped you to learn about integer and float datatype? If yes, then
mention the requirement of integer and float datatype.
Ans:- Integer data type is used when calculkation is to be done of number having non
decimal value while float data type is required when calculation is to be done of numbers
having decimal value.

DEPSTAR(CSE) 10
Computer Concepts and Programming [CE143] 21DCS060

2.2 A Big bazaar cashier has currency notes of denominations 10,50 and 100. If the
amount to be withdrawn is input through the keyboard in hundreds, find the total
number of currency notes of each denomination the cashier will have to give to the
withdrawer.
Algorithm
Step 1: Start
Step 2: Input the amount r.
Step 3: x = r/100; r = r%100.
Step 4: Requirement of 100 Rs. Notes = x.
Step 5: x = r/50; r = r%50.
Step 6: Requirement of 50 Rs. Notes = x.
Step 7: x = r/10; r = r%10.
Step 8: Requirement of 10 Rs. Notes = x.
Step 9: Change left = r.
Step 10: End.

Flow Chart

Start

Input the
amount r.

x = r/100

r = r%100

DEPSTAR(CSE) 11
Computer Concepts and Programming [CE143] 21DCS060

Requirement of 100rs

notes = x

x = r/50

r = r%50

Requirement of 50rs
notes = x

x = r/10

r = r%10

Requirement of 10rs notes = x

Change left = r

End

Code
#include<stdio.h>
#include<conio.h>
main()
{
int r, x;
printf("\n Enter the amount");
scanf("%d",&r);

x = r/100;
r = r%100;
printf("\n Requirement of 100Rs notes: %d",x);

DEPSTAR(CSE) 12
Computer Concepts and Programming [CE143] 21DCS060

x = r/50;
r = r%50;
printf("\n Requirement of 50Rs notes: %d",x);

x = r/10;
r = r%10;
printf("\n Requirement of 10Rs notes: %d",x);

printf("\n Change left: %d",r);


}
Output screenshot

Sr.No. Note requirements Count


1 Requirement of 100rs 58
notes
2 Requirement of 50rs notes 1
3 Requirement of 10rs notes 2
4 Change left 4

Question – Answer
1. Have you learned about how scanf function can be used to collect the user input?
Give the correct answer for the following table:
Sr.No. Data Type Format specifier Example of data
1 Integer %d 2
2 Float %f 2.5
3 Character %c a

DEPSTAR(CSE) 13
Computer Concepts and Programming [CE143] 21DCS060

2.3 Write a program to calculate Net Salary. User has to input Basic Salary and
Output should be:
Enter Basic Salary: 5000 (e.g. 5000)
Allowances:
DA = 70% of Basic Salary
HRA = 7% of Basic Salary
MA = 2% of Basic Salary
TA = 4% of Basic Salary
Deduction:
PF = 12% of Basic Salary
IT = any value (e.g. 500)
Net Salary = Basic Salary + Allowances – Deduction

Algorithm
Step 1: Start.
Step 2: Input basic salary bs.
Step 3: DA = (70*bs)/100.
Step 4: HRA = (7*bs)/100.
Step 5: MA = (2*bs)/100.
Step 6: TA = (4*bs)/100.
Step 7: PF = (12*bs)/100.
Step 8: IT = 500.
Step 9: Gross salary = bs+DA+HRA+MA+TA.
Step 10: Net salary = bs+DA+HRA+MA+TA-PF-IT.
Step 11: End.
Flow Chart
Start

DA = (70*bs)/100

HRA = (7*bs)/100

MA = (2*bs)/100

TA = (4*bs)/100

PF = (12*bs)/100

IT = 500

Gross Salary = bs+DA+HRA+TA+MA

Net Salary = bs+DA+HRA+MA+TA-PF-IT

End

DEPSTAR(CSE) 14
Computer Concepts and Programming [CE143] 21DCS060

Code
#include<stdio.h>
#include<conio.h>
main()
{
float bs,it;
printf("\n Enter the basic salary");
scanf("%f",&bs);
printf("\n DA of basic salary %.2f",(70*bs)/100);
printf("\n HRA of basic salary %.2f",(7*bs)/100);
printf("\n MA of basic salary %.2f",(2*bs)/100);
printf("\n TA of basic salary %.2f",(4*bs)/100);
printf("\n Total allowances
%.2f",((70*bs)/100)+((7*bs)/100)+((2*bs)/100)+((4*bs)/100));
printf("\n PF of basic salary %.2f",(12*bs)/100);
it = 500;
printf("\n IT of basic salary %.2f",it);
printf("\n Total deduction %.2f", ((12*bs)/100)+(it));
printf("\n Gross salary: %.2f",
(bs)+((70*bs)/100)+((7*bs)/100)+((2*bs)/100)+((4*bs)/100));
printf("\n Net salary:
%.2f",(bs)+((70*bs)/100)+((7*bs)/100)+((2*bs)/100)+((4*bs)/100)- ((12*bs)/100)-(it));
}
Output screenshot

Sr.No. Input/Outputs Amount


1 Basic Salary 5000
2 DA of basic salary 3500
3 HRA of basic salary 350
4 MA of basic salary 100
5 TA of basic salary 200
6 PF of basic salary 600
7 Gross salary 9150
8 Net Salary 8050

DEPSTAR(CSE) 15
Computer Concepts and Programming [CE143] 21DCS060

Question – Answer
1. Have you learned about various data types that can be suitably used for this
problem? Do mention which data types can be used and why? Also mention the
difference between the outputs.
Ans:- The basic data types used in C are as follows:
• char: The most basic data type in C. It stores a single character and requires
a single byte of memory in almost all compilers.
• int: As the name suggests, an int variable is used to store an integer.
• float: It is used to store decimal numbers (numbers with floating point
value) with single precision.
• double: It is used to store decimal numbers (numbers with floating point
value) with double precision.

DEPSTAR(CSE) 16
Computer Concepts and Programming [CE143] 21DCS060

SET 3
Write a program that takes the length of the pendulum as input and then calculate
3.1 the time period of the pendulum. Provided that, T=2π√L/G. Define the value of π as
3.14 and take L as the length of the pendulum and G as the acceleration of gravity
either in m/s2 or as input from the keyboard. Display the time period rounded to 2
decimal places.
Algorithm
Step 1: Start
Step 2: Define pi = 3.14.
Step 3: Input L in metre and G in m/s2 .
Step 4: T = 2*pi*sqrt(L/G)
Step 5: Print T.
Step 6: End
Flow Chart
Start

Define pi = 3.14

Input L in meter and G in m/s2

T = 2*pi*sqrt(L/G)

Print T

End

Code
#include<stdio.h>
#include<math.h>
#define pi 3.14
main()
{
float l,g,T;
printf("\n Enter the length of pendulum in m:"),

DEPSTAR(CSE) 17
Computer Concepts and Programming [CE143] 21DCS060

scanf("%f",&l);
printf("\n Enter the acceleration due to gravity in m/s^2:");
scanf("%f",&g);
T = 2*pi*sqrt(l/g);
printf("\n Time period of the pendulum is: %.2f s",T);
}
Output screenshot

DEPSTAR(CSE) 18
Computer Concepts and Programming [CE143] 21DCS060

Input Output
Sr.No
Length Gravity Time(s)
1 50m 9.8m/s2 14.19
2 50m 0m/s2 Error
3 50m 0.9993 g 14.19
4 50m -1 g Error

DEPSTAR(CSE) 19
Computer Concepts and Programming [CE143] 21DCS060

Question – Answers
1. Have you learned about, how math function is useful for calculating square root?
Which datatype is supported by all math functions? Also mention any 5 math
functions with their purpose.
Ans. Float datatype is supported by all math functions.
Sr.No Math Function Description
1 pow Returns the power of given
number.
2 abs Return the absolute value of given
number.
3 ceil Returns the integer value which is
greater than or equal to given
number.
4 floor Returns the integer value which is
less than or equal to given
number.
5 log Returns the natural logarithm
(base-e logarithm) of given
number.

DEPSTAR(CSE) 20
Computer Concepts and Programming [CE143] 21DCS060

3.2 Let us understand the working of Pre-increment, Post-increment, Pre-decrement


and Post-decrement
a) Consider a scenario where, Boys are playing in the park and collecting and
removing the yellow balls in/from the bucket based on teacher’s instruction.
Let’s say there are already 10 Yellow balls present in a bucket. Following is the
sequence of the instructions given by the teacher for adding/removing the balls.
i. Rajiv: ++ Yellow
ii. Preet: --Yellow
iii. Raj: Yellow++
iv. Ritul: Yellow- -
Algorithm
Step 1: Start.
Step 2: y = 10.
Step 3: y = ++y and print Rajiv’s count = y.
Step 4: y = --y and print Preet’s count = y.
Step 5: a = y++ and print Raj’s count = a.
Step 6: b = y—and print Ritul’s count = b.
Step 7: End.
Flow chart
Start

Declare
y=10

y=++y

Rajiv’s count = y
End

y=--y
a=y++ Preet’s count = y
Raj’s count = y

b=y—

Ritul’s count = b

DEPSTAR(CSE) 21
Computer Concepts and Programming [CE143] 21DCS060

Code
#include<stdio.h>
main()
{

int y=10,a,b;

printf("\n Count before execution: %d",y);


y = ++y;
printf("\n Rajiv's Count: %d",y);
y = --y;
printf("\n Preet's Count: %d",y);
a = y++;
printf("\n Raj's Count: %d",a);
b = y--;
printf("\n Ritul's Count: %d",b);
printf("\n Count after execution: %d",b);
}
Output screenshot

Sr.No. Instructions Yellow


1 Count before execution 10
2 Count after execution 11

DEPSTAR(CSE) 22
Computer Concepts and Programming [CE143] 21DCS060

3.2 b) Consider another scenario where boys and girls both are asked to add/remove
Yellow and Pink balls from the bucket respectively. Currently there are 10 Yellow
balls in the bucket and 20 Pink balls. Teacher has given the sequence of
instructions as below for adding/removing the balls. Calculate = ++Yellow +
Yellow++ + --Yellow + ++Pink - --Pink - --Pink. Get the count of Yellow and Pink
balls after evaluating above given scenario.
Algorithm
Step 1: Start.
Step 2: Input y = 10, p = 20.
Step 3: cal = ++y + y++ + --y + ++p - --p - --p.
Step 4: Count of yellow after evaluation = y
Count of pink after evaluation = p.
Step 5: End.
Flow Chart

Start

Input y =10

p =20

cal = ++y + y++ + --y + ++p - --p - --p

Print cal

Count of yellow after evaluation = y

Count of pink after evaluation = p

End

DEPSTAR(CSE) 23
Computer Concepts and Programming [CE143] 21DCS060

Code

#include<stdio.h>
main()
{
int y,p,cal;
printf("\n Eter the value of y:");
scanf("%d",&y);
printf("\n Enter the value of p:");
scanf("%d",&p);
cal = (++y) + (y++) + (--y) + (++p) - (--p) - (--p);
printf("\n Value of calculate is: %d",cal);
printf("\n Count of yellow after evaluation: %d",y);
printf("\n Count of pink after evaluation: %d",p);
}
Output screenshot

Sr.No. Instructions Yellow Pink


1 Count before execution 10 20
2 Count after execution 11 19

Question – Answers

Have you understood the working of Pre-increment, Post-increment, Pre-decrement


and Post-decrement?

Ans:- Yes through this practical I understood the working of Pre-increment, Post-
increment, Pre-decrement and Post-decrement.

DEPSTAR(CSE) 24
Computer Concepts and Programming [CE143] 21DCS060

3.3 Write a C program to swap two numbers (use two variables for collecting value from
user) without using third variable. (Hint: Use arithmetic operators)
Algorithm
Step 1: Start.
Step 2: Input the value of a, b.
Step 3: Value of a before swapping = a
Value of b before swapping = b
Step 4: a = a+b
Step 5: b = a-b
Step 6: a = a-b
Step 7: Value of a after swapping = a
Value of b after swapping = b
Step 8: End.
Flow Chart

Start

Input value of a,b

a=a+b

b=a-b

a=a-b

Print new
values of a,b

End

Code
#include<stdio.h>
main()
{
float a,b;
printf("\n Enter the value of a:");
scanf("%f",&a);
printf("\n Enter the value of b:");

DEPSTAR(CSE) 25
Computer Concepts and Programming [CE143] 21DCS060

scanf("%f",&b);
printf("\n Values of a and b before swapping are %f and %f",a,b);
a = a+b;
b = a-b;
a = a-b;
printf("\n Values of a and b after swapping are %f and %f",a,b);
}
Output screenshot

Sr.No. Instruction a b
1 Before swapping 5.652136 4.321654
After
2 4.321654 5.652136
swapping

Question – Answers
1. Have you learned about, how we can use arithmetic operators for swapping the
numbers?
Ans. Yes we can use arithmetic operators such as addition, subtraction for swapping of
two numbers.

DEPSTAR(CSE) 26
Computer Concepts and Programming [CE143] 21DCS060

SET 4
4.1 a) Write something about your characteristics not more than 50 words using gets
function and print out the same using puts function.

Algorithm
Step 1: Start
Step 2: Describe data[10];
Step 3: Enter characteristics;
Step 4: Print Characteristics
Step 5: Stop

Flow chart

Start

Declare data[10]

Enter Characteristics

Print Characteristics

Stop

Code :

#include<stdio.h>
#include<conio.h>
void main()

DEPSTAR(CSE) 27
Computer Concepts and Programming [CE143] 21DCS060

{
char data[10];
clrscr();

printf("\n Enter your charecteristics:");


gets(data);

printf("\n Your charecteristics are:");


puts(data);
getch();
}

Output Screenshot:

Questions:
1. What is the significance of using gets and puts? Are they acting as replacement
of any function? How?

The use of gets and puts is to print the function in c and to ask value from user in c
respectively. Here they are acting as replacement of function printf and scanf in c
programming. Where printf is used at that place the puts function is used to print the line
in c and similarly to ask the value instead of scanf the function gets is used,.

DEPSTAR(CSE) 28
Computer Concepts and Programming [CE143] 21DCS060

4.1 b) Write a program to convert the decimal number into octal and hexadecimal
format. Print hexadecimal and octal values for given inputs in expected
outcomes. (Hint: Use %o and %x)

Expected outcome must be filled in below table:

Algorithm
Step 1: Start
Step 2: Describe no;
Step 3: Print decimal value;
Step 4: Hexadecimal form is %x;
Step 5: Octal form is %o;
Step 6: Print Hexadecimal form and octal form.
Step 7: Stop
Flow Chart
Start

Declare no

Hexadecimal form is %x
Octadecimal form is %o

Print Hexadecimal form and


End
octadecimal form

DEPSTAR(CSE) 29
Computer Concepts and Programming [CE143] 21DCS060

Code

#include<stdio.h>
#include<conio.h>
void main()
{
int no;
clrscr();

printf("\n Enter decimal number :");


scanf("%d",&no);

printf("\n Hexadecimal of %d is %x",no,no);


printf("\n Octadecimal of %d is %o",no,no);
getch();

Output screenshot

Sr no. Inputs Octal Hexadecimal


1 78 116 4e
2 143 217 8f
3 0 0 0
4 1 1 1
5 -1 177777 ffff

DEPSTAR(CSE) 30
Computer Concepts and Programming [CE143] 21DCS060

4.2 Write a C Program to Print multiplication table from 1 to 7 to achieve the following
output. (Use #define directives and do while loop)
Algorithm
Step 1: Start
Step 2: Describe i.
Step 3: Using loop print number for (i=1; i<=10; i++)
Step 4: Use another loop for column such as (j=1; j<=7; j++)
Step 5: Print the number and multiply it as i*j
Step 6: Stop
Flow chart

Start

Declare i

Yes For i=1 No


to 10

For j=1
to 7

Yes

Print Number i*j

No

End

DEPSTAR(CSE) 31
Computer Concepts and Programming [CE143] 21DCS060

Code
#include<stdio.h>
main()
{
int i,j;
printf("\n \t \t \t MULTIPLICATION TABLE(1 TO 7)");
printf("\n ----------------------------------------------------------------------");
printf("\n");
for(i = 1; i<=10; i = i+1)
{
printf("\n");
for(j=1; j<=7; j++)
{
printf("\t%d",i*j);
}
}
printf("\n ----------------------------------------------------------------------");

}
Output screenshot

DEPSTAR(CSE) 32
Computer Concepts and Programming [CE143] 21DCS060

5.1 Write a C program for the given scenario from the flowchart. Note that you have to
enter your own height in centimetres.

Expexted outcome must be like this:

Algorithm

Step 1: Start

Step 2:Declare H

Step 3: input H

Step 4: check if H<150

Step 5: print Height is drawf

Step 6: check if H>=150 && H<165

Step 7: print Height is Average.

Step 8: Check if H>=165 && H<=195

Step 9: print Height is Tall.

Step 10:If H>195

Step11:Print U have abnormal height.

Step 12:Stop.

DEPSTAR(CSE) 33
Computer Concepts and Programming [CE143] 21DCS060

Flow chart

Start

Declare H

H<150

Drawf
H>=150
&& H<165

Avg Height
H>=165
&&
H<=195

Ab Tall

Normal

height

The Person is Drawf..

End

DEPSTAR(CSE) 34
Computer Concepts and Programming [CE143] 21DCS060

Code
#include<stdio.h>
main()
{
int h;
printf("\n Enter your height in centimeters:");
scanf("%d",&h);
if(h<150)
{
printf("\n Person is dwarf");
}
if(h>=150 & h<165)
{
printf("\n Person has average height");
}
if(h>=165 & h<=195)
{
printf("\n Person is tall");
}
else
{
printf("\nPerson has abnormal height");
}
}
Output screenshot

Sr. No. Inputs (cm) Dwarf Average Tall Abnormal


Your Height
1 √
Your Mother’s
2. √
height
Your Father’s
3.
height √
Your Sibling’s
4.
height √

DEPSTAR(CSE) 35
Computer Concepts and Programming [CE143] 21DCS060

5.2 Write a C program to find all roots of a Quadratic equation using nested switch case.
Take three user inputs from keyboard for finding the discriminant (b2 – 4ac). Use
the
concept of nested switch case for finding the roots of equation. Get the outputs for
roots
till 2 decimal points only.
Hint: Discriminant > 0
root1 = (-b + sqrt(discriminant)) / (2*a)
root2 = (-b - sqrt(discriminant)) / (2*a)
Discriminant < 0
root1 = root2 = -b / (2*a)
imaginary = sqrt (-discriminant) / (2*a) (eg. Print it as: i20.3, i.e. i followed by value)
Discriminant = 0
root1 = root2 = -b / (2*a)

Expected outcome must be filled in below table:

Algorithm

DEPSTAR(CSE) 36
Computer Concepts and Programming [CE143] 21DCS060

Flow Chart

Start

Input a,b,c

d = b2-4ac

Switch

(d>0)

Case 1 Case 0

Switch

(d<0)
Case 1

Print
roots
root1 = root2 = -b/(2*a)
Case 0
imaginary = sqrt(-
discriminant)/(2*a)

root1 = root2 = -b/(2*a);


End
printf("Two equal and real roots exists: %.2f
and %.2f",root1, root2)

DEPSTAR(CSE) 37
Computer Concepts and Programming [CE143] 21DCS060

Code
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n;
float a,b,c,root1,root2,d;
float i;
clrscr();

printf("\n Enter any value of a:");


scanf("%f",&a);

printf("\n Enter any value of b:");


scanf("%f",&b);

printf("\n Enter any value of c:");


scanf("%f",&c);

d=b*b-4*a*c;
if(d>0)
{
n=1;
}
else
{
if(d<0)
{
n=2;
}
else
{
n=3;
}
}

switch(n)
{
case 1:
root1=(-b+sqrt(d))/(2*a);
root2=(-b-sqrt(d))/(2*a);
printf("\n The value for root1 is %f and root2 is %f",root1,root2);
break;

case 2:
root1=-b/2*a;
i=sqrt(-d)/(2*a);
printf("\n The value of root is %f + i%f",root1,i);

DEPSTAR(CSE) 38
Computer Concepts and Programming [CE143] 21DCS060

break;

case 3:
root1=root2=-b/2*a;
printf("\n The value of root is %f",root1);
break;
default:
printf("\n AAP GALAT HOOOO||||");
}
getch();
}
Output Screenshot:

Sr No. Inputs Root 1 Root 2 Imaginary


a b c
1. 1 2 3
2. 3 -7 -5
3. 9 12 4

DEPSTAR(CSE) 39
Computer Concepts and Programming [CE143] 21DCS060

DEPSTAR(CSE) 40
Computer Concepts and Programming [CE143] 21DCS060

DEPSTAR(CSE) 41

You might also like