![Page 1: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/1.jpg)
Decision Making
• It is used to change the order of the program based on condition.
• Categories:– Sequential structure– Selection structure– Iteration structure– Encapsulation structure
![Page 2: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/2.jpg)
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.
![Page 3: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/3.jpg)
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
![Page 4: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/4.jpg)
IF Statement
SyntaxIF (condition is true)
{
Statements;
}
If condition
False
True
Statements
![Page 5: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/5.jpg)
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( );}
![Page 6: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/6.jpg)
Output
Enter the number: 12a is greater than 10
![Page 7: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/7.jpg)
IF…ELSE StatementSyntaxIF (condition) {
True statements;}ELSE{
False statements;}
If Condition
True False
True statements
False statements
![Page 8: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/8.jpg)
#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( );}
![Page 9: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/9.jpg)
NESTED IF… ELSE
If Condition
2
True False
True statements
False statements
IfCondition
1False
StatementsTrue
![Page 10: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/10.jpg)
NESTED IF… ELSESyntaxIF (condition1) {
IF (condition2) {
True statements;}ELSE{
False statements;}
}ELSE{
False statements;}
![Page 11: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/11.jpg)
IF…ELSE LADDER
Condition1
Statements
Condition2
Statements
Condition3
Statements Statements
TRUE
TRUE
TRUE FALSE
FALSE
FALSE
![Page 12: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/12.jpg)
IF…ELSE LADDERSyntaxIF (condition1) {statements;}
else if (condition2) {statements;}
else if (condition3){statements;}
else{statements;}
![Page 13: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/13.jpg)
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"); }
![Page 14: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/14.jpg)
else if(avg>=50) {
printf("Second class"); } else if(avg>=35) {
printf("Thrid class"); } else {
printf("Fail"); }
getch();}
![Page 15: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/15.jpg)
Output
Enter the marks:657570The average is:70.000000The Grade is: First class
![Page 16: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/16.jpg)
Looping structure
• It is used to execute some instructions several time based on some condition.– WHILE – Do…WHILE – For
![Page 17: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/17.jpg)
WHILE Loop
Syntax.
WHILE (condition){
.Body of the loop;
. }
Body of The loop
conditionFalse
True
![Page 18: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/18.jpg)
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();}
![Page 19: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/19.jpg)
Output
Enter the Number:3The value of 3! is: 6
![Page 20: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/20.jpg)
DO…WHILE Loop
Syntaxdo
{
Body of the loop
}while (condition);
Body of The loop
condition
False
True
![Page 21: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/21.jpg)
for loop
Syntaxfor (initialization; test condition; Increment/Decrement)
{
Body of the loop
}
![Page 22: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/22.jpg)
for loop
Initialization
condition False
Body of the loop
Inc / Decrement
![Page 23: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/23.jpg)
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();}
![Page 24: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/24.jpg)
Output
Enter the Number:3The value of 3! is: 6
![Page 25: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/25.jpg)
Nested for loop
Syntaxfor (initi; cond; Inc/Dec)
{
for (initi; cond; Inc/Dec){
Body of the loop
}
}
![Page 26: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/26.jpg)
CASE structure
Case 1
Case 2
Defaultcase
Switch
![Page 27: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/27.jpg)
CASE structureSyntaxswitch (expression){case constant 1:
block1;break;
case constant 2:block2;break;..
default :default block;break;
}
![Page 28: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/28.jpg)
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;}
![Page 29: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/29.jpg)
case 2:{printf("\n Its in case 2");break;}
default:{printf("\n Its in default");break;}
} getch();}
![Page 30: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/30.jpg)
Output
Enter the Number:2Its in case 2
![Page 31: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/31.jpg)
break Statement
• It is used to terminate the loop• When a break statement is encountered
inside a loop, then the loop is terminated.
![Page 32: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/32.jpg)
Loops with break Statement
while(cond){
…………if(cond)break;…………
}
![Page 33: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/33.jpg)
do{
…………if(cond)break;…………
} while(cond);
![Page 34: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/34.jpg)
for (initi; condt; Inc/Dec){…………if(cond)break;…………
}
![Page 35: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/35.jpg)
Continue Statement
• When a continue statement is encountered inside a loop, the control is transferred to the beginning.
![Page 36: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/36.jpg)
Loops with continue Statement
while(cond){
…………if(cond)continue;…………
}
![Page 37: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/37.jpg)
do{
…………if(cond)continue;…………
} while(cond);
![Page 38: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/38.jpg)
for (initi; condt; Inc/Dec){…………if(cond)continue;…………
}
![Page 39: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/39.jpg)
goto Statement
• When a goto statement is encountered inside a loop, the control is transferred to the beginning.
![Page 40: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/40.jpg)
Syntax for goto Statement
label:………………………………goto label;…………
![Page 41: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/41.jpg)
goto label; ……………………………… label:…………
![Page 42: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/42.jpg)
![Page 43: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/43.jpg)
getchar() Example
#include<stdio.h>#include<conio.h>#include<ctype.h>void main(){char x;printf("enter the character:");x=getchar();
![Page 44: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/44.jpg)
if(islower(x))putchar(toupper(x));
elseputchar(tolower(x));
getch();}
Output:enter the character:ABCa
![Page 45: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/45.jpg)
getche() Example
#include <stdio.h>#include <conio.h>void main(){
char c ;clrscr();printf("\nInput a string:");c = getche();
![Page 46: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/46.jpg)
printf("\nstring is:");putch(c);getch();
}
Output:Input a string:kstring is:k
![Page 47: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/47.jpg)
Getch() Example
#include <stdio.h>#include <conio.h>void main(){
char c;clrscr();printf("\nInput a string:");c = getch();
![Page 48: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/48.jpg)
printf("\nstring is:");putch(c);getch();
}Output:Input a string:string is:h
![Page 49: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/49.jpg)
getc Example
#include<stdio.h>#include<conio.h>#include<ctype.h>void main(){char x;printf("enter the character:");x=getc(stdin);
![Page 50: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/50.jpg)
if(islower(x))putc(toupper(x),stdout);
elseputc(tolower(x),stdout);
getch();}
Output:enter the character:abcA
![Page 51: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/51.jpg)
gets() Example
#include <stdio.h>#include<conio.h>void main(){
char c[80];clrscr();printf("Input a string:");gets(c);
![Page 52: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/52.jpg)
printf("The string is:");puts(c);getch();
}
Output:Input a string:qwertyThe string is:qwerty
![Page 53: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/53.jpg)
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);
![Page 54: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/54.jpg)
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;
![Page 55: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/55.jpg)
case 3:c=a*b;printf("\nThe result of Multiplication is:%d",c);break;
case 0:exit(0);break;
}getch();
}
![Page 56: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/56.jpg)
Output
Enter the value of a,b:56MENU1.ADD2.SUB3.MULTIPLY0.EXITEnter the choice:1The result of Addition is:11
![Page 57: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/57.jpg)
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;
}
![Page 58: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/58.jpg)
if(a==sum){ printf("\nIt is an armstrong number");}else{ printf("\nIt is not an armstrong number");}
getch();}
![Page 59: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/59.jpg)
Output
Enter the number:153It is an armstrong number
![Page 60: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/60.jpg)
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;
![Page 61: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/61.jpg)
sum=sum+r; n=n/10; } printf("sum of the digits is:%d",sum);}
![Page 62: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/62.jpg)
Output
Enter the no:156sum of the digits is:12
![Page 63: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/63.jpg)
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)
![Page 64: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/64.jpg)
{ r=n%10; sum=sum*10+r; n=n/10;
} printf("Reverse of the number is:%d",sum); getch();}
![Page 65: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/65.jpg)
Output
Enter the no:567Reverse of the number is:765
![Page 66: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/66.jpg)
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);
![Page 67: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/67.jpg)
while(f<n){ f=f1+f2;
f1=f2; f2=f;
printf("\t%d",f);}
getch();}
![Page 68: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/68.jpg)
Output
Enter the number:5 0 1 1 2 3 5
![Page 69: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/69.jpg)
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;
![Page 70: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/70.jpg)
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
![Page 71: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/71.jpg)
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);
![Page 72: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/72.jpg)
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
![Page 73: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/73.jpg)
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;
![Page 74: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/74.jpg)
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( );}
![Page 75: Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration](https://reader035.vdocuments.us/reader035/viewer/2022070415/5697bf8f1a28abf838c8d6b8/html5/thumbnails/75.jpg)
Output
Enter the value of a:4
Enter the value of b:5
Enter the value of c:6
The roots are imaginary