Loops
Loops
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.
int main()
{
double price, quantity, totalCost = 0;
while (true) {
cout << "Enter the price of the item: ";
cin >> price;
if (price < 0) {
break;
}
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;
totalGrade += grade;
}
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;
totalTemperature += temperature;
totalHumidity += humidity;
}
double averageTemperature = totalTemperature / 5;
double averageHumidity = totalHumidity / 5;
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.