Exp 1
Exp 1
Experiment No. – 1
Date of Performance:
Date of Submission:
Program Execution/
formation/ Timely
Viva Experiment Sign with
correction/ Submission
(03) Total (10) Date
ethical practices (01)
(06)
Experiment No. 1
C operators and if else, If else ladder
Arithmetic Operators
Logical(or Relational) Operators
Bit-wise Operators
Assignment Operators
MiscOperators
ArithmeticOperators:
TherearefollowingarithmeticoperatorssupportedbyClanguage:Assu
Logical(orRelational)Operators:
BitwiseOperators:
Bitwiseoperatorworksonbitsandperformsbitbybitoperation.
follows:A=0011 1100
B=0000 1101
A&B=00001100
A|B=00111101
A^B=00110001
~A=1100 0011
TherearefollowingBitwiseoperatorssupportedbyClanguage
AssignmentOperators:
TherearefollowingassignmentoperatorssupportedbyClanguage:
MiscOperators
TherearefewotheroperatorssupportedbyCLanguage.
OperatorsCategories:
Alltheoperatorswehavediscussedabovecanbecategorizedintofollowingcategories:
Postfixoperators,whichfollowasingleoperand.
Unaryprefixoperators,whichprecedeasingleoperand.
Binary operators, which take two operands and perform a variety of
arithmeticandlogical operations.
The conditional operator (a ternary operator), which takes three operands
andevaluates either the second or third expression, depending on the
evaluation ofthefirst expression.
Assignmentoperators,whichassignavaluetoavariable.
The comma operator, which guarantees left-to-right evaluation of comma-
separatedexpressions.
PrecedenceofCOperators:
tor:
Forexamplex=7+3*2;Herexisassigned13,not20becauseoperator*hashigherprecedenace
than+soit firstgetmultipliedwith3*2 andthenadds into7.
Here operators with the highest precedence appear at the top of the table, those
withthe lowest appear at the bottom. Within an expression, higher
precedenaceoperatorswillbeevaluated first.
Problem statement :(a) Write a C program to find the sum and average of
three numbers.
Algorithm:
Step 1: Start
Step 2: Declare variables num1, num2,num3 and sum,average.
Step 3: Read values num1,num2,num3
Step 4: Add num1,num2,num3 and assign the result to sum. sum←num1+num2 +num3 average ←
sum/3
Step 5: Display sum and average
Step 6: Stop
Flow chart:
Program and
output :
ProblemStatement:(b)
Writeaprogramtoacceptthreenumbersanddisplaylargestofthreeusinganestedifelsestate
ment
Theory:
Theifstatement
Theifstatementgivestheuserthechoiceofexecutingastatement(possiblycompound) ifthe
expression is evaluated to true or skipping it is the expression isevaluatedto false.
Format1:
if(expression)
{
statement
}
Thestatementisexecutedifandonlyiftheexpressionis true.
Example
if(num>10)
{
result=2 *num;
}
Thecontentofnumis multiplyby2ifand onlyifthevalueofnumisgreaterthan 10.
Format2:Clanguagealsoletsonechoosebetweentwostatementsbyusingtheif-
elseifstructure.
if(expression)
{
Statement 1
}
else
{
Statement2
}
Inthiscase,iftheexpressionistrue,thenthestatement1isexecuted.Otherwise,
1-9
Skill Based Lab: C Programming (ETLOR0VS101) A.Y. 2024-25
U
G Program in Electronics and Telecommunication Engineering
(Accredited by NBA for 3 years from AY 2024-25)
statement2isexecuted.
Example
if (num> 10)
{ result = 2 * num;
}
else
{ result = 3* num;
} result=2* num;
Intheaboveexample,ifthenumisgreaterthan10thentheresultisequaltothenum
multipliedby2 otherwiseit is equaltothenummultiplied by3.
Algorithm:
Step1:Start
Step2:Acceptthreenumbersa, b,c
Step3:if(a>b)and
if(a>c)
thendisplay
aelsethenif(
b>c)thendis
playbelseth
endisplayc
Step4:Displaylargestnumber
Step5:Stop
Flow chart:
1-10
Skill Based Lab: C Programming (ETLOR0VS101) A.Y. 2024-25
U
G Program in Electronics and Telecommunication Engineering
(Accredited by NBA for 3 years from AY 2024-25)
Program
and Output
1-11
Skill Based Lab: C Programming (ETLOR0VS101) A.Y. 2024-25
U
G Program in Electronics and Telecommunication Engineering
(Accredited by NBA for 3 years from AY 2024-25)
Input:Coefficientsofquadraticequationa,bandc
Processing:Usingtheformula
1-12
Skill Based Lab: C Programming (ETLOR0VS101) A.Y. 2024-25
U
G Program in Electronics and Telecommunication Engineering
(Accredited by NBA for 3 years from AY 2024-25)
Tocheckrootsare
i) Realandequal [when b2-4ac=0]
ii) Realanddistinct[Whenb2-4ac>0]
iii) Imaginary[ whenb2-4ac<0]
Output:Typeofroots
i) Realandequal
ii) Realanddistinct
iii) Imaginary
Theory:
:Theifstatement
In C programming language the else if ladder is a way of putting multiple ifs
togetherwhen multipath decisions are involved. It is a one of the types of decision
makingandbranching statements. A multipath decision is a chain of if’s in which the
statementassociatedwitheach else isanif.Thegeneralformofelseifladderisas follows-
if(condition 1)
{
statement1;
}
elseif(condition2)
{
statement2;
}
elseif (condition n)
{
1-13
Skill Based Lab: C Programming (ETLOR0VS101) A.Y. 2024-25
U
G Program in Electronics and Telecommunication Engineering
(Accredited by NBA for 3 years from AY 2024-25)
}
else
{
}
statement-n;
defaultstatement; statement-x;
Thisconstructisknownastheelseifladder.Theconditionsareevaluatedfromthetop of the
ladder to downwards. As soon as a true condition is found, the
statementassociatedwithitisexecutedandthecontrolistransferredtothestatement-
x(skippingtherestoftheladder).Whenallthenconditionsbecomefalse,thenthefinalelseco
ntainingthedefault statementwillbeexecuted.
Algorithm:
Step1:Start
Step2:read co-efficient ofquadraticequationa,b,c
Step3:if‘a’iszeroprint“notaquadraticequation”.
Calculateandprinttheanswerusingformula=-c/
bElse
Calculate d using the formula
b2-4acIfd=0
Print“realandequalroots”
Print root= –
b/2aifd>0
Print“realanddistinctroots”
1-14
Skill Based Lab: C Programming (ETLOR0VS101) A.Y. 2024-25
U
G Program in Electronics and Telecommunication Engineering
(Accredited by NBA for 3 years from AY 2024-25)
Calculaterootsusingformulae
(-b+sqrt(d)/2a)and(-b-sqrt(d)/
2a)Printboththeroots.
Ifd<0
Print“imaginaryroots”Cal
culaterealpartsas–b/2a
Calculateimaginarypartassqrt(-d)/2a
Printtherootsusingrealandimaginaryparts.
Step4:Stop
Flow chart:
1-15
Skill Based Lab: C Programming (ETLOR0VS101) A.Y. 2024-25
U
G Program in Electronics and Telecommunication Engineering
(Accredited by NBA for 3 years from AY 2024-25)
Program and
output:
1-16
Skill Based Lab: C Programming (ETLOR0VS101) A.Y. 2024-25
U
G Program in Electronics and Telecommunication Engineering
(Accredited by NBA for 3 years from AY 2024-25)
1-17
Skill Based Lab: C Programming (ETLOR0VS101) A.Y. 2024-25
U
G Program in Electronics and Telecommunication Engineering
(Accredited by NBA for 3 years from AY 2024-25)
1.6 Conclusion:
……………………………………………………………………………………
………C language is easy to read and understand
……………………………………………………………………………………
……………………………………………………………………………………
……………………………………………………………………………………
………………………
1.7 Questions:
Implement all logical operators in C program
………The 3 logical operators are Logical AND (&&) Logical OR (||) and
logical NOT (!)
……………………………………………………………………………………
……………………………………………………………………………………
……………………………………………………………………………………
……………………………………………………………………………………
………………………
……………………………………………………………………………………
……………………………………………………………………………………
……………………………………………………………………………………
……………………………………………………………………………………
………………………………
1-18
Skill Based Lab: C Programming (ETLOR0VS101) A.Y. 2024-25
U
G Program in Electronics and Telecommunication Engineering
(Accredited by NBA for 3 years from AY 2024-25)
……………………………………………………………………………………
……………………………………………………………………………………
……………………………………………………………………………………
……………………………………………………………………………………
………………………………
……………………………………………………………………………………
……………………………………………………………………………………
1-19
Skill Based Lab: C Programming (ETLOR0VS101) A.Y. 2024-25