Aim: 4.1 Write a program to generate first n number of Fibonacci series.
Code:
#include <stdio.h>
int main()
{
int fib1 = 0, fib2 = 1, fib3, num, count = 0;
printf("Enter the num : ");
scanf("%d",&num);
printf("First %d Fibonacci num are : \n",num);
printf("%d\n",fib1);
printf("%d\n",fib2);
count = 2;
while(count<num)
{
fib3 = fib1 + fib2;
count++;
printf("%d\n",fib3);
fib1 = fib2;
fib2 = fib3;
}
}
Output screenshot:
Aim: 4.2 Write a program to evaluate the series 1^2+2^2+3^2+......+n^2.
Code:
#include<stdio.h>
int main()
{
int n,i;
int sum=0;
printf("Enter value of n : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+(i*i);
}
printf("\nSum of the Series = %d",sum);
return 0;
}
Output screenshot:
Aim: 4.3 Write a C program to find 1+1/2+1/3+1/4+....+1/n.
Code:
#include<stdio.h>
int main()
{
int n,i;
float sum=0;
printf("Enter value of n : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+(1.0/i);
}
printf("\nSum of the Series = %f",sum);
return 0;
}
Output screenshot:
Aim: 4.4 Write a C program to find 1+1/2!+1/3!+1/4!+.....+1/n!.
Code:
#include<stdio.h>
int main()
{
int n,i,j,fact=1;
float sum=0;
printf("Enter Value of n : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact=1;
for(j=i;j>0;j--)
{
fact=fact * j;
}
sum=sum+(1.0/fact);
}
printf("\nSum of Series = %f",sum);
return 0;
}
Output screenshot:
Aim: 4.5 Write a program to evaluate the series sum=1-x+ x^2/2! x^3/3!+x^4/4! . .. ..-x^9/9!
Code:
#include<stdio.h>
#include<math.h>
int main()
{
int i,j,x,fact=1;
float sum=0;
printf("Enter the Value of X : ");
scanf("%d",&x);
for(i=0;i<=9;i++)
{
fact=1;
for(j=i;j>0;j--)
{
fact=fact*j;
}
if(i%2==0)
{
sum=sum+(pow(x,i)/fact);
}
else
{
sum=sum-(pow(x,i)/fact);
}
}
printf("\nSum of Series = %f",sum);
return 0;
}
Output screenshot:
No comments:
Post a Comment
If you have any doubts or suggestions, Please let me know