SESSION NUMBER: 02
Session Outcome: At the end of this session on ALGORITHMS AND FLOWCHARTS, Students will be able:
1. To design algorithms for conditional problems.
2. To design flowcharts for iterativeproblems.
Ex 1. Write an algorithmanddraw flowchart to findwhetherpersoniseligibleforvote ornot(above
18 eligible).
Solution:
1. Identifythe inputstobe giventovoteEligibilityfunction ( here itisage )
2. Identifythe outputtobe printedonmonitor (here itis Eligible ornotEligible)
Algorithmfor main() function:
Step1: Start
Step2: age:=20
Step3: Call voteEligibility(age) function
Step4: Stop
Algorithmfor voteEligibility(intage) function:
Step1: Start
Step2: if( age >= 18) then
print“Eligible to Vote”
Step3: else
print“Not eligible toVote”
Step4: returnto main()
Flowchart:
Code:
#include<stdio.h> // headerfile
voidVoteEligibility(int); // functionprototype
intmain()
{
intage;
age = 20;
voteEligibility(age); // functioncall
return0;
}
voidvoteEligibility(intage) //functionbodybeginshere
{
if(age>=18)
{
printf("Eligibletovote");
}
else
{
printf("NotEligible tovote");
}
return;
}
Here in the above
program,we have to
write #include<stdio.h>
stdioisthe short formfor standardinputand output
stdio.hisan headerfile.
A headerfile containsmanyfunctions.
Some of the available functionsinheaderfilestdio.hare printf,scanf,etc.
// are comments. Commentswill enable youtounderstandthe code.Whateveryouwrite after//are not
executedbythe compiler.
intmain() Executionof Cprogram starts frommain() andit isan inbuiltmethod
voidVoteEligibility(int); Functionprototype indicatesthe returntype of function, functionname,inputparameters
and theirdatatypes
intage; a memoryisallocatedandname giventomemorylocationisage. Here age iscalleda
variable.A namedmemorylocationiscalledavariable
intis a data type indicatingthatwe can store integervaluesinage memorylocation.
The range of valuesthatcan be storedinan integervariable,age is -32767 to 32768 in a 32-
bitcomputer
age=20; will putvalue 20 inthe age memorylocation.
voteEligibility(age); main() callsthe function voteEligibility(age); main() pausesexecution
CPU starts executingVoteEligibility(age);function
voidvoteEligibility(intage)
{
The userdefinedfunctionvoteEligibility(intage) isgivenfunctionalityhere.Itiscalleduser
definedfunctionas thisfunctioniswrittenbyprogrammer.Butprintf()functioniscalled
inbuiltfunctionasthe code forprintf() iswrittenbysome one else andwe are usingit.
{ indicate the functionbeginshere,inotherwords,the functionblockbeginsher
if(age>=18)
{
If is an conditional statementasitchecksa conditionage >= 18. if the conditionistrue
then{ indicatesbeginningof if block.all the statementsinside{ and} of if blockwill be
executed
Alsohere age referstothe valuespresentinage memorylocation.
If is a keywordasit has some special meaning
printf("Eligibletovote");
}
printf will printthe message “Eligible tovote”onthe monitor.
} indicatesendof if block
printf isan inbuiltfunctioninstdio.h
else
{
else mustbe writtenafterthe if blockandwill be executedif the conditionage>=18
becomesfalse.i.e,the value insideage memorylocationisnotgreaterthan18 thenelse
blockwill execute
else isalsoa keyword
printf("Eligibletovote");
}
All the statementsinside else blockwill be executedif the conditionage>=18 isfalse
return will enable youtocome outof the voteEligibilityfunctionandreturntothe main() where
thisfunctionwascalled
} End of voteEligibility functionblock
return0; main() functionreturns0to compilerindicatingthe programexecutedsuccessfully
} End of main() function
Example 2:
Use an algorithmtoprintall natural numbersfrom1 to N to make the studentsunderstanddesignof
algorithmsandflowchartfor iterative problems.
Solution:
1. Identifythe inputstobe giventoPrintmethod( here itis n)
2. Identifythe outputtobe printedonmonitor(all natural numbersfrom1 to n)
Algorithmfor main() function:
Step1: Start
Step2: n:=20
Step3: Call Print(n) function
Step4: Stop
Algorithmfor Print(n) function:
Step1: Start
Step2: i := 1
Step3: if ( i <= n) then
Step3.1 : printi
Step3.2: i := i+1
Step3.3: go to step3
Step4: returnto main()
Flowchart:
main() print(intn)
Here the main() methodpausesandthe CPU control goesto print(intn)
Here the CPU control goesback to main()
Start
n := 20
Print(n)
Stop
Print(intn)
i <= n
print i
i := 1
i := i+1
True
False
Code:
#include<stdio.h>
voidPrint(int);
intmain()
{
intn=20;
Print(n);
return0;
}
voidPrint(intn)
{
inti = 1;
while(i<=n)
{
printf("%d",i);
i=i+1;
}
}
Practice SessionProblems:
Q1) Write an algorithmanddraw flowchart tofind biggestof giventwonumber.
Algorithmfor main() function:
Step1: Start
Step2: n1:=10
Step3: n2 := 20
Step4: biggest_number:=calculateBig(n1,n2)
Step5: printbiggest_number
Step6: Stop
Algorithmfor calculateBig(intn1, int n2) function:
Step1: Start
Step2: if( n1 >= n2) then
big:= n1
Step3: else
big:= n2
Step4: returnbig
FlowChart:
Q2) Write an algorithmanddraw flowchart tofindthe biggestof given3 distinctnumbers.
Algorithmfor main() function:
Step1: Start
Step2 : n1 := 10
Step3 : n2 := 20
Step4 : n3 := 30
Step5: ans=calculateBig(n1,n2,n3)
Step6: printans
Step7: Stop
Algorithmfor calculateBig(intn1, int n2, int n3) function:
Step1: Start
Step2 : if n1 > n2 then
Step2.1: if n1 > n3 then
big:= n1
Step2.2: else
big:= n3
Step3: else
Step3.1 if n2 > n3 then
big:= n2
Step3.2 else
big:= n3
Step4: returnbig
Flowchart:
Q3) Draw flowchart & write algorithm to print the discount applicable to the given order quantity.
Vishnu Limited calculates discounts allowed to customers on the following basis
Orderquantity Normal
discount
1-99 5%
100-199 7%
200-499 9%
500 andabove 10%
FlowChart:
To get to knowaboutmemoryallocationtovariables,Considerthe below example.
Q) Write an algorithmtofindsumof twonumbersusingfunctions.
Solution:
1. Identifythe inputstobe giventoSum() method
2. Identifythe outputtobe printedonmonitor
1. Executionof C program startsfrom main()
2. main() callssum() andgives3,4 valuestosum()
3. sum() methodcomputessumof 3 and 4 and returnsto main().
4. main() will printthe outputtothe monitor
Relational Operators in C

Relational Operators in C

  • 1.
    SESSION NUMBER: 02 SessionOutcome: At the end of this session on ALGORITHMS AND FLOWCHARTS, Students will be able: 1. To design algorithms for conditional problems. 2. To design flowcharts for iterativeproblems. Ex 1. Write an algorithmanddraw flowchart to findwhetherpersoniseligibleforvote ornot(above 18 eligible). Solution: 1. Identifythe inputstobe giventovoteEligibilityfunction ( here itisage ) 2. Identifythe outputtobe printedonmonitor (here itis Eligible ornotEligible) Algorithmfor main() function: Step1: Start Step2: age:=20 Step3: Call voteEligibility(age) function Step4: Stop Algorithmfor voteEligibility(intage) function: Step1: Start Step2: if( age >= 18) then print“Eligible to Vote” Step3: else print“Not eligible toVote” Step4: returnto main()
  • 2.
    Flowchart: Code: #include<stdio.h> // headerfile voidVoteEligibility(int);// functionprototype intmain() { intage; age = 20; voteEligibility(age); // functioncall return0; } voidvoteEligibility(intage) //functionbodybeginshere { if(age>=18) { printf("Eligibletovote"); } else { printf("NotEligible tovote"); } return; } Here in the above program,we have to write #include<stdio.h> stdioisthe short formfor standardinputand output stdio.hisan headerfile. A headerfile containsmanyfunctions. Some of the available functionsinheaderfilestdio.hare printf,scanf,etc. // are comments. Commentswill enable youtounderstandthe code.Whateveryouwrite after//are not
  • 3.
    executedbythe compiler. intmain() ExecutionofCprogram starts frommain() andit isan inbuiltmethod voidVoteEligibility(int); Functionprototype indicatesthe returntype of function, functionname,inputparameters and theirdatatypes intage; a memoryisallocatedandname giventomemorylocationisage. Here age iscalleda variable.A namedmemorylocationiscalledavariable intis a data type indicatingthatwe can store integervaluesinage memorylocation. The range of valuesthatcan be storedinan integervariable,age is -32767 to 32768 in a 32- bitcomputer age=20; will putvalue 20 inthe age memorylocation. voteEligibility(age); main() callsthe function voteEligibility(age); main() pausesexecution CPU starts executingVoteEligibility(age);function voidvoteEligibility(intage) { The userdefinedfunctionvoteEligibility(intage) isgivenfunctionalityhere.Itiscalleduser definedfunctionas thisfunctioniswrittenbyprogrammer.Butprintf()functioniscalled inbuiltfunctionasthe code forprintf() iswrittenbysome one else andwe are usingit. { indicate the functionbeginshere,inotherwords,the functionblockbeginsher if(age>=18) { If is an conditional statementasitchecksa conditionage >= 18. if the conditionistrue then{ indicatesbeginningof if block.all the statementsinside{ and} of if blockwill be executed Alsohere age referstothe valuespresentinage memorylocation. If is a keywordasit has some special meaning printf("Eligibletovote"); } printf will printthe message “Eligible tovote”onthe monitor. } indicatesendof if block printf isan inbuiltfunctioninstdio.h else { else mustbe writtenafterthe if blockandwill be executedif the conditionage>=18 becomesfalse.i.e,the value insideage memorylocationisnotgreaterthan18 thenelse blockwill execute else isalsoa keyword
  • 4.
    printf("Eligibletovote"); } All the statementsinsideelse blockwill be executedif the conditionage>=18 isfalse return will enable youtocome outof the voteEligibilityfunctionandreturntothe main() where thisfunctionwascalled } End of voteEligibility functionblock return0; main() functionreturns0to compilerindicatingthe programexecutedsuccessfully } End of main() function
  • 5.
    Example 2: Use analgorithmtoprintall natural numbersfrom1 to N to make the studentsunderstanddesignof algorithmsandflowchartfor iterative problems. Solution: 1. Identifythe inputstobe giventoPrintmethod( here itis n) 2. Identifythe outputtobe printedonmonitor(all natural numbersfrom1 to n) Algorithmfor main() function: Step1: Start Step2: n:=20 Step3: Call Print(n) function Step4: Stop Algorithmfor Print(n) function: Step1: Start Step2: i := 1 Step3: if ( i <= n) then Step3.1 : printi Step3.2: i := i+1 Step3.3: go to step3 Step4: returnto main() Flowchart: main() print(intn) Here the main() methodpausesandthe CPU control goesto print(intn) Here the CPU control goesback to main() Start n := 20 Print(n) Stop Print(intn) i <= n print i i := 1 i := i+1 True False
  • 6.
  • 7.
    Practice SessionProblems: Q1) Writean algorithmanddraw flowchart tofind biggestof giventwonumber. Algorithmfor main() function: Step1: Start Step2: n1:=10 Step3: n2 := 20 Step4: biggest_number:=calculateBig(n1,n2) Step5: printbiggest_number Step6: Stop Algorithmfor calculateBig(intn1, int n2) function: Step1: Start Step2: if( n1 >= n2) then big:= n1 Step3: else big:= n2 Step4: returnbig FlowChart:
  • 8.
    Q2) Write analgorithmanddraw flowchart tofindthe biggestof given3 distinctnumbers. Algorithmfor main() function: Step1: Start Step2 : n1 := 10 Step3 : n2 := 20 Step4 : n3 := 30 Step5: ans=calculateBig(n1,n2,n3) Step6: printans Step7: Stop Algorithmfor calculateBig(intn1, int n2, int n3) function: Step1: Start Step2 : if n1 > n2 then Step2.1: if n1 > n3 then big:= n1 Step2.2: else big:= n3 Step3: else Step3.1 if n2 > n3 then big:= n2 Step3.2 else big:= n3 Step4: returnbig Flowchart:
  • 9.
    Q3) Draw flowchart& write algorithm to print the discount applicable to the given order quantity. Vishnu Limited calculates discounts allowed to customers on the following basis Orderquantity Normal discount 1-99 5% 100-199 7% 200-499 9% 500 andabove 10% FlowChart:
  • 10.
    To get toknowaboutmemoryallocationtovariables,Considerthe below example. Q) Write an algorithmtofindsumof twonumbersusingfunctions. Solution: 1. Identifythe inputstobe giventoSum() method 2. Identifythe outputtobe printedonmonitor 1. Executionof C program startsfrom main() 2. main() callssum() andgives3,4 valuestosum() 3. sum() methodcomputessumof 3 and 4 and returnsto main(). 4. main() will printthe outputtothe monitor