CS-114 Report - 4
CS-114 Report - 4
Programming
Lab Description:
A loop is part of a program that repeats. The while loop has two important
parts: (1) an expression that is tested for a true or false value, and (2) a
statement or block that is repeated as long as the expression is true.
Task 01:
Write a C++ program to print all natural numbers from 1 to n using while loop;
where n is the number input by user. Attempt the same question again but this
time, make use of for loop. Apply the break point in the start of loops and run
the code line by line till the loops terminate to develop better understanding.
2
Task 02:
Write a C++ program that computes the sum of first n positive integers: sum =
1 + 2 + 3 + … + n; where n is the number entered by user. Make sure the
program terminates if the user enters a negative number. Try practicing both
loops.
3
Task 03:
Write a do-while loop that asks the user to enter two numbers. The numbers
should be added and the sum displayed. The user should be asked if he or
she wishes to perform the operation again. If so, the loop should repeat;
otherwise it should terminate.
4
Task 04:
Write a C++ program that takes a number as an input from user and prints the
table of that number. Improve the program so that it now has the ability to
print the table up to any given range. Which loop should be used and why?
5
Task 05:
Write a C++ program to display the n number of terms of odd natural number
and their sum.
Task 06:
Write a program in C++ which prints the numbers from 1 to 150. But, multiples
of 5 should be replaced with "Fizz", multiples of 7 should be replaced with
"Buzz" and multiples of both 5 and 7 should be replaced with "FizzBuzz".