0% found this document useful (0 votes)
3 views22 pages

Exp 1

Uploaded by

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

Exp 1

Uploaded by

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

U

G Program in Electronics and Communication


(Advanced Communication Technology)

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

1.1 Aim: Implement c operators, if else, if else ladder


Problem statement:
(a) Write a C program to find the sum and average of three numbers
(b) Write a program to accept three numbers and display largest of three
using a nested if-else statement
(c) Write a program to find all the roots of a quadratic equation using if-else
ladder.

1.2 Course Outcome: CO 1, CO 2

1.3 Learning Objectives: Demonstrate C operators, if else and if else ladder in c


program
1.4 Requirement: Computer, Suitable IDE
1.5 Related Theory:

Skill Based Lab: C Programming (ACLOR0VS101) A.Y. 2024-25


1-1
U
G Program in Electronics and Communication
(Advanced Communication Technology)

An operator is a symbol that specifies the mathematical, logical, or relational operator


to be performed. C language supports following type of operators.

 Arithmetic Operators
 Logical(or Relational) Operators
 Bit-wise Operators
 Assignment Operators
 MiscOperators

ArithmeticOperators:

TherearefollowingarithmeticoperatorssupportedbyClanguage:Assu

mevariableAholds 10andvariableB holds20then:

Operator Description Example


+ Addstwooperands A+B willgive30
- Subtracts second operand from A-Bwill give-10
thefirst
* Multiplyboth operands A* B willgive200
/ Dividenumeratorbydenominator B/ Awillgive2
% ModulusOperatorandremainderofaf B%Awillgive0
ter an integerdivision
++ Incrementoperator,increasesi A++willgive11
ntegervaluebyone
-- Decrementoperator,decreasesi A--willgive9
ntegervaluebyone

Logical(orRelational)Operators:

There are following logical operators supported by C

languageAssumevariableAholds 10andvariableBholds20 then:

Operator Description Example

Skill Based Lab: C Programming (ACLOR0VS101) A.Y. 2024-25


1-2
U
G Program in Electronics and Communication
(Advanced Communication Technology)

== Checks if the value of two (A==B)isnot true.


operandsis equal or not, if yes then
conditionbecomestrue.
!= Checksifthevalueoftwooperandsis (A!=B)istrue.
equal or not, if values are
notequalthenconditionbecomestrue.

> Checksifthevalueofleftoperandis (A>B)isnottrue.


greater than the value of
rightoperand,ifyesthenconditionbe
comestrue.
< Checksifthevalueofleftoperandis (A<B)is true.
less than the value of
rightoperand,ifyesthenconditionbe
comestrue.
>= Checks if the value of left (A>=B)isnot true.
operandisgreaterthanorequaltothev
alueof right operand, if yes
thenconditionbecomes true.
<= Checks if the value of left (A<=B)istrue.
operandis less than or equal to the
value ofright operand, if yes then
conditionbecomestrue.
&& Called Logical AND operator. (A&&B)is true.
Ifboththeoperandsarenonzerothenc
onditionbecomes true.
|| Called Logical OR Operator. If (A||B)is true.
anyofthetwooperandsisnonzerothen
conditionbecomes true.
! CalledLogicalNOTOperator.Useto !(A &&B)isfalse.
reverses the logical state of
itsoperand. If a condition is true
thenLogical NOT operator will
makefalse.

BitwiseOperators:

Skill Based Lab: C Programming (ACLOR0VS101) A.Y. 2024-25


1-3
U
G Program in Electronics and Communication
(Advanced Communication Technology)

Bitwiseoperatorworksonbitsandperformsbitbybitoperation.

Assume if A = 60; and B = 13; Now in binary format they will be as

follows:A=0011 1100

B=0000 1101

A&B=00001100

A|B=00111101

A^B=00110001

~A=1100 0011

TherearefollowingBitwiseoperatorssupportedbyClanguage

Operator Description Example


& Binary AND Operator copies a (A & B) will give 12 which is
bitto the result if it exists in 00001100
bothoperands.
| Binary OR Operator copies a bit (A | B) will give 61 which is
ifitexists in eatheroperand. 00111101
^ Binary XOR Operator copies the (A^B)will give49which
bitif it is set in one operand but is00110001
notboth.
~ BinaryOnesComplementOperatoris (~A ) will give -60 which is
unary and has the 11000011
efectof'flipping'bits.
<< Binary Left Shift Operator. The A<<2will give240which is1111
leftoperands value is moved left by 0000
thenumberofbitsspecifiedbytheright
operand.

Skill Based Lab: C Programming (ACLOR0VS101) A.Y. 2024-25


1-4
U
G Program in Electronics and Communication
(Advanced Communication Technology)

>> Binary Right Shift Operator. A>>2will give15which is0000


Theleftoperands value is moved 1111
rightby the number of bits
specified bytheright operand.

Skill Based Lab: C Programming (ACLOR0VS101) A.Y. 2024-25


1-5
U
G Program in Electronics and Telecommunication Engineering
(Accredited by NBA for 3 years from AY 2024-25)

AssignmentOperators:
TherearefollowingassignmentoperatorssupportedbyClanguage:

Operator Description Example


= Simple assignment C=A+B willassignedvalueofA+Binto
operator,Assigns values from C
right
sideoperandstoleftsideoperand
+= AddANDassignmentoperator,It C+=Ais equivalenttoC=C+A
adds right operand to the
leftoperand and assign the
result toleftoperand
-= Subtract AND C-=Ais equivalentto C=C-A
assignmentoperator, It
subtracts
rightoperandfromtheleftopera
ndand assign the result to
leftoperand
*= Multiply AND C*=Ais equivalentto C =C *A
assignmentoperator, It
multiplies
rightoperandwiththeleftopera
ndand assign the result to
leftoperand
/= Divide AND C/=Ais equivalent toC =C / A
assignmentoperator,Itdividesle
ftoperandwith the right
operand
andassigntheresulttoleftoperan
d
%= Modulus AND C%=Ais equivalenttoC =C %A
assignmentoperator, It takes
modulususing two operands
and assigntheresulttoleft
operand
<<= LeftshiftANDassignmento C<<=2 issameasC= C<<2
perator

Skill Based Lab: C Programming (ACLOR0VS101) A.Y. 2024-25


1-6
U
G Program in Electronics and Telecommunication Engineering
(Accredited by NBA for 3 years from AY 2024-25)

>>= Right shift AND C>>=2 issameasC= C>>2


assignmentoperator
&= Bitwise AND C&=2is sameas C=C&2
assignmentoperator
^= bitwise exclusive OR C^=2 issameas C =C ^ 2
andassignmentoperator
|= bitwise inclusive OR C|=2is same asC=C|2
andassignmentoperator

Skill Based Lab: C Programming (ACLOR0VS101) A.Y. 2024-25


1-7
U
G Program in Electronics and Telecommunication Engineering
(Accredited by NBA for 3 years from AY 2024-25)

MiscOperators

TherearefewotheroperatorssupportedbyCLanguage.

Operator Description Example


sizeof() Returnsthesizeofanvariable. sizeof(a),whereaisinterger,willreturn4.

& Returnstheaddressofanva &a; will give actaul address of


riable. thevariable.
* Pointertoavariable. *a;willpointertoavariable.
?: ConditionalExpression If Condition is true ? Then value
X :OtherwisevalueY

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:

Operator precedence determines the grouping of terms in an expression. This


affectshow an expression is evaluated. Certain operators have higher precedence than
others;forexample,themultiplicationoperatorhashigherprecedencethantheadditionopera

Skill Based Lab: C Programming (ETLOR0VS101) A.Y. 2024-25 1-6


U
G Program in Electronics and Telecommunication Engineering
(Accredited by NBA for 3 years from AY 2024-25)

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.

Category Operator Associativity


Postfix ()[]->. ++-- Lefttoright
Unary +-!~++--(type)*&sizeof Righttoleft
Multiplicative */ % Lefttoright
Additive +- Lefttoright
Shift <<>> Lefttoright
Relational <<= >>= Lefttoright
Equality ==!= Lefttoright
BitwiseAND & Lefttoright
BitwiseXOR ^ Lefttoright
BitwiseOR | Lefttoright
LogicalAND && Lefttoright
LogicalOR || Lefttoright
Conditional ?: Righttoleft
Assignment =+= -=*=/=%= >>= <<=&=^= |= Righttoleft
Comma , Lefttoright

Problem statement :(a) Write a C program to find the sum and average of
three numbers.

Skill Based Lab: C Programming (ETLOR0VS101) A.Y. 2024-25 1-7


U
G Program in Electronics and Telecommunication Engineering
(Accredited by NBA for 3 years from AY 2024-25)

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:

Skill Based Lab: C Programming (ETLOR0VS101) A.Y. 2024-25 1-8


U
G Program in Electronics and Telecommunication Engineering
(Accredited by NBA for 3 years from AY 2024-25)

Program and
output :

Skill Based Lab: C Programming (ETLOR0VS101) A.Y. 2024-25 1-9


U
G Program in Electronics and Telecommunication Engineering
(Accredited by NBA for 3 years from AY 2024-25)

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)

Problem statement: (c) Writeaprogramtofindalltherootsofaquadraticequationusingif-


elseladder.
ProblemDefinition:

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

You might also like