- Related Questions & Answers
- 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
- 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++
- 8086 program to find sum of Even numbers in a given series
- 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
- Python program to print all odd numbers in a range
- Java Program to create random BigInteger within a given range
- PHP program to find the sum of cubes of the first n natural numbers
- Average of odd numbers till a given odd number?
- Find a range of composite numbers of given length in C++
- PHP program to find the sum of the 5th powers of first n natural numbers
- C Program for Find sum of odd factors of a number?

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

To find the sum of odd numbers within a given range, the code is as follows −

<?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); ?>

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.

Advertisements