Monday, October 27, 2014

Display Pascal triangle

Display Pascal triangle
#include 
#include 
void main()
{
    int array[15][15], i, j, rows, num = 25, k;
    clrscr();
    printf("\n enter the number of rows:");
    scanf("%d", &rows);
    for (i = 0; i < rows; i++)
    {
        for (k = num - 2 * i; k >= 0; k--)
            printf(" ");
                for (j = 0; j <= i; j++)
                {
                if (j == 0 || i == j)
                        {
                    array[i][j] = 1;
                }
                else
                {
                    array[i][j] = array[i - 1][j - 1] + array[i - 1][j];
                        }
                        printf("%4d", array[i][j]);
            }
            printf("\n");
    } 
   getch();
}
 
 
 
 
 
 
Output
 
 
enter the number of rows:2
                             1
                           1   1
Calculate the Value of nPr
#include 
#include 
void main(void)
{
   printf("%d\n", fact(8));
   int n, r; 
   clrscr();
   printf("Enter value for n and r\n");
   scanf("%d%d", &n, &r);
   int npr = fact(n) / fact(n - r);
   printf("\n Permutation values is = %d", npr);
}
 
int fact(int x)
{
   if (x <= 1)
       return 1;
   return x * fact(x - 1);
}
Output
 
40320
Enter value for n and r
5 4
 
Permutation values is = 120
Find the Sum of Series 1 + 1/2 + 1/3 + 1/4 + … + 1/N
#include 
#include 
void main()
{
    double number, sum = 0, i;
    clrscr();
    printf("\n enter the number ");
    scanf("%lf", &number);
    for (i = 1; i <= number; i++)
    {
        sum = sum + (1 / i);
        if (i == 1)
            printf("\n 1 +");
        else if (i == number)
            printf(" (1 / %lf)", i);
        else
            printf(" (1 / %lf) + ", i);
    }
    printf("\n The sum of the given series is %.2lf", sum);
}

Output
 
enter the number 4
 
1 + (1/2.000000)(1/3.000000)(1/4.000000)
The sum of the given series is 2.08


Find Sum of the Series 1/1! + 2/2! + 3/3! + ……1/N!
#include 
#include  
double sumseries(double);
 
main()
{
    double number,sum;
    printf("\n Enter the value:  ");
    scanf("%lf", &number);
    sum = sumseries(number);
    printf("\n Sum of the above series = %lf ", sum);
}
 
double sumseries(double m)
{
    double sum2 = 0, f = 1, i;
    for (i = 1; i <= m; i++)
    {
        f = f * i;
        sum2 = sum2 +(i / f);
    }
    return(sum2);
}

Output
 
Enter the value:  5

Sum of the above series = 2.708333

No comments:

Post a Comment