0% found this document useful (0 votes)
21 views

Loops

Uploaded by

tripleagoat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Loops

Uploaded by

tripleagoat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Loop Related Problems

Basic Questions:
1. Write a program to print numbers from 1 to 10.
2. Write a program to calculate the sum of the first 10 natural numbers.
3. Write a program that prompts the user to input a positive integer. It should then print the
multiplication table of that number.
4. Write a program to find the factorial value of any number entered through the keyboard.
5. Two numbers are entered through the keyboard. Write a program to find the value of one number
raised to the power of another.

Real-life Scenario-Based Questions:


1. A store owner wants to calculate the total cost of the items in a customer's shopping cart. Write a
program that asks the user to input the prices and quantities of different items in the cart, and then
calculate the total cost.
#include <iostream>
using namespace std;

int main()
{
double price, quantity, totalCost = 0;

while (true) {
cout << "Enter the price of the item: ";
cin >> price;

if (price < 0) {
break;
}

cout << "Enter the quantity of the item: ";


cin >> quantity;

totalCost += price * quantity;


}

cout << "Total cost: " << totalCost << endl;

return 0;
}

2. A school wants to calculate the average grade of 5 students in a class. Write a program that asks
the user to input the grades of 5 students in a class, and then calculate the average grade.
#include <iostream>
using namespace std;

int main()
{
int grade, numGrades = 5;
double totalGrade = 0;

for (int i = 1; i <= 5; i++) {


cout << "Enter the grade of student " << i << ": ";
cin >> grade;

totalGrade += grade;
}

double averageGrade = totalGrade / numGrades;

cout << "Average grade: " << averageGrade << endl;

return 0;
}

3. A person wants to calculate the total amount of money they have saved in a year. Write a program
that asks the user to input the amount of money they save each month, and then calculates the total
amount of money saved in a year.

4. A weather station wants to calculate the average temperature and humidity over a certain period (5
days). Write a program that asks the user to input the temperature and humidity readings for five
days and calculate rates for the average temperature and humidity.

#include <iostream>
using namespace std;

int main()
{
double temperature, humidity, totalTemperature = 0, totalHumidity =
0;

for (int i = 1; i <= 5; i++) {


cout << "Enter the temperature for day " << i << ": ";
cin >> temperature;
cout << "Enter the humidity for day " << i << ": ";
cin >> humidity;

totalTemperature += temperature;
totalHumidity += humidity;
}
double averageTemperature = totalTemperature / 5;
double averageHumidity = totalHumidity / 5;

cout << "Average temperature: " << averageTemperature << endl;


cout << "Average humidity: " << averageHumidity << endl;

return 0;
}

5. A company wants to calculate the total sales of its employees. Write a program that asks the user
to input the sales data of different employees, and then calculates the total sales for each employee
and the company.

Problem-Solving Questions:
6. Write a program that calculates the sum of all the even numbers between 1 and a given number.
For example, if the user inputs the number 10, the program should print out 30 (which is the sum
of 2+4+6+8+10).

7. Write a program that calculates the sum of the squares of all the odd numbers between 1 and a
given number. For example, if the user inputs the number 10, the program should print out 165
(which is the sum of 1+9+25+49+81).

8. Write a program that calculates the sum of all the numbers divisible by 3 between 1 and a given
number. For example, if the user inputs the number 9, the program should print out 18 (which is
the sum of 3+6+9).

9. Write a program that prints the first n terms of the series 1 , 1/2 , 1/3 , 1/4 ... , 1/n. The program
should ask the user to enter the value of n, and then use a loop to print the series.

10. Write a program that prints the first n terms of the series 1 + 1/4 + 1/9 + ... + 1/n^2. The program
should ask the user to enter the value of n, and then use a loop to print the series.

11. Write a program that prints the first n terms of the series 1 , 2 , 4 , 7 , 11, 16 … , n. (Difference
between two numbers increments by 1). The program should ask the user to enter the value of n,
and then use a loop to print the series.

12. Write a program that calculates the sum of the first n terms of the Fibonacci series 1, 1, 2, 3, 5, 8,
13, 21 ... n (next number is the addition of last two numbers.). The program should ask the user to
enter the value of n, and then use a loop to calculate the sum.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy