algorithm: step by step procedure of solving a particular problem. pseudo code: artificial informal...

8
Algorithm: Step by step procedure of solving a particular problem. Pseudo code: Artificial informal language used to develop algorithms. Flow chart: Graphical representation of an algorithm. Algorithm to find whether a number even or odd: Step1: Begin Step1: START Step2: Take a number Step2: Read num Step3: if the number is divisible by2 then Step3: if(num%2=0) then print that number is even print num is even otherwise print that number is odd otherwise print num is odd Step4: End Step4: STOP (Algorithm in natural language) (Algorithm by using pseudo code) #include<stdio.h> #include<conio.h> main() { int num; printf(“Enter any number”); scanf(“%d”,&num); if(num%2==0) printf(“%d is even”,num); else printf(%d is odd”,num); } start read num print num is even stop If num If num %2=0 %2=0 print num is odd Flow Flow chart: chart: Yes Yes No No

Upload: antony-malone

Post on 16-Dec-2015

216 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Algorithm: Step by step procedure of solving a particular problem. Pseudo code: Artificial informal language used to develop algorithms. Flow chart: Graphical

Algorithm: Step by step procedure of solving a particular problem.Pseudo code: Artificial informal language used to develop algorithms.Flow chart: Graphical representation of an algorithm.Algorithm to find whether a number even or odd:Step1: Begin Step1: STARTStep2: Take a number Step2: Read numStep3: if the number is divisible by2 then Step3: if(num%2=0) then

print that number is even print num is even otherwise print that number is odd otherwise

print num is oddStep4: End Step4: STOP(Algorithm in natural language) (Algorithm by using pseudo code)

#include<stdio.h>#include<conio.h>main(){ int num; printf(“Enter any number”); scanf(“%d”,&num); if(num%2==0) printf(“%d is even”,num); else printf(%d is odd”,num); }(Program in C language)

start

read num

print num is even

stop

If numIf num%2=0%2=0

print num is odd

Flow chart:Flow chart:

YesYes NoNo

Page 2: Algorithm: Step by step procedure of solving a particular problem. Pseudo code: Artificial informal language used to develop algorithms. Flow chart: Graphical

Flow chart symbolsFlow chart symbolsOval Terminal

Parallegram Input/output

Rectangle Process

Document Hard copy

Diamond Decision

Circle Connector

Double sided Rectangle Sub program

Hexagon Iteration

Trapezoid Manual Operation

Cylinder Magnetic Disk Storage

Page 3: Algorithm: Step by step procedure of solving a particular problem. Pseudo code: Artificial informal language used to develop algorithms. Flow chart: Graphical

Test Test ExpressionExpression

??

EntryEntry

FalseFalse

Next statementNext statement

True Statement-blockTrue Statement-block

TrueTrue

simple if:simple if:

Test Test ExpressionExpression

??

EntryEntry

True-blockTrue-blockStatementsStatements

FalseFalse

False-blockFalse-blockStatementsStatements

TrueTrue

Next statementNext statement

if-else:if-else:

/* check a citizen is eligible for voting *//* check a citizen is eligible for voting */#include<stdio.h>#include<stdio.h>int main()int main(){{ int age;int age; printf(“Enter the age : ”);printf(“Enter the age : ”); scanf(“%d”,&age);scanf(“%d”,&age); if(age >= 18)if(age >= 18) printf(“Eligible for voting…”);printf(“Eligible for voting…”); getch();getch();}}

/* print a number is even or odd *//* print a number is even or odd */#include<stdio.h>#include<stdio.h>int main()int main(){{ int number;int number; printf(“Enter a number : “);printf(“Enter a number : “); scanf(“%d”, &number);scanf(“%d”, &number); if((number %2) == 0)if((number %2) == 0) printf(“%d is even number.”,number);printf(“%d is even number.”,number); elseelse printf(“%d is odd number.”,number);printf(“%d is odd number.”,number);}}

Page 4: Algorithm: Step by step procedure of solving a particular problem. Pseudo code: Artificial informal language used to develop algorithms. Flow chart: Graphical

Test Test condition1condition1

??

EntryEntry

Statement-3Statement-3

TrueTrue

Test Test condition2condition2

??

FalseFalse

Statement-2Statement-2 Statement-1Statement-1

TrueTrue

FalseFalse

Next statementNext statement

nested if…else:nested if…else: /* check whether a year is leap year or not *//* check whether a year is leap year or not */#include<stdio.h>#include<stdio.h>int main() {int main() { int year;int year; printf("Enter the year ?");printf("Enter the year ?"); scanf("%d",&year);scanf("%d",&year); if((year %100) == 0)if((year %100) == 0) {{ if((year % 400) == 0)if((year % 400) == 0) printf("%d is leap year.",year);printf("%d is leap year.",year); elseelse printf("%d is not leap year.",year);printf("%d is not leap year.",year); } else {} else { if((year % 4) == 0)if((year % 4) == 0) printf("%d is leap year.",year);printf("%d is leap year.",year); elseelse printf("%d is not leap year.",year);printf("%d is not leap year.",year); }} getch();getch();}}

Page 5: Algorithm: Step by step procedure of solving a particular problem. Pseudo code: Artificial informal language used to develop algorithms. Flow chart: Graphical

if…else…if :if…else…if :

EntryEntry

Test Test condition1condition1

??

TrueTrueStatement-1Statement-1

TrueTrueStatement-2Statement-2

Test Test condition2condition2

??

FalseFalse

TrueTrueTest Test conditionNconditionN

??

FalseFalse

Statement-NStatement-N

Next statementNext statement

/* program to print the grade of student *//* program to print the grade of student */#include<stdio.h>#include<stdio.h>int main() {int main() { int marks;int marks; printf("Enter marks ? ");printf("Enter marks ? "); scanf("%d", &marks);scanf("%d", &marks); if(marks >= 75)if(marks >= 75) printf("Distinction");printf("Distinction"); else if(marks >= 60)else if(marks >= 60) printf("First class");printf("First class"); else if(marks >= 50)else if(marks >= 50) printf("Second class");printf("Second class"); else if(marks >= 35)else if(marks >= 35) printf("Third class");printf("Third class"); elseelse printf("Failed");printf("Failed");}}

Page 6: Algorithm: Step by step procedure of solving a particular problem. Pseudo code: Artificial informal language used to develop algorithms. Flow chart: Graphical

switch statement :switch statement :

EntryEntry

switch switch expressionexpression

??

Next statementNext statement

/* program to simulate a simple calculator *//* program to simulate a simple calculator */#include<stdio.h>#include<stdio.h>int main() {int main() { float a,b;float a,b; char opr;char opr; printf("Enter number1 operator number2 : ");printf("Enter number1 operator number2 : "); scanf("%f %c %f",&a,&opr,&b); scanf("%f %c %f",&a,&opr,&b); switch(opr)switch(opr) {{ case '+':case '+': printf("Sum : %f",(a + b));printf("Sum : %f",(a + b)); break;break; case '-':case '-': printf("Difference : %f",(a - b));printf("Difference : %f",(a - b)); break;break; case '*':case '*': printf("Product : %f",(a * b));printf("Product : %f",(a * b)); break;break; case '/':case '/': printf("Quotient : %f",(a / b));printf("Quotient : %f",(a / b)); break;break; default:default: printf("Invalid Operation!");printf("Invalid Operation!"); }}}}

associateassociatestatementstatement

associateassociatestatementstatement

associateassociatestatementstatement

associateassociatestatementstatement

value1value1 value2value2 valueNvalueN defaultdefault

ExitExit

…………......

Page 7: Algorithm: Step by step procedure of solving a particular problem. Pseudo code: Artificial informal language used to develop algorithms. Flow chart: Graphical

TestCondition

?

Body of The loop

False

truetrue

while – (Entry controlled )while – (Entry controlled )EntryEntry

Loop StatementsLoop Statements

Following Statement

TestCondition

?

Body of The loop

False

TrueTrue

EntryEntry

Following Statement

do-while – (Exit controlled )do-while – (Exit controlled )

/* sum of 1 to 10 numbers *//* sum of 1 to 10 numbers */#include<stdio.h>#include<stdio.h>int main() {int main() { int i = 1,sum = 0;int i = 1,sum = 0; while(i<=10){while(i<=10){ sum = sum + i;sum = sum + i; i = i + 1;i = i + 1; } } printf(“Total : %d “,sum);printf(“Total : %d “,sum);} }

/* average of 5 numbers *//* average of 5 numbers */#include<stdio.h>#include<stdio.h>int main() {int main() { int count = 1;int count = 1; float x, sum = 0;float x, sum = 0; do {do { printf(“x = “);printf(“x = “); scanf(“%f”,&x);scanf(“%f”,&x); sum += x;sum += x; ++ count;++ count; } while(count <= 5);} while(count <= 5); printf(“Average = %f “, (sum/5))printf(“Average = %f “, (sum/5))} }

Page 8: Algorithm: Step by step procedure of solving a particular problem. Pseudo code: Artificial informal language used to develop algorithms. Flow chart: Graphical

for -- for -- StatementStatement

Initialization StatementIncrement Statement

TestCondition

?

Body of The loop

EntryEntry

TrueTrue

Following Statement

False

/* check whether a number is prime or not *//* check whether a number is prime or not */#include<stdio.h>#include<stdio.h>int main() {int main() { int n,i,factors = 0;int n,i,factors = 0; printf("Enter a number : ");printf("Enter a number : "); scanf("%d",&n);scanf("%d",&n); for(i = 1; i <= n; i++) {for(i = 1; i <= n; i++) { if((n % i)==0) ++factors;if((n % i)==0) ++factors; }} if (factors == 2)if (factors == 2) printf("%d is prime number.",n);printf("%d is prime number.",n); elseelse printf("%d is not prime number.",n);printf("%d is not prime number.",n);}}