Sapmple Algorithms & Flow Chart

20
1 /* Program to check whether the given number is perfect or not*/ ALGORITHM-1 1. START 2. Read n 3. s=0 4. for i=1 to n do a) begin b)if (n%i==0) c)s=s+i d)end for 5. if s==n goto step 6 else goto step 7 6. write ‘Given number is perfect number ’ goto step 8 7. write ‘Given number is not a perfect number ’ 8. STOP (Alternative) ALGORITHM-2 1. START 2. Read n 3. i=1 4. s=0 5. if(i<n) goto step 6 else goto step 10 6. if (n%i==0) goto step 7 else goto step 8 7. s=s+i 8. i=i+1 9. goto step 5 10. if( s==n) goto step 11 else goto step 13 11. write ‘Given number is perfect number ’ goto step 12 12. write ‘Given number is not a perfect number ’ 13. STOP

Transcript of Sapmple Algorithms & Flow Chart

Page 1: Sapmple Algorithms & Flow Chart

1

/* Program to check whether the given number is perfect or not*/

ALGORITHM-1

1. START

2. Read n

3. s=0

4. for i=1 to n do

a) begin

b)if (n%i==0)

c)s=s+i

d)end for

5. if s==n goto step 6 else goto step 7

6. write ‘Given number is perfect number ’ goto step 8

7. write ‘Given number is not a perfect number ’

8. STOP

(Alternative) ALGORITHM-2

1. START

2. Read n

3. i=1

4. s=0

5. if(i<n) goto step 6 else goto step 10

6. if (n%i==0) goto step 7 else goto step 8

7. s=s+i

8. i=i+1

9. goto step 5

10. if( s==n) goto step 11 else goto step 13

11. write ‘Given number is perfect number ’ goto step 12

12. write ‘Given number is not a perfect number ’

13. STOP

Page 2: Sapmple Algorithms & Flow Chart

2

FLOW CHART

T F

F

T F

T

START

Read n

i=1

i++

i<n

n%i==0

?

s=s+i

WRITE

‘ NOT

PERFECT

S=0

s==n

?

WRITE

‘PERFECT

STOP

Page 3: Sapmple Algorithms & Flow Chart

3

/* Program to check whether the given number is perfect or not*/

#include<stdio.h>

#include<conio.h>

void main() /*main declaration*/

{ /*varable declaration*/

int i,s=0,n;

clrscr();

printf("Enter the values of n:");

scanf("%d",&n);

for(i=1;i<n;i++)

{

if(n%i==0)

{

s=s+i;

}

}

if(s==n)

printf("The given number is perfect number");

else

printf("The given number is not a perfect number");

getch();

}

Page 4: Sapmple Algorithms & Flow Chart

4

/* Write a C program to find the reverse of a given number*/

ALGORITHM

1. START

2. read n

3. s=0

4. x=n

5. while(n!=0)

6. begin

7. r=n%10

8. s=s*10+r

9. n=n/10

10. end while

11. write s

12. STOP

(Alternative) ALGORITHM

1. START

2. read n

3. s=0

4. x=n

5. if (n!=0) goto step 6 else goto step 10

6. r=n%10

7. s=s*10+r

8. n=n/10

9. goto step 5

10. write s

11. STOP

Page 5: Sapmple Algorithms & Flow Chart

5

FLOW CHART(To find the reverse of a given number)

T F

START

Read n

n!=0

?

r=n%10

WRITE s

x=n

s=0

s=s*10+r

n=n/10

s=0

s=0

STOP

Page 6: Sapmple Algorithms & Flow Chart

6

/* Write a C program to find the reverse of a given number*/

#include<stdio.h>

#include<conio.h>

void main()

{

long n,x,s=0;

int r;

clrscr();

printf("Enter the value of n:");

scanf("%ld",&n);

x=n;

while(n!=0)

{

r=n%10;

s=s*10+r;

n=n/10;

}

printf("\nGiven number is %ld",x);

printf("\nReversed number is %ld",s);

getch();

}

Page 7: Sapmple Algorithms & Flow Chart

7

/*program for arithematic operatios (+.-,*,/) */

ALGORITHM

1. START

2. Read a,b,choice

3. If (choice==’+’)

a) c=a+b

b) write c

c) goto 8

4. If (choice==’-‘)

a) c=a-b

b) write c

c) goto 8

5. If (choice==’*’)

a. c=a*b

b) write c

c) goto 8

6. If (choice==’/‘)

a) c=a-b

b) write c

c) goto 8

7. default

write Invalid option

8. STOP

Page 8: Sapmple Algorithms & Flow Chart

8

FLOW CHART(For arithematic operatios (+.-,*,/) *)

+

-

* / default

START

READ

a,b,choice

choice

?

c=a+b c=a-b

c=a*b

c=a/b

WRITE c

WRITE ‘

Invalid

option’

STOP

Page 9: Sapmple Algorithms & Flow Chart

9

/*program for arithematic operatios (+.-,*,/) */

#include<stdio.h>

#include<conio.h>

void main() /*main declaration*/

{ /*varable declaration*/

int a,b;

float c;

char ch;

clrscr();

printf("Enter ur choise(+,-,*,/):");

scanf("%c",&ch);

printf("Enter the values of a,b:");

scanf("%d%d",&a,&b);

switch(ch)

{

case '+':c=a+b;

printf("Result=%f",c);

break;

case '-':c=a-b;

printf("Result=%f",c);

break;

case '*':c=a*b;

printf("Result=%f",c);

break;

case '/':c=(float)a/b;

printf("Result=%f",c);

break;

default :printf("Error in input");

}

getch();

}

Page 10: Sapmple Algorithms & Flow Chart

10

/* Program to check whether the given number is prime or not*/

Algorithm-1

1. Start

2. Read n

3. K=1

4. for(i=2;i<=n/2;i++)

5. begin

6. if(n%i==0) goto 7 else goto 4

7. k=0

8. goto 10

9. end for

10. if(k==1) goto 11 else goto 12

11. WRIE "The given number is prime" goto 13;

12. WRIE "The given number is not a prime"

13. stop

(Alternative)Algorithm-2

1. start

2. READ n

3. k=1

4. i=2

5. if(i<=n/2) goto step6 else goto step 11

6. if(n%i==0) goto 7 else goto 9

7. k=0

8. goto 11

9. i++

10. goto 5

11. if(k==1) goto 12 else goto 13

12. WRIE "The given number is prime" goto 14

13. WRIE "The given number is not a prime"

14. stop

Page 11: Sapmple Algorithms & Flow Chart

11

FLOW CHART(To check whether the given number is prime or not)

F

T

F

T F

T

START

READ n

i=1

i++

i<n

n%i==0

?

k=1

WRITE

‘ NOT

PRIME’

k=0

k==1

?

WRITE

‘PRIME

NUM’

STOP

Page 12: Sapmple Algorithms & Flow Chart

12

/* Program to check whether the given number is prime or not*/

/*C Program to check whether the given number is prime or not */

#include<stdio.h>

#include<conio.h>

void main()

{

int i,j,n,k=1;

clrscr();

printf("Enter the value of n:");

scanf("%d",&n);

for(i=2;i<=n/2;i++)

{

if(n%i==0)

{

k=0;

break;

}

}

if(k==1)

printf("The given number is prime");

else

printf("The given number is not a prime");

getch();

}

Page 13: Sapmple Algorithms & Flow Chart

13

/* program to find maximum of three numbers */

Algorithm

1.START

2.read a,b,c

3. if (a>b) then goto step4 else goto step 5

4.if(a>c) then max=a else max=c goto step6

5.if(b>c) then max=b else max=c goto step6

6. write max

7.STOP

FlowChart

F T T

F

F

F

T

START

Read a,b,c

a>b ?

a>c?

b>c ?

Max=c

Max=a

STOP

Max=b

Write max

Page 14: Sapmple Algorithms & Flow Chart

14

//maximum of 3 numbers

#include<stdio.h>

#include<conio.h>

void main() /*main declaration*/

{ /*varable declaration*/

int a,b,c,max;

clrscr();

printf("Enter the values of a,b,c:");

scanf("%d%d%d",&a,&b,&c);

if(a>b)

{

if(a>c)

max=a;

else

max=c;

} else if(b>c)

max=b;

else max=c;

printf("the max value is %d",max);

getch();

}

Page 15: Sapmple Algorithms & Flow Chart

15

/* Program to print Fibonacci series */

Algorithm-I

1.START

2.READ n

3.a=0

4.b=1

5. WRITE a,b

6. for i=1 to n-2 do

Begin

a) c=a+b;

b) WRITE c

c) a=b

d) b=c

End for

7.STOP

(Alternative) Algorithm-II

1.START

2.READ n

3.a=0

4.b=1

5. WRITE a,b

6. i=1

7. if (i<=n-2) goto step 8 else goto step 14

8. c=a+b

9. WRITE c

10. a=b

11 b=c

12 i=i+1

13 goto step 7

14.STOP

Page 16: Sapmple Algorithms & Flow Chart

16

FLOWCHART (To print Fibonacci series )

F

T

START

READ n

b=1

a=0

WRITE a,b

i=1

i++

i<=n-2

i<n

c=a+b

a=b

WRITE c

b=c

i=i+1

STOP

Page 17: Sapmple Algorithms & Flow Chart

17

/* Program to print Fibonacci series */

#include<stdio.h>

#include<conio.h>

void main()

{

int a,b,c,i,n;

clrscr();

printf("Enter n:");

scanf("%d",&n);

a=0;

b=1;

printf("\nFibonacci series:");

printf("%3d%3d",a,b);

for(i=1;i<=n-2;i++)

{

c=a+b;

printf("%3d",c) ;

a=b;

b=c;

}

getch();

}

Page 18: Sapmple Algorithms & Flow Chart

18

/*program to display students grade using if */

ALGORITHM

1. START

2. READ marks

3. If (marks >= 70)

WRITE ‘Passed with Distinction’ goto step8 else goto step 4

4. If (marks >= 60)

WRITE ‘Passed in First Class’ goto step8 else goto step 5

5. If (marks >= 50)

WRITE ‘Passed in Second Class’ goto step8 else goto step 6

6. If (marks >= 40)

WRITE ‘Pass Class’ goto step8 else goto step 7

7. WRITE ‘Failed’

8. STOP

Page 19: Sapmple Algorithms & Flow Chart

19

FLOWCHART

T

F

T

F

T

F

T

F

START

READ

marks

marks>=70 ? WRITE

‘DISTINCTION

ON

marks >=60 ? WRITE ‘FIRST

CLASS’

marks>=50

?

marks>=40

?

WRITE ‘SECOND

CLASS’

WRITE ‘PASS

CLASS’

WRITE FAIL’

STOP

Page 20: Sapmple Algorithms & Flow Chart

20

/*program to display students grade using if */

#include<stdio.h>

#include<conio.h>

void main() /*main declaration*/

{ /*varable declaration*/

int m,k;

clrscr();

printf("Enter marks:");

scanf("%d",&m);

if(m>=70)

printf("Distinction");

else if(m>=60)

printf("First class");

else if(m>=50)

printf("Second class");

else if(m>=40)

printf("Pass class");

else

printf("Fail");

}

getch();

}