Algorithm-1 Aim: Write An Algorithm For Even or Odd
Algorithm-1 Aim: Write An Algorithm For Even or Odd
Algorithm-1 Aim: Write An Algorithm For Even or Odd
Step 1: Start
Step 2: Read: N
Step 3: If N%2 == 0 Then Print N is an Even Number.
Step 4: Else Print N is an Odd Number.
Step 5: Stop
Flow-chart
start
Read n
No yes
If n%2=0
stop
Algorithm-2
Aim: Write Algorithm to find biggest of 3 numbers.
Step 1: Start
Step 2: Read variables a,b and c.
Step 3: If a>b and a>c then
print a is the largest number.
Else If b>c
print b is the largest number.
Else
print c is the greatest number.
Step 4: Stop
Flow-chart
start
Read a, b, c
yes
If a> b
& a>c
Print “a is big”
No
yes
If b>c
No
Print “b is big”
Print “c is big”
stop
Algorithm-3
Algorithm for print weekdays using Switch case
Step 1:Start
Step 2: print the following message
"Enter 1 for Sunday, 2 for Monday, 3 for Tuesday , 4 for Wednesday,
5 for Thursday, 6 for Friday, 7 for Saturday.
Step 3: Accept your choice in ch.
Step 4: Switch(ch)
Step 5: if the choice in ch is 1 print "sunday".
Step 6: else if the choice in ch is 2 print "Monday".
Step 7: else if the choice in ch is 3 print "Tuesday".
Step 8: else if the choice in ch is 4 print "Wednesday".
Step 9: else if the choice in ch is 5 print "Thursday".
Step10: else if the choice in ch is 6 print "Friday".
Step11: else if the choice in ch is 7 print "Saturday".
Step12: else print "please enter a value between 1 to 7".
Step13: Stop
Flowchart
start
yes
If ch=1
Print “Sunday”
No
yes
If ch=2
No Print “Monday”
yes
If ch=3
No Print “Tuesday”
yes
If ch=4
No Print “Tuesday”
yes
If ch=5
No Print “Tuesday”
yes
If ch=6
No Print “Tuesday”
yes
If ch=7
No Print “Tuesday”
stop
Algorithm-4
Algorithm for print reverse of the number
step 1: Start
step 2: Intilize rev=0.
step 3: Read a number num
step 4: Check whether the number n>0 then go to step 5
else go to step 9
step 5: rem=rem%10
step 6: rev=rev*10+rem
step 7: num =num/10
step 8: Go to step 4
step 9: Print rev
step 10: Stop
Flowchart
start
Read num
rev=0
yes
If num>0
No rem=num%10
rev=rev*10+rem
num=num/10;
Print rev
stop
ALGORITHM-5
Algorithm for number is Armstrong or not
Step 1: Start
Step 2: read number num
Step 3: set sum=0 and temp=num
Step 4: r=num%10
Step 5: sum=sum+(r*r*r)
Step 6: num=num/10
Step 7: repeat steps 4 to 6 until num > 0
Step 8: if sum = temp print “number is Armstrong” otherwise print “number is not
Armstrong”
Step 9: stop
Flowchart
start
Read num
sum=0, temp=num
No yes
If num>0
r=num%10
yes sum=sum+r*r*r
If
num=num/10;
sum=tem
p
Print “armstrong”
No
Print “not
Armstrong"
”
stop
Algorithm-6
Algorithm for number is prime or not
Step 1: start
Step 2: set i=1, count=0;
Step 3: read num
Step 4: For i=2 to num repeat the step 5
Step 5: if num%i =0 then count=count+1 and goto step 6
Step 6: if count =0 then print “Prime number”
else print “Not a prime number”
Step 7: stop
Flowchart
Start
Read num
i=2, count=0
yes
If i<num
No
No
If num%i=0
yes i=i+1
count=count+1
No If yes
count=0
Print “prime"
Print “not prime”
stop
Algorithm-7
Algorithm to add digits of a number up to single digit.
Step 1: start
Step 2: Accept numer num
Step 3: Repeat the steps 4,5,9 until num>9
Step 4: initialize Sum = 0
Step 5: Repeat the steps 6,7,8 until num >0
Step 6: rem = num % 10;
Step 7: Sum = Sum + rem;
Step 8: num = num / 10;
Step 9: num=sum
Step 10: Print sum
Step 8: stop
Flowchart
start
Read num
yes
If num>9
No
sum=0
Print sum
yes
If num>0
stop
No rem=num%10
sum=sum+rem
num=num/10;
num=sum
Algorithm-8
Algorithm for print Fibonacci series
Step 1: Start
Step 2: Take no. of terms in Fibonacci series as nt
Step 3: Initialize variables fib1=0, fib2=1, count=2;
Step 4: print first two terms i.e. fib1 and fib2
Step 5: Repeat the steps 6,7,8,9 until count <=nt
Step 6: fib3=fib1+fib2
Step 7: print fib3
Step 8: fib1=fib2 and fib2=fib3
Step 9: count=count+1
Step 6: Stop
start
Read nt
Fib1=0,
fib2=1,
count=2
Print fib1
and fib2
yes
If count<=nt
No Fib3=fib1+fib2
Fib1=fib2
Fib2=fib3
Count=count+1
Algorithm-9
Algorithm for sorting of Array of elements
Step 1:
Start
Step 2:
Accept n for length of an array
Step 3:
Accept n of values and store it in an array a
Step 4:
for i=0 to n follow the step 5
Step 5:
for j=i+1 to n follow the step 6
Step 6:
if a[i]>a[j] then swap a[i] & a[j] as follows
t=a[i]
a[i]=a[j]
a[j]=t
Step 7: print the rearranged array a.
Step 8: Stop
start
Read array
values in a
i=0
No yes
If i<n
j=i+1
Print re
arranged
array a yes
If j<n
No yes
If a[i]>a[j]
stop i=i+1
t=a[i],
No
a[i]=a[j]
a[j]=t
j=j+1
Algorithm-10
Algorithm for addition of two matrices
Step 1:Start
Step 2: Accept ra and ca for rows and columns of matrix A
respectively.
Step 3: Accept rb and cb for rows and columns of matrix B
respectively.
Step 4: if ra==rb and ca==cb then
accept matrix A with a row size ra and column size ca and
accept matrix B with a row size rb and column size cb and
take an empty matrix C.
Step 5: else print "Matrix addition is not possible" and goto
step14
Step 6: i=0
Step 7: Repeat the septs 8,9,12 until i<ra
Step 8: j=0
Step 9: Repeat the steps 10, 11 until j<ca
Step 10: C[i][j]=A[i][j]+B[i][j]
Step 11: j=j+1
Step 12: i=i+1
Step 13: print the resultant matrix C.
Step 14: Stop
start
No
If ra=rb &
ca=cb
i=0
No yes
If i<ra
j=0
Print Matrix C
yes
If j<ca
No C[i][j]=A[i][j]+B[i][j]
stop
i=i+1 j=j+1
Algorithm-11
Algorithm for String Palendrome
Step 1: Start
Step 2: Accept a String str1
Step 3: copy string str1 to sting str2
Step 4: Reverse the String str1 using String function strrev
Step 4: if str1=str2 then print "palindrome" otherwise print "Not
palindrome".
Step 5: Stop
start
Read a string
str1
str2=str1
Strrev(str1)
No yes
If
str1=str3
Print Print
“Not palindrome” “palindrome
”
stop
Algorithm-12
Algorithm to find factorial of a given number using
Recursion.
Step 1: Start
Step 2: Read number n
Step 3: Call factorial function as fact(n)
Step 4: Print f for factorial of a given number n.
Step 5: Stop
fact(n)
Step 1: If n==1 then return 1
Step 2: Else return(n*fact(n-1))
start
Function fact(n)
Read n
yes
If n=1
f=fact(n)
Return n
No
Print f
n*fact(n-1)
stop
Algorithm-13
Algorithm for Call by value and Call by Reference
Step 1: Start
Step 2: declare a function swap with pointer arguments
Step 3: Accept two numbers x,y.
Step 4: Call the function swap with arguments(address of x, address of y) as
swap(&x,&y)
Step 5: print x,y
Step 6: Stop
Function swap(*a,*b)
Step 1: Take a variable c
Step 2: swap the values *a and *b as follows
c=*a
*a=*b
*b=c
Step 3: return
start
Function swap(*a,*b)
Read x,y
c=*a,
*a=*b,
swap(&x,&y)
*b=c
Print x,y
Return
stop
Algorithm-14
Algorithm for Employee Structure
Step 1: Start
Step 2: Take a Structure called employee and declare eno,
ename, Desg, Sal in that structure
Step 3: Take structure variable e to the structure employee
Step 4: Accept employee details as follows.
e.eno,e.ename,e.desg,e.sal
Step 5: Print employee details as follows
e.eno,e.ename,e.desg,e.sal
Step 6: Stop
start
Employee
structure
Eno, ename,
desg, sal
Taka a structure
variable e to the
structure employee
stop
Algorithm-15
Algorithm to copy the content of one to another file.
Step 1: Start
Step 2: take a file filename1 for reading and another file
filename2 for writing.
Step 3: Take a file pointer fp1 and assign it to filename1 in
reading mode.
Step 4: if fp1=null then print “can not open filename1” and goto
step 9
else goto step 5
Step 5: Take another file pointer fp2 and assign it to filename2 in
writing mode.
Step 6: Do the step 7 until file pointer fp1 points to end of file.
Step 7: Read a character from fp1 and write that character to
fp2.
Step 8: Display the message “content copied..”
Step 9: close files fp1 and fp2
Step 10: Stop
start
fp1=fopen(“filename1”,”r”)
No yes
if fp1=null
c=fgetc(fp1)
yes
If c=EOF
(end of file)
No
print “content copied..”
fputc(c,fp2)
stop