0% found this document useful (0 votes)
46 views

Spring 2020 - CS502 - 1

This document contains an assignment with two questions about algorithms. Question 1 asks the student to design a pseudocode algorithm to identify if a number is a palindrome or not. The student provides an algorithm that reverses the number and compares it to the original to check for equality. Question 2 asks the student to calculate the worst case time complexity of the algorithm from Question 1. The student analyzes the algorithm and determines it has a worst case time complexity of O(n).

Uploaded by

m.shahid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Spring 2020 - CS502 - 1

This document contains an assignment with two questions about algorithms. Question 1 asks the student to design a pseudocode algorithm to identify if a number is a palindrome or not. The student provides an algorithm that reverses the number and compares it to the original to check for equality. Question 2 asks the student to calculate the worst case time complexity of the algorithm from Question 1. The student analyzes the algorithm and determines it has a worst case time complexity of O(n).

Uploaded by

m.shahid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 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 [email protected]

GOOD LUCK

You might also like