- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# 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 Questions & Answers
- Golang Program to Print Odd Numbers Within a Given Range
- Python - Find the number of prime numbers within a given range of numbers
- 8086 program to find sum of odd numbers in a given series
- PHP program to find the sum of cubes of natural numbers that are odd
- 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
- How to find Kaprekar numbers within a given range using Python?
- Finding sum of all numbers within a range in JavaScript
- Program to find count of numbers having odd number of divisors in given range in C++
- Program to find the sum of first n odd numbers in Python
- Find numbers with K odd divisors in a given range in C++
- Python Program to Find All Numbers which are Odd and Palindromes Between a Range of Numbers
- Program to find sum of first N odd numbers in Python
- Program to find bitwise AND of range of numbers in given range in Python
- Python program to generate random numbers within a given range and store in a list?

Advertisements