
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
PHP program to find the sum of odd numbers within a given range
To find the sum of odd numbers within a given range, the code is as follows −
Example
<?php function odd_num_sum($val) { $entries = (int)($val + 1) / 2; $sum = $entries * $entries; return $sum; } function num_in_range($low, $high) { return odd_num_sum($high) - odd_num_sum($low - 1); } $low = 3; $high = 23; echo "The sum of odd natural numbers between the numbers 3 and 23 is ", num_in_range($low, $high); ?>
Output
The sum of odd natural numbers between the numbers 3 and 23 is 141.75
A function named ‘odd_num_sum’ is defined that computes the sum of odd numbers within a specific range of numbers. The function ‘num_in_range’ gives the range of values between two numbers that are passed as parameters to this function. Outside both the function, the range values are defined and this function ‘num_in_range’ is called by passing the low and high values as parameters. Relevant output is displayed on the console.
- Related Articles
- Golang Program to Print Odd Numbers Within a Given Range
- 8086 program to find sum of odd numbers in a given series
- Python - Find the number of prime numbers within a given range of numbers
- PHP program to find the numbers within a given array that are missing
- C++ program to find numbers with K odd divisors in a given range
- PHP program to find the sum of cubes of natural numbers that are odd
- How to find Kaprekar numbers within a given range using Python?
- Program to find count of numbers having odd number of divisors in given range in C++
- Finding sum of all numbers within a range in JavaScript
- Python Program to Find All Numbers which are Odd and Palindromes Between a Range of Numbers
- Python program to generate random numbers within a given range and store in a list?
- Java program to generate random numbers within a given range and store in a list
- Find numbers with K odd divisors in a given range in C++
- Program to find the sum of first n odd numbers in Python
- Program to find sum of first N odd numbers in Python

Advertisements