+92 332 4229 857 99ProjectIdeas@Gmail.com

Product of N odd numbers (C++)


Product of N odd numbers
productOddNums() calculates the product of N odd numbers between a specified range. It takes 2 parameter:
a is the lower limit of the specified range of odd numbers.
b is the upper limit of the specified range of odd numbers.
It returns the product of N odd numbers if both the numbers given to the function are odd, if any parameter to productOddNums() is even then this function will return a 0.

Code

#include "stdafx.h"
#include "iostream"
#include "conio.h"
#include "iomanip"
#include "math.h"

using namespace std;

int productOddNums(int a,int b)
{
int var=1;

 if((a%2==0)||(b%2==0))
   return 0;
 else
 {
  for(int i=a;i<=b;i=i+2)
  var=var*i;
  return var;
 }
}

int main()
{
       cout<< productOddNums( 1 , 3 );cout<<endl;
       cout<< productOddNums( 1 , 4 );cout<<endl;
       cout<< productOddNums( 1 , 5 );cout<<endl;
       cout<< productOddNums( 1 , 6 );cout<<endl;
       cout<< productOddNums( 1 , 7 );cout<<endl;
       cout<< productOddNums( 1 , 8 );cout<<endl;
       cout<< productOddNums( 1 , 9 );cout<<endl;
      
       _getche();
       return 0;
}

Output
3
0
15
0
105
0
945

0 comments: