Computer Concept & Programming In C - Unit III - 8

/* Q.11 Write a program in 'C' to generate the following pattern
*
**
***
****                       */                                            (AKTU. 2008 - 09)
Ans. #include<stdio.h>
#include<conio.h>
void main()
{
int i, j;
clrscr();
for( i = 0 ; i < 5; i++)
  {
    for( j = 0; j < i + 1; j++)
printf("*");
    printf("\n");
  }
printf("\n\n Press any key to exit.");
getch();
}

Q.12 Write a program in C which inputs ten integers and print their average.                                                          (AKTU. 2008-09)
Ans. # include <stdio.h>
# include <conio.h>
Void main ( )
{
int array [10];
float average ;
clrscr ( );
printf (“\n Enter 10 integer to find their average”);
for (int i = 0; i < 10; i++)
scanf (“%d”, & array [i]);
// Finding Sum
int sum = 0;
for (int i = 0; i < 10; i++)
sum = sum + array [i];
// Finding Average
average = sum/10;
printf (“\n The average of the 10 numbers = %f, average);
getch ( );
}

Q.13 Write a program to calculate the sum of following series up to first 100 terms
         S = 14 + 34 + 54 + 74 + ………….100 terms.                (AKTU. 2008-09)
Ans. We see here that the series is,
S = 14 + 34 + 54 + 74 + …………. upto 100 terms.
# include <stdio.h>
# include <conio.h>
# include <math.h>
# define terms 100 
void main ( )
{
long S = 0;
int j = 1;
  clrscr ( );

for (int i = 0; i < terms; i++)
{
(double) S;
S+ = pow (j, 4.0);
j+ = 2;
}
printf (“\n The sum of series S of % d terms is:”, (int) terms);
printf (“%f”, S);
printf (“\n Press any key to exit.”);
getch ( );
}

Q.14 Draw a flowchart and write a function in C to calculate factorial of given number and also write a program to calculate the sum of the following series using the above fn :
S = 1! + 2! + 3! + ........ + N!.                                             (AKTU. 2010-11)
Ans.
The C program is as follows :
/* S = 1! + 2! + 3! ---- + N! */
#include<stdio.h>
#include<conio.h>
double factorial (int);
void main()
{
 int i, n;
 double sum = 0.0;
 clrscr();
 printf("\nEnter number of terms");
 scanf("%d", &n);

 for(i = 1; i <= n; i++)
   sum += factorial(i);

 printf("\nSum of series = %f", sum);
 getch();
}

double factorial(int p)
{
 int j;
 double  fact = 1.0;
 for(j = 1; j <= p; j++)
  fact *= j;
 return fact;
}