C++ Loops Practice Questions with Solutions

Loops are one of the most powerful features of C++ programming. They allow you to execute a block of code repeatedly without writing the same statements multiple times. Instead of manually repeating instructions, loops automate repetitive tasks, making programs shorter, faster, and easier to maintain.

In C++, there are three main types of loops:

  • for loop – Best when the number of iterations is known.
  • while loop – Executes as long as a condition remains true.
  • do...while loop – Executes the loop body at least once before checking the condition.

Loops are widely used in competitive programming, software development, game development, data processing, automation, and algorithm design. They are also essential for mastering arrays, strings, functions, and object-oriented programming. C++ Loops practice questions with solutions help to build concepts.

In this chapter, you’ll solve practical looping problems that improve your understanding of iteration, counters, conditions, and repeated execution.

Each question includes:

  • Problem Statement
  • Complete C++ Solution
  • Sample Input
  • Sample Output
  • Explanation
  • Concepts Covered

Let’s begin with some beginner-friendly C++ loop practice questions.


1. C++ Program to Print Numbers from 1 to 10

Problem Statement

Write a C++ program to print numbers from 1 to 10 using a for loop.

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    for (int i = 1; i <= 10; i++)
    {
        cout << i << " ";
    }

    return 0;
}

Sample Output

1 2 3 4 5 6 7 8 9 10

Explanation

The loop starts from 1, increments by 1 after each iteration, and stops after printing 10.

Concepts Covered

  • for Loop
  • Loop Counter
  • Increment Operator
  • Iteration

2. C++ Program to Print Numbers from 10 to 1

Problem Statement

Write a C++ program to print numbers from 10 to 1 in reverse order.

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    for (int i = 10; i >= 1; i--)
    {
        cout << i << " ";
    }

    return 0;
}

Sample Output

10 9 8 7 6 5 4 3 2 1

Explanation

The loop starts from 10, decreases by 1 after every iteration, and stops at 1.

Concepts Covered

  • Reverse Loop
  • Decrement Operator
  • for Loop

3. C++ Program to Print Even Numbers from 1 to 100

Problem Statement

Write a C++ program to print all even numbers between 1 and 100.

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    for (int i = 2; i <= 100; i += 2)
    {
        cout << i << " ";
    }

    return 0;
}

Sample Output

2 4 6 8 10 12 14 16 18 20 ...

Explanation

The loop starts from 2 and increments by 2, ensuring only even numbers are printed.

Concepts Covered

  • Increment by 2
  • Even Numbers
  • for Loop

4. C++ Program to Print Odd Numbers from 1 to 100

Problem Statement

Write a C++ program to print all odd numbers between 1 and 100.

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    for (int i = 1; i <= 100; i += 2)
    {
        cout << i << " ";
    }

    return 0;
}

Sample Output

1 3 5 7 9 11 13 15 17 19 ...

Explanation

Since every odd number differs by 2, the loop increases by 2 after each iteration.

Concepts Covered

  • Odd Numbers
  • Loop Increment
  • Iteration

5. C++ Program to Calculate the Sum of First N Natural Numbers

Problem Statement

Write a C++ program to calculate the sum of the first N natural numbers.

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    int n;
    int sum = 0;

    cout << "Enter the value of N: ";
    cin >> n;

    for (int i = 1; i <= n; i++)
    {
        sum += i;
    }

    cout << "Sum = " << sum;

    return 0;
}

Sample Input

Enter the value of N: 10

Sample Output

Sum = 55

Explanation

The loop starts from 1, adds each number to the variable sum, and continues until N.

For N = 10:

1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55

Concepts Covered

  • Accumulator Variable
  • for Loop
  • Arithmetic Operations
  • Natural Numbers

6. C++ Program to Find the Sum of Even Numbers from 1 to N

Problem Statement

Write a C++ program to calculate the sum of all even numbers between 1 and N.

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    int n;
    int sum = 0;

    cout << "Enter the value of N: ";
    cin >> n;

    for (int i = 2; i <= n; i += 2)
    {
        sum += i;
    }

    cout << "Sum of Even Numbers = "
         << sum;

    return 0;
}

Sample Input

Enter the value of N: 10

Sample Output

Sum of Even Numbers = 30

Explanation

The loop starts from 2 and increases by 2 after every iteration, ensuring that only even numbers are added.

Calculation:

2 + 4 + 6 + 8 + 10 = 30

Concepts Covered

  • for Loop
  • Even Numbers
  • Accumulator Variable
  • Arithmetic Operations

7. C++ Program to Find the Sum of Odd Numbers from 1 to N

Problem Statement

Write a C++ program to calculate the sum of all odd numbers between 1 and N.

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    int n;
    int sum = 0;

    cout << "Enter the value of N: ";
    cin >> n;

    for (int i = 1; i <= n; i += 2)
    {
        sum += i;
    }

    cout << "Sum of Odd Numbers = "
         << sum;

    return 0;
}

Sample Input

Enter the value of N: 9

Sample Output

Sum of Odd Numbers = 25

Explanation

The loop starts from 1 and increments by 2, ensuring only odd numbers are included.

Calculation:

1 + 3 + 5 + 7 + 9 = 25

Concepts Covered

  • Odd Numbers
  • Loop Increment
  • Arithmetic Operations

8. C++ Program to Print the Multiplication Table of a Number

Problem Statement

Write a C++ program to print the multiplication table of a given number up to 10.

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    int number;

    cout << "Enter a number: ";
    cin >> number;

    for (int i = 1; i <= 10; i++)
    {
        cout << number
             << " x "
             << i
             << " = "
             << number * i
             << endl;
    }

    return 0;
}

Sample Input

Enter a number: 7

Sample Output

7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70

Explanation

The loop runs from 1 to 10, multiplying the entered number with the loop counter during each iteration.

Concepts Covered

  • Multiplication Table
  • for Loop
  • Loop Counter
  • Arithmetic Operators

9. C++ Program to Find the Factorial of a Number

Problem Statement

Write a C++ program to calculate the factorial of a positive integer.

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    int number;
    long long factorial = 1;

    cout << "Enter a positive number: ";
    cin >> number;

    for (int i = 1; i <= number; i++)
    {
        factorial *= i;
    }

    cout << "Factorial = "
         << factorial;

    return 0;
}

Sample Input

Enter a positive number: 5

Sample Output

Factorial = 120

Explanation

Factorial is calculated by multiplying every integer from 1 to N.

Example:

5! = 5 × 4 × 3 × 2 × 1 = 120

Concepts Covered

  • Factorial
  • Multiplication
  • Loop Counter
  • Accumulator Variable

10. C++ Program to Print Numbers from N to 1 Using While Loop

Problem Statement

Write a C++ program to print numbers from N down to 1 using a while loop.

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    int n;

    cout << "Enter the value of N: ";
    cin >> n;

    while (n >= 1)
    {
        cout << n << " ";
        n--;
    }

    return 0;
}

Sample Input

Enter the value of N: 8

Sample Output

8 7 6 5 4 3 2 1

Explanation

The while loop executes as long as the condition n >= 1 remains true. After each iteration, the value of n decreases by 1.

Concepts Covered

  • while Loop
  • Reverse Counting
  • Loop Condition
  • Decrement Operator

11. C++ Program to Count the Number of Digits in an Integer

Problem Statement

Write a C++ program to count the total number of digits in a given integer.

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    int number;
    int count = 0;

    cout << "Enter a number: ";
    cin >> number;

    while (number != 0)
    {
        number = number / 10;
        count++;
    }

    cout << "Total Digits = "
         << count;

    return 0;
}

Sample Input

Enter a number: 98765

Sample Output

Total Digits = 5

Explanation

The program repeatedly divides the number by 10 until it becomes 0. Each division removes one digit, and the counter keeps track of the total digits.

Concepts Covered

  • while Loop
  • Integer Division
  • Counter Variable
  • Number Processing

12. C++ Program to Calculate the Sum of Digits of a Number

Problem Statement

Write a C++ program to calculate the sum of all digits of an integer.

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    int number;
    int digit;
    int sum = 0;

    cout << "Enter a number: ";
    cin >> number;

    while (number != 0)
    {
        digit = number % 10;
        sum += digit;
        number /= 10;
    }

    cout << "Sum of Digits = "
         << sum;

    return 0;
}

Sample Input

Enter a number: 12345

Sample Output

Sum of Digits = 15

Explanation

Each digit is extracted using the modulus (%) operator and added to the variable sum.

Calculation:

1 + 2 + 3 + 4 + 5 = 15

Concepts Covered

  • while Loop
  • Modulus Operator
  • Digit Extraction
  • Accumulator Variable

13. C++ Program to Reverse a Number

Problem Statement

Write a C++ program to reverse the digits of a given integer.

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    int number;
    int reverse = 0;

    cout << "Enter a number: ";
    cin >> number;

    while (number != 0)
    {
        reverse = reverse * 10 + number % 10;
        number /= 10;
    }

    cout << "Reversed Number = "
         << reverse;

    return 0;
}

Sample Input

Enter a number: 12345

Sample Output

Reversed Number = 54321

Explanation

The last digit is extracted using % 10 and appended to the reversed number during each iteration.

Concepts Covered

  • while Loop
  • Number Manipulation
  • Modulus Operator
  • Integer Division

14. C++ Program to Check Whether a Number is a Palindrome

Problem Statement

Write a C++ program to determine whether a number is a palindrome.

A palindrome number remains the same when its digits are reversed.

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    int number;
    int original;
    int reverse = 0;

    cout << "Enter a number: ";
    cin >> number;

    original = number;

    while (number != 0)
    {
        reverse = reverse * 10 + number % 10;
        number /= 10;
    }

    if (original == reverse)
    {
        cout << "Palindrome Number";
    }
    else
    {
        cout << "Not a Palindrome Number";
    }

    return 0;
}

Sample Input

Enter a number: 1221

Sample Output

Palindrome Number

Explanation

The program reverses the entered number and compares it with the original number. If both are equal, the number is a palindrome.

Concepts Covered

  • while Loop
  • Number Reversal
  • if…else
  • Integer Processing

15. C++ Program to Check Whether a Number is an Armstrong Number

Problem Statement

Write a C++ program to determine whether a three-digit number is an Armstrong number.

An Armstrong number is equal to the sum of the cubes of its digits.

Example:

153 = 1³ + 5³ + 3³ = 153

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    int number;
    int original;
    int digit;
    int sum = 0;

    cout << "Enter a number: ";
    cin >> number;

    original = number;

    while (number != 0)
    {
        digit = number % 10;
        sum += digit * digit * digit;
        number /= 10;
    }

    if (sum == original)
    {
        cout << "Armstrong Number";
    }
    else
    {
        cout << "Not an Armstrong Number";
    }

    return 0;
}

Sample Input

Enter a number: 153

Sample Output

Armstrong Number

Explanation

Each digit is extracted, cubed, and added to the total. If the final sum equals the original number, it is an Armstrong number.

Concepts Covered

  • while Loop
  • Digit Extraction
  • Number Processing
  • Conditional Statements
  • Armstrong Number Logic

Chapter Summary

In this chapter, you learned how loops simplify repetitive tasks in C++ programming. You practiced using for and while loops to print sequences, calculate sums, generate multiplication tables, compute factorials, reverse numbers, count digits, and solve classic number-based problems like palindrome and Armstrong numbers. Mastering loops is essential because they form the foundation for arrays, strings, functions, searching, sorting, and many advanced algorithms.


Key Takeaways

  • Loops execute a block of code repeatedly until a condition becomes false.
  • for loops are ideal when the number of iterations is known.
  • while loops are useful when the number of iterations depends on a condition.
  • Accumulator variables help calculate sums and products efficiently.
  • Digit extraction is commonly performed using the modulus (%) operator.
  • Integer division (/) helps remove digits one by one.
  • Loops are widely used in mathematical and number-processing programs.
  • Nested loops are commonly used for pattern printing.
  • Efficient use of loops reduces code duplication.
  • Loops are one of the most important concepts in C++ programming and competitive coding.

Frequently Asked Questions (FAQs)

1. What is a loop in C++?

A loop repeatedly executes a block of code until a specified condition becomes false.


2. What are the different types of loops in C++?

The three main loops are:

  • for
  • while
  • do...while

3. When should I use a for loop?

Use a for loop when the number of iterations is known in advance.


4. What is the difference between while and do...while?

A while loop checks the condition before execution, whereas a do...while loop executes the body at least once before checking the condition.


5. Why is the modulus operator (%) important in loop programs?

It helps extract the last digit of a number and is commonly used in digit-based problems such as reversing numbers, palindrome checks, and Armstrong numbers.


6. What is an accumulator variable?

An accumulator variable stores a running total during loop execution, such as the sum of numbers or the product for factorial calculation.


7. Can loops be nested in C++?

Yes. One loop can be placed inside another loop. Nested loops are commonly used for pattern printing and matrix operations.


8. Why are loops important for beginners?

Loops eliminate repetitive code, improve efficiency, and are required for solving arrays, strings, searching, sorting, mathematical problems, and competitive programming challenges.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top