
/*------------------------------------------------------*/
/*Program Homework7                                     */
/*                                                      */
/*This program converts temperature from degrees        */
/*Fahrenheit to degrees Celsius from 0-100 degrees      */
/*------------------------------------------------------*/

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int formula(int n);
int fac(int z);
int select(int n, int k);

int main(void)
{
  /*declare variables.*/
  int n;
  
   printf("The result is %d when n=2 \n", formula(2));
   printf("The result is %d when n=4 \n", formula(4));
   printf("The result is %d when n=6 \n", formula(6));
   printf("The result is %d when n=8 \n", formula(8));
   
   /* OR  */
   printf("With Loop\n");
   for(n=2; n<=8; n=n+2){
     printf("The result is %d when n=%d \n", formula(n), n);
   } 
  return 0;
}
/*
 * COMPUTE THE FORMULA GIVEN IN THE HW 7
 */
int formula(int n)
{
 int i, sum;
 
 sum=0;
 for(i=0; i<=n; i++)  {
     if (i%2==0) 
        sum = sum + (i+1) * select(n, i);
     else 
        sum = sum - (i+1) * select(n, i);
 }
 return sum;
}
/* 
 * compute the factorial of a number 
 */
int fac(int z)
{  
    int i;
    int res =1;
     for(i=1; i<=z; i++){
              res = res * i;
     }
     return res;
}                 

/*
 * compute the different ways of slecting k out of n elements
 */
int select(int n, int k)
{
  return fac(n)/(fac(k)*fac(n-k));
}

/*--------OUTPUT------------------------------------------------
The result is 0 when n=2
The result is 0 when n=4
The result is 0 when n=6
The result is 0 when n=8
With Loop
The result is 0 when n=2
The result is 0 when n=4
The result is 0 when n=6
The result is 0 when n=8

Press any key to continue . . .
--------------------------------------------------------------*/
