Decision Making It is used to change the order of the program based on condition. Categories: –...

Post on 20-Jan-2016

213 views 0 download

Transcript of Decision Making It is used to change the order of the program based on condition. Categories: –...

Decision Making

• It is used to change the order of the program based on condition.

• Categories:– Sequential structure– Selection structure– Iteration structure– Encapsulation structure

Decision Making (cont)• Sequential structure– In which instructions are executed in sequence.

• Selection structure– In which instruction are executed based on the

result of some condition.• Iteration structure– In which instruction are executed repeatedly.

• Encapsulation structure– In which some compound structure are used.

SELECTION STRUCTURE

• It allows the program to make a choice from alternative paths.

• C provide the following selection structures– IF statement– IF … ELSE statement– Nested IF … ELSE statement– IF … ELSE ladder

IF Statement

SyntaxIF (condition is true)

{

Statements;

}

If condition

False

True

Statements

Example#include<stdio.h>#include <conio.h>void main ( ){int a;clrscr( );printf("\nEnter the number:");scanf("%d",&a);

if(a>10){

printf(" \n a is greater than 10");}

getch( );}

Output

Enter the number: 12a is greater than 10

IF…ELSE StatementSyntaxIF (condition) {

True statements;}ELSE{

False statements;}

If Condition

True False

True statements

False statements

#include<stdio.h>#include <conio.h>void main ( ){int a;clrscr( );printf("\nEnter the number:");scanf("%d",&a);

if(a>10){

printf(" \n a is greater than 10");}

else{

printf(" \n a is less than 10");}

getch( );}

NESTED IF… ELSE

If Condition

2

True False

True statements

False statements

IfCondition

1False

StatementsTrue

NESTED IF… ELSESyntaxIF (condition1) {

IF (condition2) {

True statements;}ELSE{

False statements;}

}ELSE{

False statements;}

IF…ELSE LADDER

Condition1

Statements

Condition2

Statements

Condition3

Statements Statements

TRUE

TRUE

TRUE FALSE

FALSE

FALSE

IF…ELSE LADDERSyntaxIF (condition1) {statements;}

else if (condition2) {statements;}

else if (condition3){statements;}

else{statements;}

Example#include<stdio.h>#include<conio.h>void main(){ int m1,m2,m3; float avg; printf("\nEnter the marks:"); scanf("%d%d%d",&m1,&m2,&m3); avg=(m1+m2+m3)/3; printf("\n The average is:%f",avg); printf("\n The Grade is:"); if(avg>=60) {

printf("First class"); }

else if(avg>=50) {

printf("Second class"); } else if(avg>=35) {

printf("Thrid class"); } else {

printf("Fail"); }

getch();}

Output

Enter the marks:657570The average is:70.000000The Grade is: First class

Looping structure

• It is used to execute some instructions several time based on some condition.– WHILE – Do…WHILE – For

WHILE Loop

Syntax.

WHILE (condition){

.Body of the loop;

. }

Body of The loop

conditionFalse

True

Example#include<stdio.h>#include<conio.h>void main(){ int i=1,fact=1,n; printf("\nEnter the Number:"); scanf("%d",&n); while(i<=n) {

fact =fact *i; i++; // i=i+1

} printf("\n The value of %d! is:%d",n,fact); getch();}

Output

Enter the Number:3The value of 3! is: 6

DO…WHILE Loop

Syntaxdo

{

Body of the loop

}while (condition);

Body of The loop

condition

False

True

for loop

Syntaxfor (initialization; test condition; Increment/Decrement)

{

Body of the loop

}

for loop

Initialization

condition False

Body of the loop

Inc / Decrement

Example#include<stdio.h>#include<conio.h>void main(){ int i,fact=1,n; printf("\nEnter the Number:"); scanf("%d",&n); for(i=1;i<=n;i++) {

fact =fact *i; } printf("\n The value of %d! is:%d",n,fact); getch();}

Output

Enter the Number:3The value of 3! is: 6

Nested for loop

Syntaxfor (initi; cond; Inc/Dec)

{

for (initi; cond; Inc/Dec){

Body of the loop

}

}

CASE structure

Case 1

Case 2

Defaultcase

Switch

CASE structureSyntaxswitch (expression){case constant 1:

block1;break;

case constant 2:block2;break;..

default :default block;break;

}

Example#include<stdio.h>#include<conio.h>void main(){ int i,n; printf("\nEnter the Number:"); scanf("%d",&n);

switch(n) {

case 1:{printf("\n Its in case 1");break;}

case 2:{printf("\n Its in case 2");break;}

default:{printf("\n Its in default");break;}

} getch();}

Output

Enter the Number:2Its in case 2

break Statement

• It is used to terminate the loop• When a break statement is encountered

inside a loop, then the loop is terminated.

Loops with break Statement

while(cond){

…………if(cond)break;…………

}

do{

…………if(cond)break;…………

} while(cond);

for (initi; condt; Inc/Dec){…………if(cond)break;…………

}

Continue Statement

• When a continue statement is encountered inside a loop, the control is transferred to the beginning.

Loops with continue Statement

while(cond){

…………if(cond)continue;…………

}

do{

…………if(cond)continue;…………

} while(cond);

for (initi; condt; Inc/Dec){…………if(cond)continue;…………

}

goto Statement

• When a goto statement is encountered inside a loop, the control is transferred to the beginning.

Syntax for goto Statement

label:………………………………goto label;…………

goto label; ……………………………… label:…………

getchar() Example

#include<stdio.h>#include<conio.h>#include<ctype.h>void main(){char x;printf("enter the character:");x=getchar();

if(islower(x))putchar(toupper(x));

elseputchar(tolower(x));

getch();}

Output:enter the character:ABCa

getche() Example

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

char c ;clrscr();printf("\nInput a string:");c = getche();

printf("\nstring is:");putch(c);getch();

}

Output:Input a string:kstring is:k

Getch() Example

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

char c;clrscr();printf("\nInput a string:");c = getch();

printf("\nstring is:");putch(c);getch();

}Output:Input a string:string is:h

getc Example

#include<stdio.h>#include<conio.h>#include<ctype.h>void main(){char x;printf("enter the character:");x=getc(stdin);

if(islower(x))putc(toupper(x),stdout);

elseputc(tolower(x),stdout);

getch();}

Output:enter the character:abcA

gets() Example

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

char c[80];clrscr();printf("Input a string:");gets(c);

printf("The string is:");puts(c);getch();

}

Output:Input a string:qwertyThe string is:qwerty

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

int a,b,c,n;clrscr();printf("\nEnter the value of a,b:");scanf("%d%d",&a,&b);printf("\nMENU");printf("\n1.ADD\n2.SUB\n3.MULTIPLY\n0.EXIT");printf("\nEnter the choice:");scanf("%d",&n);

switch(n){ case 1:

c=a+b;printf("\nThe result of Addition is:%d",c);break;

case 2:c=a-b;printf("\nThe result of Subtraction is:%d",c);break;

case 3:c=a*b;printf("\nThe result of Multiplication is:%d",c);break;

case 0:exit(0);break;

}getch();

}

Output

Enter the value of a,b:56MENU1.ADD2.SUB3.MULTIPLY0.EXITEnter the choice:1The result of Addition is:11

Finding Armstrong No#include<stdio.h>#include<conio.h>void main(){ int r=0,sum=0,n,a; printf("\nEnter the number:"); scanf("%d",&n); a=n;

while(n>0){

r=n%10;sum=sum+r*r*r;n=n/10;

}

if(a==sum){ printf("\nIt is an armstrong number");}else{ printf("\nIt is not an armstrong number");}

getch();}

Output

Enter the number:153It is an armstrong number

Sum of the Digits#include<stdio.h>#include<conio.h>void main(){ int r=0,sum=0,n; printf("\nEnter the no:"); scanf("%d",&n); while(n>0) { r=n%10;

sum=sum+r; n=n/10; } printf("sum of the digits is:%d",sum);}

Output

Enter the no:156sum of the digits is:12

Reverse of a number

#include<stdio.h>#include<conio.h>void main(){ int r=0,sum=0,n; printf("\nEnter the no:"); scanf("%d",&n); while(n>0)

{ r=n%10; sum=sum*10+r; n=n/10;

} printf("Reverse of the number is:%d",sum); getch();}

Output

Enter the no:567Reverse of the number is:765

Fibonacci Series

#include<stdio.h>#include<conio.h>void main(){int f=0,f1=-1,f2=1,n,i;printf("\nEnter the number:");scanf("%d",&n);

while(f<n){ f=f1+f2;

f1=f2; f2=f;

printf("\t%d",f);}

getch();}

Output

Enter the number:5 0 1 1 2 3 5

Swapping #include<stdio.h>#include <conio.h>void main ( ){int a,b,c;clrscr( );printf(" \nEnter the value of a:");scanf("%d",&a);printf(" \nEnter the value of b:");scanf("%d",&b);c=a;a=b;b=c;

printf(" \nThe value of a is:%d",a);printf(" \nThe value of b is:%d",b);getch( );}

Output:Enter the value of a:5Enter the value of b:4

The value of a is:4The value of b is:5

Swapping without using third variable

#include<stdio.h>#include <conio.h>void main ( ){int a,b;clrscr( );printf(" \nEnter the value of a:");scanf("%d",&a);printf(" \nEnter the value of b:");scanf("%d",&b);

a=a+b;b=a-b;a=a-b;printf(" \nThe value of a is:%d",a);printf(" \nThe value of b is:%d",b);getch( );}

Output:Enter the value of a:5Enter the value of b:6

The value of a is:6The value of b is:5

Quadratic Equation#include<stdio.h>#include <conio.h>#include<math.h>void main ( ){int a,b,c,d,r1,r2;clrscr( );printf(" \nEnter the value of a:");scanf("%d",&a);printf(" \nEnter the value of b:");scanf("%d",&b);printf(" \nEnter the value of c:");scanf("%d",&c);d=b*b-4*a*c;

if(d>=0){

r1=(-b+sqrt(d))/(2*a);r2=(-b-sqrt(d))/(2*a);

printf(" \nThe roots are %d,%d",r1,r2);}else{ printf(" \nThe roots are imaginary");}getch( );}

Output

Enter the value of a:4

Enter the value of b:5

Enter the value of c:6

The roots are imaginary