## C++ Loop Examples

### C++ program to print all odd numbers from 1 to N

Odd number

Opposite of even numbers, odd numbers are having a difference of 3 unit or number. In other words, if the number is not completely divisible by 2 then it is an odd number.

Logic

The logic for printing odd numbers are pretty simple and straight forward, we only need to check if the number is not divisible by 2. If the number is not divisible by 2, then we have to print it.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include <iostream> using namespace std; int main() { int i, n; // Take input from user. cout << "Print all odd numbers till : "; cin >> n; cout << endl << "Odd numbers from 1 to " << n << " are : " << endl; for(i = 1; i <= n; i++) { // Check for odd or not. if((i % 2) != 0) { cout << i << " "; } } return 0; } |

Output

1 2 3 4 5 |
Print all odd numbers till : 50 Odd numbers from 1 to 50 are : 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 |

## C++ program to calculate sum of first N odd numbers

Odd number

The Opposite of even numbers. Odd numbers have a difference of 3 unit or number. In other words, if the number is not completely divisible by 2 then it is an odd number.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include <iostream> using namespace std; int main() { int i, n, sum = 0; // Take input from user. cout << "Print sum of odd numbers till : "; cin >> n; for(i = 1; i <= n; i++) { // Check for odd or not. if((i % 2) != 0) { sum += i; } } cout << endl << "Sum of odd numbers from 1 to " << n << " is : " << sum; return 0; } |

Output

1 2 3 4 |
Print sum of odd numbers till : 50 Sum of odd numbers from 1 to 50 is : 625 |

### C++ program to print all even numbers from 1 to N

Even number

Even number are numbers that have a difference of 2 unit or number. In other words, if the number is completely divisible by 2 then it is an even number.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include <iostream> using namespace std; int main() { int i, n; // Take input from user. cout << "Print all even numbers till : "; cin >> n; cout << endl << "Even numbers from 1 to " << n << " are : " << endl; for(i = 1; i <= n; i++) { // Check for even or not. if((i % 2) == 0) { cout << i << " "; } } return 0; } |

1 2 3 4 5 |
Print all even numbers till : 50 Even numbers from 1 to 50 are : 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 |

### C++ program to calculate sum of first N even numbers

Even number

Even numbers are numbers that have a difference of 2 unit or number. In other words, if the number is completely divisible by 2 then it is an even number.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include <iostream> using namespace std; int main() { int i, n, sum = 0; // Take input from user. cout << "Print sum of even numbers till : "; cin >> n; for(i = 1; i <= n; i++) { // Check for even or not. if((i % 2) == 0) { sum += i; } } cout << endl << "Sum of even numbers from 1 to " << n << " is : " << sum; return 0; } |

Output

1 2 3 4 |
Print sum of even numbers till : 100 Sum of even numbers from 1 to 100 is : 2550 |

### C++ program to print first N natural numbers using for loop

Natural number

Natural numbers are numbers that are common and clearly in nature. As such, it is a whole, nonnegative number.

Logic

To print the first N natural number we only have to run one single loop from 1 to N.

After taking input (num) from user start one loop from 1 to num, and then inside the loop simply print the current variable “i”.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream> using namespace std; int main() { int i, num; // Take number from user cout << "Enter any number : "; cin >> num; cout << endl << "Natural numbers from 1 to " << num << endl; for(i = 1; i <= num; i++) { cout << i << " "; } return 0; } |

Output

1 2 3 4 |
Enter any number : 10 Natural numbers from 1 to 10 1 2 3 4 5 6 7 8 9 10 |

### C++ program to calculate sum of first N natural numbers

Natural number

Natural numbers are numbers that are common and clear in nature. As such, it is a whole, non-negative number.

Logic

To print the sum of first N natural numbers, we need to run one loop from 1 to N, and each time inside the loop we have to add / sum value of “i” (current number) into one temporary variable.

Once the loop is over, outside of the loop we have to print a temporary variable containing the sum of natural numbers.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <iostream> using namespace std; int main() { int i, num, sum = 0; // Take number from user cout << "Enter any number : "; cin >> num; for(i = 1; i <= num; i++) { sum += i; } cout << endl << "Sum of first " << num << " natural numbers : " << sum; return 0; } |

Output

1 2 3 4 |
Enter any number : 50 Sum of first 50 natural numbers = 1275 |

### C++ program to print all prime numbers between 1 to N

Prime number

A prime number is an integer greater than 1 whose only factors are 1 and itself. A factor is an integer that can be divided evenly into another number.

Logic

To print all the prime numbers up to N, we start one loop from 2 to N and then inside the loop we check current number or “num” is prime or not. To check if it is prime or not we again need one nested loop. It is not an efficient way to check prime number but it is simpler to understand the basic of looping in C++.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#include <iostream> using namespace std; int main() { int num, i, upto; // Take input from user cout << "Find prime numbers upto : "; cin >> upto; cout << endl << "All prime numbers upto " << upto << " are : " << endl; for(num = 2; num <= upto; num++) { for(i = 2; i <= (num / 2); i++) { if(num % i == 0) { i = num; break; } } // If the number is prime then print it. if(i != num) { cout << num << " "; } } return 0; } |

Output

1 2 3 4 5 |
Find prime numbers upto : 100 All prime numbers upto 100 are : 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 |

### C++ program to find sum of prime numbers between 1 to N

Prime number

A prime number is an integer greater than 1 whose only factors are 1 and itself. A factor is an integer that can be divided evenly into another number.

Logic

To print the sum of all prime numbers up to N we have to iterate through each number up to the given number and check if the number is a prime or not if it is a prime number then simply sum it or add it in one temporary variable.

Once the outer loop is completed we have to print that temporary variable containing the sum of primes.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#include <iostream> using namespace std; int main() { int num, i, upto, sum = 0; // Take input from user cout << "Find sum of prime numbers upto : "; cin >> upto; for(num = 2; num <= upto; num++) { for(i = 2; i <= (num / 2); i++) { if(num % i == 0) { i = num; break; } } // If the number is prime then add it. if(i != num) { sum += num; } } cout << endl << "Sum of all prime numbers upto " << upto << " : " << sum; return 0; } |

1 2 3 4 |
Find sum of prime numbers upto : 50 Sum of all prime numbers upto 50 : 326 |

### C++ program to find all factors of a number

Factor

A factor is an integer that can be divided evenly into another number or in other words factors of a number are numbers that multiply to form a product.

Logic

To print all the factors of a particular number we have to iterate through all the smaller numbers up to the given number. If the user given number is completely divisible by any number then it is a factor of that number.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <iostream> using namespace std; int main() { int i, num; // Take input from user cout << "Enter any number : "; cin >> num; cout << endl << "All factors of " << num << " are : " << endl; for(i = 1; i <= num; i++) { if(num % i == 0) { cout << i << " "; } } return 0; } |

Output

1 2 3 4 5 |
Enter any number : 50 All factors of 50 are : 1 2 5 10 25 50 |

### C++ program to print alphabets from a to z

In this program, we are going to see how we can print alphabets in C++. The program is pretty simple and straight forward. Here ASCII value of the characters comes into the picture, we use the ASCII value of starting and end character to run the loop.

Logic

ASCII value of small letter “a” is 97 and for “z” it is 122, so we run the loop from 97 to 122 and then inside the loop we have to print the current character.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> using namespace std; int main() { char c; cout << "Alphabets from a - z are : " << endl; // Start from ASCII value of small letter a. for(c = 'a'; c <= 'z'; c++) { cout << c << " "; } return 0; } |

Output

1 2 3 4 |
Alphabets from a - z are: a b c d e f g h i j k l m n o p q r s t u v w x y z |

### C++ program to check whether a given number is perfect number or not

Perfect number

A perfect number is a positive integer that is equal to the sum of its proper divisors. The smallest perfect number is 6, which is the sum of 1, 2, and 3.

Logic

To check if the number is perfect or not we have to run one loop from 1 to N and sum all the numbers between 1 to N, if the sum is equal to N then it is a perfect number.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#include <iostream> using namespace std; int main() { int i, num, sum = 0; // Take input from user cout << "Enter any number : "; cin >> num; // Calculate sum of all proper divisors for(i = 1; i < num; i++) { if(num % i == 0) { sum += i; } } // Check whether the sum of divisors is equal to num if(sum == num) { cout << endl << num << " is PERFECT NUMBER"; } else { cout << endl << num << " is NOT PERFECT NUMBER"; } return 0; } |

Output

1 2 3 4 |
Enter any number : 5 5 is NOT PERFECT NUMBER |

### C++ program to check whether a given number is Armstrong number or not

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
#include <iostream> #include <math.h> using namespace std; int main() { int originalNum, num, lastDigit, digits, sum; // Take number from user cout << "Enter number to check : "; cin >> num; sum = 0; originalNum = num; // Find total digits in given number digits = (int) log10(num) + 1; while(num > 0) { lastDigit = num % 10; sum = sum + round(pow(lastDigit, digits)); // Remove the last digit num = num / 10; } // Check for armstrong or not if(originalNum == sum) { cout << endl << originalNum << " is ARMSTRONG NUMBER"; } else { cout << endl << originalNum << " is NOT ARMSTRONG NUMBER"; } return 0; } |

1 2 3 4 |
Enter number to check : 1634 1634 is ARMSTRONG NUMBER |

### C++ program to check whether a number is palindrome or not

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#include <iostream> using namespace std; int main() { int num, temp, reverse = 0; // Take input from user cout << "Enter any number : "; cin >> num; temp = num; while(temp != 0) { reverse = (reverse * 10) + (temp % 10); temp /= 10; } // Check for palindrome if(reverse == num) { cout << endl << num << " is a palindrome."; } else { cout << endl << num << " is not a palindrome."; } return 0; } |

1 2 3 4 |
Enter any number : 121 121 is a palindrome. |

### C++ program to check whether a number is prime number or not

### C++ program to count number of digits in a given integer

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#include <iostream> using namespace std; int main() { int num, temp; int count = 0; // Take input from user cout << "Enter any number : "; cin >> num; // Store to temporary variable. temp = num; while(temp != 0) { // Increment counter count++; // Remove last digit of 'temp' temp /= 10; } cout << endl << "Total digits in " << num << " : " << count; return 0; } |

1 2 3 4 |
Enter any number : 123456 Total digits in 123456 : 6 |

### C++ program to find product of digits in a number

### C++ program to find sum of all digits in a number

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include <iostream> using namespace std; int main() { int num, temp, sum = 0; // Take numbers from user cout << "Enter any number : "; cin >> num; // Store to temporary variable. temp = num; while(temp != 0) { sum += temp % 10; // Remove last digit of 'temp' temp /= 10; } cout << endl << "Sum of all digits in " << num << " : " << sum; return 0; } |

1 2 3 4 |
Enter any number : 123456 Sum of all digits in 123456 : 21 |

### C++ program to calculate factorial of a number

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <iostream> using namespace std; int main() { int i, num; int fact = 1; // Take numbers from user cout << "Enter any number : "; cin >> num; for(i = 1; i <= num; i++) { fact = fact * i; } cout << endl << "Factorial of " << num << " : " << fact; return 0; } |

1 2 3 4 |
Enter any number : 5 Factorial of 5 : 120 |

## C++ program to find HCF of two user given numbers

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#include <iostream> using namespace std; int main() { int i, num1, num2, min, hcf=1; // Take two numbers from user cout << "Enter two numbers : "; cin >> num1 >> num2; // Find minimum between two numbers min = num1; if(num2 < num1) { min = num2; } for(i = 1; i <= min; i++) { if((num1 % i) == 0 && (num2 % i) == 0) { hcf = i; } } cout << endl << "HCF of " << num1 << " and " << num2 << " : " << hcf; return 0; } |

1 2 3 4 |
Enter two numbers : 12 30 HCF of 12 and 30 : 6 |

### C++ program to find LCM of two user given numbers

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#include <iostream> using namespace std; int main() { int i, num1, num2, max, lcm=1; // Take two numbers from user cout << "Enter any two number : "; cin >> num1 >> num2; // Find the max number max = (num1 > num2) ? num1 : num2; i = max; while(1) { if((i % num1) == 0 && (i % num2) == 0) { lcm = i; break; } i += max; } cout << endl << "LCM of " << num1 << " and " << num2 << " : " << lcm; return 0; } |

1 2 3 4 |
Enter any two number : 6 24 LCM of 6 and 24 : 24 |

### C++ program to calculate power of a number using for loop

Power of a number

The power of a number represents the number of times to use that number in a multiplication. Usually, power is represented with a base number and an exponent.

Logic

Let’s declare one temporary variable *power* with value 1, we have used the data-type *long long* so that it can hold a big long value.

To calculate the power of a number we need a base and an exponent value, we are taking these two values from the user, after taking input (base, exponent) from the user we start one loop from 1 to *exponent.*

Inside the loop, for every iteration, we multiply the base by power (base * power) and store the result again in variable *power *until the loop is completed.

Once the loop is over, simply print the variable *power *containing the resultant power value of a given number.

Output

1 2 3 4 |
Enter base and exponent : 2 5 Result : 2 ^ 5 = 32 |

### C++ program to find reverse of any number

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include <iostream> using namespace std; int main() { int num, temp, reverse = 0; // Take numbers from user cout << "Enter any number : "; cin >> num; temp = num; while(temp != 0) { reverse = (reverse * 10) + (temp % 10); temp /= 10; } cout << endl << "Reverse of " << num << " : " << reverse; return 0; } |

1 2 3 4 |
Enter any number : 123456 Reverse of 123456 : 654321 |

### C++ program to print multiplication table of a any given number

## Add comment