You are on page 1of 15

Name : Jimmy Gupta & Jatin Mittal

Roll : 7477 & 7481

Q : WAP to compute the sum of the first n terms of the following series

S = 1+1/2+1/3+1/4+1/5+…..

Algorithm :::::::::::

1. Prompt and read in the number of numbers to be summed.


2. Initialize sum for zero numbers
3. While less than n numbers have been summed repeatedly do
(a) Read in next number,
(b) Compute current sum by adding the number read to the most recent sum.
4. Write out sum of n numbers.
Name : Jimmy Gupta & Jatin Mittal

Roll : 7477 & 7481

Q : WAP to compute the sum of the first n terms of the following series

S = 1-2+3-4+…..

Algorithm :::::::::::

1. Prompt and read in the number of numbers to be summed.


2. Initialize sum for zero numbers
3. While less than n numbers have been summed repeatedly do
(c) Read in next number,
(d) Compute current sum by adding the number read to the most recent sum.

4. Write out sum of n numbers.


Name : Jimmy Gupta & Jatin Mittal

Roll : 7477 & 7481

Q : WAP to compute the factorial of a given number N

Algorithm :::::::::::

1. Establish n, the factorial required where n >= 0


2. Set product p for 0! ( special case ). Also set product count to zero.
3. While less than n products have been calculated repeatedly do
(a) Increment product count,
(b) Compute the i th product p by multiplying i by the most recent product.
4. Return the result n! .
Name : Jimmy Gupta & Jatin Mittal

Roll : 7477 & 7481

Q : WAP to reverse a given integer N

Algorithm :::::::::::

1. Establish n, the positive integer to be reversed


2. Set the initial condition for the reversed integer dreverse.
3. While the integer being reversed is greater than zero do
(a) Use the remainder function to extract the rightmost digit of the number being reversed
(b) Increase the previous reversed integer representation dreverse by a factor of 10 and add to
it the most recently extracted digit to give the current dreverse value
(c) Use integer division by 10 to remove the rightmost digit from the number being reversed.
Name : Jimmy Gupta & Jatin Mittal

Roll : 7477 & 7481

Q : Write a method to find whether a given number is prime or not. Use the same to generate the prime
numbers less than 100.

Algorithm :::::::::::

1. Initialize and write out the first 3 primes. Also initialize the square of the 3 rd prime.
2. Initialize x to 5.
3. While x less than n do
(a) Get next x value excluding multiples of 2 and 3
(b) If not past end of multiples list then
(b.1) if x >= square of largest prime then
(1.a) include next prime multiple as its square
(1.b) update square by squaring next prime > sq. root x
(c) While have not established x is non – prime with valid prime multiples do

(c.1) while current prime multiple is less than x , increment by current prime value doubled

(c.2) do prime test by comparing x with current multiple

(d) if current x prime then

(d.1) write out x and if it is less than sq. root n store it .


Name : Jimmy Gupta & Jatin Mittal

Roll : 7477 & 7481

Q : WAP to compute factors of a given number N

Algorithm :::::::::::

1. Establish n the number whose factors are sought.


2. Compute the remainder r and quotient q for the first prime nxtprime = 2
3. While it is not established that n is prime do
(a) If nxtprime is an exact divisor of n then
(a.1) save nxtprime as a factor f
(a.2) reduce n by nxtprime
Else
(a’.1) get next biggest prime from sieve of Eratosthenes
(b) Compute next quotient q and remainder r for current value of n and current prime divisor
nxtprime
4. If n is greater than 1 then add n to list as a factor f.
5. Return the factors f of the original number n.
Name : Jimmy Gupta & Jatin Mittal

Roll : 7477 & 7481

Q : Write a method to generate the nth term of fibonacci sequence

Algorithm :::::::::::

1. Establish n , indicating the nth Fibonacci number is required.


2. Derive the binary representation of n by repeated division by 2 and store representation in
array d[1..i-1]
3. Initialize the first two members of the doubling sequence.
4. Stepping down from the (i-1)th most significant digit in the binary representation of n to 1
do
(a) Use current pair of Fibonacci numbers f(n) and f(n+1) to generate the pair f(2n) and
f(2n+1)
(b) If current binary digit d[k] is zero then make the reassignments to f(n) and f(n+1) else
extend sequence by 1 number and then make the reassignments to f(n) and f(n+1)
5. Return the nth Fibonacci number f(n).
Name : Jimmy Gupta & Jatin Mittal

Roll : 7477 & 7481

Q : Write a method to generate g.c.d. of two positive numbers

Algorithm :::::::::::

1. Establish the two positive integers whose gcd is being sought


2. Repeatedly
(a) Get the remainder from dividing the larger integer by the smaller
(b) Let the smaller assume the role of the larger integer
(c) Let the remainder assume the role of the divisor until a zero remainder is obtained
3. Return the gcd of the original pair of integers.
Name : Jimmy Gupta & Jatin Mittal

Roll : 7477 & 7481

Q : WAP to find max number in a set of N numbers

Algorithm :::::::::::

1. Establish an array a[1…n] of n elements where n >= 1


2. Set temporary maximum max to first array element.
3. While less than n array elements have been considered do
(a) If next element greater than current maximum max then assign it to max
4. Return maximum max for the array of n elements
Name : Jimmy Gupta & Jatin Mittal

Roll : 7477 & 7481

Q : WAP to remove duplicate from an ordered array

Algorithm :::::::::::

1. Establish the array a[1…n] of n elements


2. Set loop index i to 2 to allow correct termination
3. Compare successive pairs of elements until a duplicate is encountered then set unique element
count j
4. While all pairs have not been examined do
(a) If next pair not duplicated then
(a.1) add one to unique element count j
(a.2) move later element of pair to array position determined by the unique element count j
Name : Jimmy Gupta & Jatin Mittal

Roll : 7477 & 7481

Q : WAP that sorts a list of N numbers using selection sort

Algorithm :::::::::::

1. Establish the array a[1…n] of n elements


2. While there are still elements in the unsorted part of the array do
(a) Find the minimum min and its location p in the unsorted part of the array a[i…n]
(b) Exchange the minimum min in the unsorted part of the array with the first element a[i] in
the unsorted array
Name : Jimmy Gupta & Jatin Mittal

Roll : 7477 & 7481

Q : WAP that sorts a list of N numbers using insertion sort

Algorithm :::::::::::

1. Establish the array a[1…n] of n elements


2. Find the minimum and put it in place to act as sentinel
3. While there are still elements to be inserted ordered part do
(a) Select next element x to be inserted
(b) While x is less than preceding element do

(b.1) move preceding element up one position

(b.2) extend search back one element further

(c) insert x at current position


Name : Jimmy Gupta & Jatin Mittal

Roll : 7477 & 7481

Q : WAP that sorts a list of N numbers using bubble sort

Algorithm :::::::::::

1. Establish the array a[1…n] of n elements


2. While there are still elements in the unsorted part of the array do
(a) Set the order indicator sorted to true
(b) For all adjacent pairs of elements in the unsorted part of the array do
(b.1) if current adjacent pair not in non – descending order then
(1.a) exchange the elements of the pair
(1.b) set sorted to false

3. Return the sorted array


Name : Jimmy Gupta & Jatin Mittal

Roll : 7477 & 7481

Q : WAP to merge the 2 ordered arrays of integers

Algorithm :::::::::::

1. Establish the array a[1…m] and b[1…n]


2. If last a element less than or equal to last b element then
(a) Merge all of a with b
(b) Copy rest of b
Else
(a’) merge all of b with a
(b’) copy rest of a
3. Return the merged result c[1…n+m]
Name : Jimmy Gupta & Jatin Mittal

Roll : 7477 & 7481

Q : WAP that search for a given element x in a set of N numbers using binary search

Algorithm :::::::::::

1. Establish the array a[1…n] and the value sought x


2. Assign the upper and lower variables to the array limits
3. While lower < upper do
(a) Compute the middle position of remaining array segment to be searched
(b) If the value sought is greater than current middle value then
(b.1) adjust lower limit accordingly
Else
(b’.1) adjust upper limit accordingly
4. If the array element at lower position is equal to the value sought then
(a) Return found
Else
(a’) return not found

You might also like