Loop Programs
C++ loop programs with for and while: tables, sums, factorials, digit tricks, GCD, primes, Fibonacci, and a switch calculator. Each one traced by hand, step by step.
14 programs · open one, then the next
Start with Multiplication Table- 01Multiplication TableBeginnerPrint the multiplication table of any number from 1 to 10 in C++ using a for loop. Shows how the loop counter changes and why <= 10 differs from < 10.Open example →cpp#include <iostream> using namespace std; int main() {
- 02Sum of n Natural NumbersBeginnerAdd the numbers 1 to n in C++ with a for loop and a running total. Compare the loop with the n(n+1)/2 formula and see what happens for n = 0 or a huge n.Open example →cpp#include <iostream> using namespace std; int main() {
- 03Factorial of a NumberBeginnerCompute the factorial of a number in C++ with a for loop and a long long result. Covers 0! = 1, rejecting negatives, and where even 64 bits overflow (past 20!).Open example →cpp#include <iostream> using namespace std; int main() {
- 04Reverse a Number Using a While LoopBeginnerReverse the digits of an integer in C++ with a while loop, % 10, and / 10. A loop-basics lesson that traces 1234 becoming 4321 one pass at a time.Open example →cpp#include <iostream> using namespace std; int main() {
- 05GCD of Two NumbersIntermediateFind the greatest common divisor of two numbers in C++ with the Euclidean algorithm. Traces 48 and 18 down to 6 and explains why a % b shrinks so quickly.Open example →cpp#include <iostream> using namespace std; int main() {
- 06LCM of Two NumbersIntermediateFind the least common multiple of two numbers in C++ by computing the GCD first and using a × b / GCD. Covers coprime pairs and the overflow risk in a × b.Open example →cpp#include <iostream> using namespace std; int main() {
- 07Palindrome CheckBeginnerCheck whether a number reads the same forwards and backwards in C++ by reversing its digits and comparing. Traces 121 and 123 and explains why 10 fails.Open example →cpp#include <iostream> using namespace std; int main() {
- 08Prime CheckBeginnerCheck whether a number is prime in C++ by testing divisors up to its square root. Explains why √n is enough, why 0 and 1 are not prime, and how break saves time.Open example →cpp#include <iostream> #include <cmath> using namespace std;
- 09Primes in RangeIntermediatePrint every prime number between two limits in C++ using a reusable isPrime function. Shows how a helper function keeps the loop short and when a sieve is better.Open example →cpp#include <iostream> #include <cmath> using namespace std;
- 10Armstrong NumberIntermediateCheck if a number is an Armstrong number in C++, where each digit raised to the digit count adds back to the number. Traces 153 and explains the two loops.Open example →cpp#include <iostream> #include <cmath> using namespace std;
- 11nth Fibonacci NumberBeginnerFind the nth Fibonacci number in C++ with a loop that keeps only the last two values. Traces position 10 giving 34 and warns about int overflow near position 47.Open example →cpp#include <iostream> using namespace std; int main() {
- 12Calculate the Power of a NumberBeginnerRaise a base to an exponent in C++ with pow from <cmath>, using double so fractions and negative powers work. Shows why ^ is XOR and when a plain loop is enough.Open example →cpp#include <iostream> #include <cmath> using namespace std;
- 13Factors of a Natural NumberBeginnerList all factors of a natural number in C++ by testing every divisor from 1 to n with %. Traces 24 and shows the faster square-root pairing trick for big numbers.Open example →cpp#include <iostream> using namespace std; int main() {
- 14Basic CalculatorBeginnerBuild a four-operation calculator in C++ with switch on a char operator. Covers break and fall-through, division by zero, and reporting errors with return 1.Open example →cpp#include <iostream> using namespace std; int main() {