Lab 5
Lab 5
Lab 5
While loop:
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.
For loop:
A for loop is a repetition control structure that allows you to efficiently write a loop that needs
to execute a specific number of times. When you know exactly how many times you want to
loop through a block of code, use the for loop instead of a while loop.
Syntax
Example
int main (){
}
Other Forms of the Update Expression :
You are not limited to using increment statements in the update expression. Here is a loop that
displays all the even numbers from 2 through 100 by adding 2 to its counter:
1. Grocery Shopping List and Bill Calculator. The program should accomplish the
following tasks:
Display a menu of grocery items with their respective prices.
Ask the user to choose items they want to buy from the menu. For each selected
item, prompt the user to enter the quantity they wish to purchase. Ensure that the
quantity entered is greater than 0.
Then display the final bill amount.
2. Write a C++ program to input basic salary of an employee and calculate its Gross salary
according to following:
Basic Salary <= 10000 : HRA = 20%, DA = 80%
Basic Salary <= 20000 : HRA = 25%, DA = 90%
Basic Salary > 20000 : HRA = 30%, DA = 95%
3. Program to print natural numbers in reverse from n to 1 using while loop.
Note: check whether the number is positive or not.
4. Program to Generate Fibonacci Sequence up to a Certain Number. The Fibonacci
sequence is a series of numbers where a number is found by adding up the two
numbers before it. Starting with 0 and 1, the sequence goes 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,
and so forth.
5. Write a method with a while loop that computes the sum of first n positive integers: sum
= 1 + 2 + 3 + … + n.
6. Write a C++ program to calculate the approximate values of sin(x) and cos(x) using the
Taylor series expansion. You should allow the user to input the value of 'x' and the
number of terms in the series to use. The program should then display the calculated
values of sin(x) and cos(x).