Q.Write a program to display the sum of the sequence given below.
1 + 1/2 + 1/4 + 1/8 + 1/16 + ... + 1/1024
#include<stdio.h>
#include<conio.h>
void main()
{
int x,n;
float s=1,i=0.5;
printf("Enter the number");
scanf("%d",&n);
for (x=1; x<=n ; x=x+1)
{
s=s+i;
i=i*0.5;
}
printf("Sum:%.3f",s);
getch();
}
No comments:
Post a Comment