Sunday, 16 October 2011
Pascal Triangle Programme
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MINROWS 1
#define MAXROWS 12
int getInputInRange(int,int);
int n_choose_r(int,int);
int factorial(int);
void printRow (int);
int main ( void )
{
int row = 0;
int numRows;
numRows = getInputInRange(MINROWS,MAXROWS);
while(row<numRows)
{
printRow(row);
printf("\n");
row++;
}
system("Pause");
return 0;
}
int getInputInRange(int min,int max)
{
int userInput;
printf("Please enter the integer value between %d and %d\n",min,max);
scanf_s("%d",&userInput);
while(userInput<min||userInput>max)
{
printf("Error:The integer value is not in the expected range\n\n");
printf("Please enter the integer value between %d and %d\n",min,max);
scanf_s("%d",&userInput);
}
return userInput;
}
void printRow(int rowNums)
{
int position = 0;
int coefficient;
while ( position <= rowNums)
{
coefficient = n_choose_r(rowNums,position);
printf("%d 4i",coefficient);
position++;
}
}
int n_choose_r(int n,int r)
{
int c;
c = factorial(n)/factorial(r)*factorial(n-r);
return c;
}
int factorial(int num)
{
int fact = 1;
while ( num > 1 )
{
fact*=num;
num --;
}
return fact;
}
- shoutul-humaira - 1649-
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment