[go: up one dir, main page]

#Include : 9. Design An Algorithm and Implement Using A C Program Which Finds The Sum of The Infinite Series

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

9.

Design an algorithm and implement using a C program which finds the sum
of the infinite series
1 – x2/2! + x4/4! – x6/6! + ....
#include <stdio.h>
void main()
{
float x,sum,t,d;
int i,n;
printf("Input the Value of x :");
scanf("%f",&x);
printf("Input the number of terms : ");
scanf("%d",&n);
sum =1; t = 1;
for (i=1;i<n;i++)
{
d = (2*i)*(2*i-1);
t = -t*x*x/d;
sum =sum+ t;
}
printf("\nthe sum = %f\nNumber of terms = %d\nvalue of
x = %f\n",sum,n,x);
}

Output:
Input the Value of x :2
Input the number of terms : 6
the sum = -0.416155
Number of terms = 6
value of x = 2.000000
10 Design a C program to print the sequence of numbers in which
each number is the sum of the three most recent predecessors.
Assume first three numbers as 0, 1, and 1.
#include<stdio.h>
#include<conio.h>
void main()
{
int a=0, b=1, c=1,d, i, n;
printf("\n Enter the range:");
scanf("%d", &n);
printf("\n The fibonacci series : \n");
printf("%d\t%d\t%d\t",a,b,c);
for(i=4; i<=n; i++)
{
d=a+b+c;
printf("%d\t",d);
a=b;
b=c;
c=d;
}
getch();
}
12. Develop an algorithm which computes the all the factors between 1 to 100 for a given
number and implement it using C.
Program:
#include<stdio.h>
int main()
{
int number, i;
printf("Enter a positive integer: ");
scanf("%d",&number);
printf("Factors of %d are: ", number);
for(i=1; i <= number; ++i)
{
if (number%i == 0)
{
printf("%d ",i);
}
}
return 0;
}
Output:
Enter a positive integer: 100
Factors of 100 are: 1 2 4 5 10 20 25 50 100
14. Design a C program which reverses the elements of the array.
#include <stdio.h>
#define MAX_SIZE 100

int main()
{
int arr[MAX_SIZE];
int size, i;

/* Input size of array */


printf("Enter size of the array: ");
scanf("%d", &size);

/* Input array elements */


printf("Enter elements in array: ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}

/*
* Print array in reversed order
*/
printf("\nArray in reverse order: ");
for(i = size-1; i>=0; i--)
{
printf("%d\t", arr[i]);
}

return 0;
}
Enter size of the array: 5
Enter elements in array: 4 5 3 2 1

Array in reverse order: 1 2 3 5 4

You might also like