- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures

- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces

# Check if a number can be written as sum of three consecutive integers in C++

In this section we will see, whether a number can be represented as tree consecutive numbers or not. Suppose a number is 27. This can be represented as 8 + 9 + 10.

This can be solved in two different approach. The first approach is Naïve approach. In that approach, we have to check i + (i + 1) + (i + 2) is same as number or not. Another efficient approach is by checking whether the number is divisible by 3 or not. Suppose a number x can be represented by three consecutive 1s, then x = (y - 1) + y + (y + 1) = 3y. So the number must be divisible by 3.

## Example

#include <iostream> using namespace std; bool hasThreeNums(int n) { if(n % 3 == 0){ return true; } return false; } int main() { int num = 27; if(hasThreeNums(num)){ cout << "Can be represented"; }else{ cout << "Cannot be presented"; } }

## Output

Can be represented

- Related Articles
- Check if a number can be expressed as a sum of consecutive numbers in C++
- Program to check a number can be written as a sum of distinct factorial numbers or not in Python
- Check if N can be represented as sum of integers chosen from set {A, B} in Python
- The sum of three consecutive integers is (-87). Find the three integers.
- Check if a number can be represented as a sum of 2 triangular numbers in C++
- Check if a number can be expressed as sum two abundant numbers in C++
- Check if a prime number can be expressed as sum of two Prime Numbers in Python
- Check if a number can be represented as sum of non zero powers of 2 in C++
- Check if a number can be expressed as power in C++
- Check if a number can be expressed as a^b in C++
- Check if a number can be expressed as a^b in Python
- Find if n can be written as product of k numbers in C++
- Check if an integer can be expressed as a sum of two semi-primes in Python
- Check if array sum can be made K by three operations on it in Python
- Check if a number can be expressed as 2^x + 2^y in C++

Advertisements