Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

Sultan Qaboos University

College of Science
Department of Computer Science

COMP2002, Spring 2012

Week 6 Lab Exercises (Repetition Statements)

Exercise 1:
Write a C++ program that reads a list of positive numbers and outputs the largest number in the
list. Use any negative number as a sentinel value to indicate the end of the input list. Your output
should indicate which number is the largest. The output should include appropriate messages if the
input list contains less than two numbers.

Exercise 2:
Write a program that inputs two numbers x and n, where n is a positive integer, and finds the sum
of the first n terms of the series x + (x + x2) + (x + x2 + x3 ) + …
Use loop structures as appropriate to computer the sum.

Answer the following questions before developing your code:


1. How many terms are there in the sum?
2. In each term, i, how many values are added?

Submit your source file named lab6_xxxxx.cpp on Moodle before the end of the lab session
(replacing xxxxx with your student ID)

Additional Exercise:

1. Write a C++ program that reads a positive integer n and computes and displays the first n
terms of the Fibonacci sequence. The first and second terms of the Fibonacci sequence = 1.
For all following terms n, Fibonacci(n) = Fibonacci(n-1) + Fibonacci(n-2); i.e., the value of
a term in the Fibonacci sequence is equal to the sum of the previous two terms. The
Fibonacci sequence is: 1, 1, 2, 3, 5, 8, 13, 21, 34, …

2. Write a C++ program that finds and displays the first n prime numbers where n is given by
the user (via the keyboard).

You might also like