You are on page 1of 2

Assignment No.

01 Student id:mc170401069
Student Name:Mehvish noor
SEMESTER Spring 2020
Total Marks: 20
CS502- Fundamentals of Algorithms Due Date: 28/05/2020

Question No 01: (Marks: 10)


You are required to design (write) a simple algorithm (Only Pseudo code) which can identify a number to be
Palindrome or Not Palindrome.
Ans:
Following is the algorithm which can identify a number to be Palindrome or Not Palindrome. In following
algorithm I created a reversed number and original number then compare the two variables if they are equal then
the number is a Palindrome, and if they are not equal then number is Not Palindrome.
Algorithm(Pseudo code)

i- start
ii- read n;
iii= int n,num,digit,rev=0;
iv-n=num;
v-while(num!=0)
digit=num%10;
rev=rev*10+digit;
num=num/10;
end while
vi- if (rev==n)
display"Palindrome"
else
display"Not Palindrome"
end if
vii- stop;

Question No 02: (Marks: 10)


You are required to calculate (Step by Step) the worst case time complexity T(n) of the algorithm designed in
Question No. 01.
Solution:
The loop in the above algorithm is executed at most n\2 times when n is Palindrome. The computation within the
loop body takes constant time .the condition testing in the while loop is not executed at all .so the best case
performance of algorithm is o(1)and the worst case time complexity is (n) . The worst time complexity of above
algorithm according to frequency is
T(n)=1+1+1+n+n+n+1+1+1+1
T(n)=3n+7

=====================================Ended=======================================

For any query about the assignment, contact at CS502@vu.edu.pk

GOOD LUCK

You might also like