Number expressed as sum of five consecutive integers | CPP Program to check if a number can be expressed as sum of five consecutive integers . ; function to check if a number can be expressed as sum of five consecutive integers . ; if n is 0 ; if n is positive , increment loop by 1. ; if n is negative , decrement loop by 1. ; Running loop from 0 to n - 4 ; check if sum of five consecutive integer is equal to n . ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void checksum ( int n ) { if ( n == 0 ) { cout << " - 2 ▁ - 1 ▁ 0 ▁ 1 ▁ 2" << endl ; return ; } int inc ; if ( n > 0 ) inc = 1 ; else inc = -1 ; for ( int i = 0 ; i <= n - 4 ; i += inc ) { if ( i + i + 1 + i + 2 + i + 3 + i + 4 == n ) { cout << i << " ▁ " << i + 1 << " ▁ " << i + 2 << " ▁ " << i + 3 << " ▁ " << i + 4 ; return ; } } cout << " - 1" ; } int main ( ) { int n = 15 ; checksum ( n ) ; return 0 ; }