sum of range of odd numbers calculator code example
Example 1: sum all odd fibonacci numbers
function sumFibs(num) {
var prevNumber = 0;
var currNumber = 1;
var result = 0;
while (currNumber <= num) {
if (currNumber % 2 !== 0) {
result += currNumber;
}
currNumber += prevNumber;
prevNumber = currNumber - prevNumber;
}
return result;
}
sumFibs(4);
Example 2: c++ sum of even and odd numbers
void sumEvenAndOdd(){
int n=0 , evenSum=0 , oddSum=0;
int numbers[Size];
cout<<"enters the number(N) of integers\n";
cin>>Size;
cout<<"Enter "<<Size<<" number/s:\n";
for(int i =0 ;i< Size ; ++i){
cin>>numbers[i];
}
for(int i =0 ;i<Size ; ++i){
if(numbers[i]%2 ==0){
evenSum += numbers[i];
}
else{
oddSum += numbers[i];
}
}
cout<<"the sum of even numbers is : "<<evenSum <<"\nthe sum of odd numbers is :"<<oddSum<<endl;
}