Slip18
Q1. Write a C program to generate following triangle up to n lines. [15 Marks]
1
1 2
1 2 3
#include <stdio.h>
int main()
{
int i, j, rows;
printf("Enter number of rows: ");
scanf("%d",&rows);
for(i=1; i<=rows; ++i)
{
for(j=1; j<=i; ++j)
{
printf("%d ",j);
}
printf("\n");
}
return 0;
}
Q2. Write a program to calculate sum of following series up to n terms. [25 Marks]
Sum=X-X
2
/2!+X3
/3!-……
#include<math.h>
#include<stdio.h>
int fact(int index)
{
int f = 1, i;
for(i = 1; i <= index; i ++)
{
f = f*i;
}
return f;
}
void main()
{
int x = 1;
int n = 3;
double sum = 0,term=-1, m;
// Sum of n-1 terms starting from 2nd term
int i;
for (i = 1; i <= n; i++) {
term = term * (-1);
m = term * pow(x, i) / fact(i);
sum = sum + m;
}
printf("\n%.4f", sum);
getch();
}
(Note: Write separate user defined function to calculate power and factorial)
No comments:
Post a Comment