C++

💻 while & for Loop Examples in C++23 min read

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.

Output

 

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.

Output

 

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.

Output
 

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.

Output

 

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”.

Output

 

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.

Output

 

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++.

Output

 

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.

 

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.

Output

 

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.

Output

 

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.

Output

 

Output
 

Output
 

Output
 

Output
 

Output
 

Output
 

Output
 

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

 

Output
 

 

Leave a Comment