- 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

# Count ways to express ānā as sum of odd integers in C++

Given an integer n as input. The goal is to find the number of ways in which we can represent ‘n’ as the sum of odd integers. For example, if n is 3 it can be represented as sum ( 1+1+1 ) and (3) so total 2 ways.

**For Example**

## Input

n=6

## Output

Count of ways to express ‘n’ as sum of odd integers are: 8

## Explanation

The ways in which we can express ‘n’ as sum of odd integers − 1. 1+1+1+1+1+1 2. 3+1+1+1 3. 1+3+1+1 4. 1+1+3+1 5. 1+1+1+3 6. 3+3 7. 1+5 8. 5+1

## Input

n=9

## Output

Count of ways to express ‘n’ as sum of odd integers are: 34

## Explanation

The some of the ways in which we can express ‘n’ as sum of odd integers: 1. 1+1+1+1+1+1+1+1+1 2. 3+3+3 3. 5+3+1 4. 7+1+1 5. ….and other such combinations

**Approach used in the below program is as follows** −

In this approach we will check the ways of representing a number as a sum of odd integers from previous numbers that are n−1th and n−2th numbers. Ways will be ways(n−1) + ways(n−2).

Take an integer n as input.

Function odd_ways(int n) takes a number and returns the count of ways to express ‘n’ as sum of odd integers.

Take an array arr of length n+1 to store count ways for representing numbers as sum of odd integers.

For number 0, there is no such way so set arr[0] with 0.

For number 1 there is only one way so set arr[1] with 1.

For the rest of numbers we can set arr[i] with arr[i−1]+arr[i−2] for i between 2 to n.

At the end we have arr[n] for the number of ways in which n is represented as the sum of odd integers.

Return arr[n] as result.

## Example

#include<iostream> using namespace std; int odd_ways(int n){ int arr[n+1]; arr[0] = 0; arr[1] = 1; for(int i = 2; i <= n; i++){ arr[i] = arr[i-1] + arr[i-2]; } return arr[n]; } int main(){ int n = 6; cout<<"Count of ways to express ‘n’ as sum of odd integers are: "<<odd_ways(n); return 0; }

## Output

If we run the above code it will generate the following output −

Count of ways to express ‘n’ as sum of odd integers are: 8

- Related Articles
- Count ways to express a number as sum of powers in C++
- Count of different ways to express N as the sum of 1, 3 and 4 in C++
- Count ways to express a number as sum of consecutive numbers in C++
- Ways to write N as sum of two or more positive integers in C++
- (i) Express 49 as the sum of 7 odd numbers.(ii) Express 121 as the sum of 11 odd numbers.
- Express 144 as sum of 12 odd numbers.
- Express an odd number as sum of prime numbers in C++
- Express the sum of 9 as an odd consecutive number.
- Express the following numbers as the sum of consecutive odd numbers: $36$.
- Express 18 as the sum of two prime numbers in all possible ways.
- Minimum number of palindromes required to express N as a sum using C++.
- Express the HCF of 468 and 222 as $468x + 222y$ where x, y are integers in two different ways.
- 8.(i) Express 49 as the sum of 7 odd numbers.(ii) Express 121 as the sum of 11 odd numbers.9. How many numbers lie between squares of the following numbers?(i) 12 and 13(ii) 25 and 26(iii) 99 and 100"\n
- Minimum numbers needed to express every integer below N as a sum in C++
- Squared sum of n odd numbers - JavaScript