Está en la página 1de 10

ALGORITHM

(SIZE [size of queue]; F,R [front and rear end pointers], Q[] [Queue], ITEM (to be
deleted/inserted are global variables accessible to all methods)

Algorithm for void insert()

STEP 0: START
STEP 1: IF (F=SIZE), THEN DISPLAY “QUEUE OVERFLOWS”, ELSE GO TO STEP 3.
STEP 2: RETURN
STEP 3: IF (F=NULL AND R=NULL), THEN SET F to 0 AND R to 0; ELSE GO TO STEP 4.
STEP 4: SET R to R+1.
STEP 5: SET Q[R] to ITEM
STEP 6: END

Algorithm for void delete()

STEP 0: START
STEP 1: IF(F=NULL AND R=NULL), THEN DISPLAY “Queue Underflows”.
STEP 2: SET ITEM to Q[F]
STEP 3: DISPLAY “Deleted item: “, ITEM
STEP 4: IF (F=R), THEN SET F=NULL AND R=NULL, ELSE GO TO STEP 5.
STEP 5: SET F to F+1.
STEP 6: END

Algorithm for void main()

STEP 0: START
STEP 1: DECLARE integer “choice” AND SET it TO 1.
STEP 2: DECLARE integer “n” AND ACCEPT its value from the user.
STEP 3: IF choice ≠0, THEN GO TO STEP 4.
STEP 4: ACCEPT choice from the user after displaying queue operations.
STEP 5: IF choice=1, THEN ACCEPT an integer “value” AND IMPLEMENT algorithm of void
insert() taking value as parameter.
STEP 6: ELSE IF choice=2, THEN implement algorithm of void delete()
STEP 7: ELSE IF choice=3, DISPLAY the array Q[F]
STEP 8: ELSE DISPLAY “Invalid choice”
STEP 9: REPEAT STEPS 3 TO 8, TILL choice=0.
STEP 10: END.
ALGORITHM

Algorithm for void insert()

STEP 0: START
STEP 1: IF (SP=SIZE), TEHN DISPLAY “STACK OVERFLOWS”, ELSE GO TO STEP 2.
STEP 2: SET SP to SP+1.
STEP 3: SET STACK[SP] to ITEM.
STEP 4: END

Algorithm for void delete()

STEP 0: START
STEP 1: IF (SP=NULL), THEN DISPLAY “STACK UNDERFLOWS”, ELSE GO TO STEP 2.
STEP 2: SET val to STACK[SP].
STEP 3: DISPLAY “Popped element: “, val
STEP 4: SET SP=SP-1.
STEP 5: END

Algorithm for void main()

STEP 0: START
STEP 1: IMPLEMENT algorithm of void insert()
STEP 2: IMPLEMENT algorithm of void delete()
STEP 3: DISPLAY STACK[]
STEP 4: END.
ALGORITHM
(SIZE [size of queue]; F,R [front and rear end pointers], Q[] [Queue], ITEM (to be
deleted/inserted are global variables accessible to all methods)

Algorithm for void add()

STEP 0: START
STEP 1: IF (F=0 AND R=SIZE) OR (F=R+1), THEN DISPLAY “QUEUE OVERFLOWS”.
STEP 2: IF (F=NULL AND R=NULL), THEN SET F=0 AND R=0, ELSE GO TO STEP 3.
STEP 3: IF (R=SIZE) THEN SET R=0, ELSE SET R=R+1.
STEP 4: SET Q[R]= ITEM.
STEP 5: END.

Algorithm for void del()

STEP 0: START
STEP 1: IF (F=NULL AND R=NULL), THEN DISPLAY “Queue Underflows”
STEP 2: SET ITEM to Q[F]
STEP 3: DISPLAY “Deleted item: “. ITEM
STEP 4: IF (F=SIZE), THEN SET F to 0, ELSE GO TO STEP 5.
STEP 5: IF (F=R), SET F=NULL AND R=NULL, ELSE GO TO STEP 6.
STEP 6: SET F=F+1.
STEP 7: END

Algorithm for void main()

STEP 0: START
STEP 1: ACCEPT integer “n” (size of queue) from the user.
STEP 2: DECLARE integer choice and SET it to 1.
STEP 3: DELCARE Boolean “exit” AND SET it to FALSE.
STEP 4: IF (exit=TRUE), THEN GO TO STEP 5.
STEP 5: ACCEPT “choice”.
STEP 6: IF (choice=1), THEN IMPLEMENT algorithm of void add().
STEP 7: ELSE IF (choice=2), THEN IMPLEMENT algorithm of void del()
STEP 8: ELSE IF (choice=3), THEN DISPLAY Q[] (queue)
STEP 9: ELSE DISPLAY “Invalid input”
STEP 10: END
ALGORITHM

Algorithm for long sum_dig()

STEP 0: START
STEP 1: SET integer “copy” to n.
STEP 2: IF (n ≠ 0), THEN GO TO STEP 3.
STEP 3: SET long “d” to n%10
STEP 4: SET long “s” to s+d
STEP 5: SET N← N/10
STEP 6: REPEAT STEPS 2 TO 5 TILL n=0
STEP 7: RETURN value of “s”.
STEP 8: END

Algorithm for void main()


STEP 0: START
STEP 1: DECLARE long “n” AND ACCEPT its value from the user.
STEP 2: CONVERT “n” to a String AND SET it to str.
STEP 3: IF length of str ≠15, THEN DISPLAY “Invalid Input”, ELSE GO TO STEP 4.
STEP 4: DECLARE integer “k” AND SET IT TO 14.
STEP 5: DECLARE a[] of size 15.
STEP 6: IF (n ≠ 0), THEN GO TO STEP 7
STEP 7: SET long d←n%10
STEP 8: SET a[k]←d
STEP 9: SET k←k-1
STEP 10: SET n←n/10
STEP 11: REPEAT STEPS 6 TO 10, TILL n=0
STEP 12: DECLARE integer “i” AND SET i←0
STEP 13: IF (i%2≠0), THEN SET a[i]←a[i]*2, AND DISPLAY a[i], ELSE GO TO STEP 14
STEP 14: DISPLAY a[i].
STEP 15: SET i←i+1.
STEP 16: IF i<15, THEN REPEAT STEPS 13 TO 15, ELSE GO TO STEP 17.
STEP 17: SET i←0
STEP 18: IMPLEMENT algorithm of long sum_dig() AND SET s←s+sum_dig(i)
STEP 19: SET i←i+1.
STEP 20: IF i<15, THEN REPEAT STEPS 18 TO 19, ELSE GO TO STEP 21.
STEP 21: IF (sum%10=0), THEN DISPLAY “Valid IMEI number”.
STEP 22: END
ALGORITHM

Algorithm for String change()

STEP 0: START
STEP 1: DECLARE integers i, x; character ch and String s
STEP 2: SET i←0
STEP 3: SET ch← ith character of string s.
STEP 4: IF (ch=’Z’) THEN SET ch←’A’ ELSE GO TO STEP 5.
STEP 5: IF (ch=’Y’) THEN SET ch←’B’ ELSE GO TO STEP 6.
STEP 6: SET x←(integer value of)ch
STEP 7: SET x←x+1
STEP 8: SET ch←(character)x
STEP 9: RETURN
STEP 10: SET str←str+ch
STEP 11: SET i←i+1.
STEP 12: IF i<s.length(), THEN REPEAT STEPS 3 TO 11, ELSE GO TO STEP 13.
STEP 13: RETURN str.

Algorithm for void main()


STEP 0: START
STEP 1: ACCEPT String s as the sentence.
STEP 2: ACCEPT integers “p” and “q”
STEP 3: DECLARE String word[] AND SET it to s.split(“ “)
STEP 4: IF(p≠q), THEN GO TO STEP 5; ELSE GO TO STEP 11
STEP 5: IMPLEMENT algorithm of change() using word[p-1] as parameter
STEP 6: IMPLEMENT algorithm of change() using word[q-1] as parameter
STEP 7: SET i←0
STEP 8: DISPLAY word[i]
STEP 9: SET i←i+1
STEP 10: REPEAT STEPS 8 TO 9, TILL i<word.length
STEP 11: IMPLEMENT algorithm of change() using word[p-1] as parameter
STEP 12: SET i←0
STEP 13: DISPLAY word[i]
STEP 14: SET i←i+1
STEP 15: REPEAT STEPS 13 TO 14, TILL i<word.length
STEP 16: END
ALGORITHM
Algorithm for int isLeap()
STEP 0: START
STEP 1: IF (y%400=0) OR ((y%100≠0) AND (y%4=0)), THEN RETURN 29, ELSE RETURN
28.
STEP 2: END

Algorithm for boolean dateValidate()


STEP 0: START
STEP 1: SET month[2]←isLeap(y)
STEP 2: IF (m<0 OR m>12 OR d<0 OR d>month[m] OR y<0 OR y>9999), THEN RETURN
FALSE, ELSE RETURN TRUE.
STEP 3: END

Algorithm for int dayno()


STEP 0: START
STEP 1: SET integer dn←0
STEP 2: SET i←0
STEP 3: SET dn←dn+month[i]
STEP 4: SET i←i+1
STEP 5: REPEAT STEPS 3 TO 4 TILL i=m
STEP 6: SET dn←dn+d
STEP 7: SET i←1
STEP 8: IMPLEMENT algorithm of isLeap, IF isLeap(i)=29, THEN SET dn←dn+366, ELSE
SET dn←dn+365
STEP 9: RETURN dn.
STEP 10: END

Algorithm for void main()


STEP 0: START
STEP 1: ACCEPT date 1 and 2 as Strings date1 and date2.
STEP 2: SET integers d1 and d2 as date1.substring(0,p) and date2.substring(p+1,q)
respectively.
STEP 3:IF (ob.dateValidate(d1,m1,y1)=true & ob.dateValidate(d2,m2,y2)=true), THEN GO
TO STEP 4
STEP 4: DIFFERENCE: a-b.
STEP 5: END
ALGORITHM
Algorithm for SUPER Class: class Calculate
STEP 0: START
STEP 1: DECLARE integers n1 and n2 as global variables
STEP 2: START method void accept()
STEP 3: DISPLAY “Enter the value of n1 and n2 respectively”
STEP 4: ACCEPT values of n1 and n2 respectively
STEP 5: END of void accept()
STEP 6: START void display()
STEP 7: DISPLAY n1 “ “ n2
STEP 8: END

Algorithm for SUB Class: class Hcf extends Calculate


STEP 0: START
STEP 1: DECLARE integer “h” as a global variable
STEP 2: START method void findhcf()
STEP 3: CALL method void accept() in super class
STEP 4: DECLARE integer a and SET it to super.n1 (using concept of inheritance)
STEP 5: DELCARE integer b and SET it to super.n2
STEP 6: IF a<b, THEN GO TO STEP 7
STEP 7: SET a←b
STEP 8: SET b←super.n1 (using concept of inheritance)
STEP 9: DECLARE integer t
STEP 10: IF (a%b ≠ 0) , THEN GO TO STEP 11, ELSE GO TO STEP 15
STEP 11: SET t←(a%b)
STEP 12: SET a←b
STEP 13: SET b←t
STEP 14: GO TO STEP 10
STEP 15: SET h←b
STEP 16: END of void findhcf()
STEP 17: START void show()
STEP 18: DISPLAY "n1"+" "+"n2"+" "+"HCF"+" "+"LCM");
STEP 19:DISPLAY super.display() [Method overriding]
STEP 20: DISPLAY h
STEP 21: DECLARE integer lcm and SET it to (super.n1*super.n2)/h;
STEP 22: DISPLAY lcm
STEP 23: END

Algorithm for void main()


STEP 0: START
STEP 1: CREATE object “obj” and CALL methods findhcf(), show()
STEP 2: END
ALGORITHM

Algorithm for int sum_dig()

STEP 0: START
STEP 1: SET integer “copy” to n.
STEP 2: IF (n ≠ 0), THEN GO TO STEP 3.
STEP 3: SET long “d” to n%10
STEP 4: SET long “s” to s+d
STEP 5: SET N← N/10
STEP 6: REPEAT STEPS 2 TO 5 TILL n=0
STEP 7: RETURN value of “s”.
STEP 8: END

Algorithm for int sumPrimeFact()

STEP 0: START
STEP 1: SET i←2 and sum←0
STEP 2: IF (n>1) THEN GO TO STEP 3
STEP 3: IF (n%i=0), THEN sum=sum+sumdig(i); ELSE GO TO STEP 5
STEP 4: SET n=n/i.
STEP 5: SET i←i+1
STEP 6: REPEAT STEPS 3 TO 5, TILL n≤1
STEP 7: RETURN sum
STEP 8: END

Algorithm for void main()


STEP 0: START
STEP 1: ACCEPT number and SET it to integer n.
STEP 2: IMPLEMENT algorithm for int sum_dig() and SET it to integer “a”.
STEP 3: IMPLEMENT algorithm for int sumPrimeFact() and SET it to integer “b”.
STEP 4: IF (a=b), THEN DISPLAY “Smith Number”
STEP 5: END
ALGORITHM
(SIZE [size of queue]; F,R [front and rear end pointers], DQ[] [Queue], ITEM (to be
deleted/inserted) are global variables accessible to all methods)

Algorithm to Insert an element in Dqueue

STEP 0: START
STEP 1: IF (F=NULL AND R=NULL), THEN SET F=0 AND R=0.
STEP 2: SET DQ[F]=ITEM
STEP 3: IF (F=0), THEN DISPLAY “Overflow at Front End”, ELSE GO TO STEP 4
STEP 4: SET F=F-1
STEP 5: SET DQ[F]←ITEM
STEP 6: IF (R=SIZE), THEN DISPLAY “Overflow at Rear end”, ELSE GO TO STEP 7
STEP 7: SET R←R+1.
STEP 8: SET DQ[R]←ITEM
STEP 9: END

Algorithm to Delete an element from a Dqueue

STEP 0: START
STEP 1: IF (F=NULL AND R=NULL), THEN DISPLAY “Queue Underflows”.
STEP 2: SET ITEM←DQ[F]
STEP 3: DISPLAY “Deleted element is”, ITEM
STEP 4: IF(F=R), THEN SET F←NULL AND R←NULL, ELSE GO TO STEP 5.
STEP 5: SET F←F+1.
STEP 6: SET ITEM←DQ[R]
STEP 7: IF (F=R), THEN SET F←NULL, R←NULL
STEP 8: ELSE SET R←R-1.
STEP 9: END
ALGORITHM

STEP 0: START
STEP 1: DECLARE integer “m” and ACCEPT its value from the user
STEP 2: IF (m<3 OR m>9), THEN DISPLAY “Size out of range”, ELSE GO TO STEP 3
STEP 3: DECLARE 2-d array a[][] with size m*m
STEP 4: DECLARE integers “i” and “j”
STEP 5: SET i←0 and j←0
STEP 6: ACCEPT matrix element from the user and SET it to a[i][j]
STEP 7: SET j←j+1
STEP 8: REPEAT STEPS 6 TO 7 TILL j=m, THEN GO TO STEP 9
STEP 9: SET i←i+1
STEP 10: REPEAT STEPS 6 TO 9 TILL i=m, THEN GO TO STEP 11.
STEP 11: SET i←0 AND j←0
STEP 12: DISPLAY a[i][j]
STEP 13: SET j←j+1 AND i←i+1
STEP 14: DISPLAY “Matrix after 900 rotation”
STEP 15: SET i←0 AND j←m-1
STEP 16: DISPLAY a[i][j]
STEP 17: SET j←j-1
STEP 18: REPEAT STEPS 16 TO 17 TILL j=0, THEN GO TO STEP 19
STEP 19: SET i←i+1
STEP 20: REPEAT STEPS 16 TO 19 TILL i=m, THEN GO TO STEP 21.
STEP 21: DECLARE integer “sum” AND SET sum←0
STEP 22: SET sum← A[0][0]+A[0][m-1]+A[m-1][0]+A[m-1][m-1] {Sum of corner elements}
STEP 23: DISPLAY “Sum of corner elements: “, sum
STEP 24: END

También podría gustarte