Lab 05, Esc101, 2004-05 Semester-II

Solve the following problems


Problem 1

    The Fibonacci sequence is defined by the following rule. The first two values in the sequence are 1 and 1. Every subsequent value is the sum of the two values preceding it. For example, the third value is 1 + 1 = 2, the fourth value is 1 + 2 = 3.
    If fn denotes the nth value in the Fibonacci sequence, then

f1 = 1
f2 = 1
fn = fn-1 + fn-2  if n > 2.

    Write a program that prints the values f1 to fn  for a given n.


Problem 2

    Factoring of integers.  Write a program that prints all the factors of a given integer. For example, when the user enters 150, the program should print

2
3
5
5


Problem 3

  
Count the number of occurrences of a string within another string. For example in "abcbc", "bc" occurs twice.



Problem 4

Given a large string, print it in parts of three.  For instance, given "abcdefghijklmnop", print:

abc
def
ghi
jkl
mno
p