//In the Name of ALLAH /* This program calculates the following series: 1+(1/2)+(1/4)+(1/8)+(1/16)+... Also the program contains a new command which determines the number of digits to be printed in the output when we use a float variable. More explanation in line 25 */ #include <stdio.h> #include <conio.h> void main() { int count; int num; //num tells the number of terms a user wants to have for calculating the result. float sum, x; clrscr(); printf("This program calculates the following series:\n1+(1/2)+(1/4)+(1/8)+(1/16)+...\n"); printf("Enter the number of terms you want to be participated in calculating the result of the series: "); scanf("%d",&num); for (sum=0, x=1.0, count=1; count<=num; count++, x*=2) { sum+=1/x; printf("sum=%7.4f when count=%d\n", sum, count); /* %7.4f means the output will contain 7 characters (6 digits plus a dot (point)). 4 digits are reserved for the right side of the point. The other two, will be used before the point. Example1: 46.5676 There are 7 characters in this example: 2 digits on the right side + 1 point + 4 digits on the left side ----- When we type %7.4f, if our variable contains more than 4 digits on the right side of the point, only the first four digits on the right side will be shown. Example2: x=12.45678; If we use %7.4f in printf command for this example, the output will be: 12.4567 ----- Example3: sum=123.567899; By writing printf("%7.4f", sum) in this example, 123.5678 will be printed in the output. Note that all of the digits before the point will be shown in the output whether the total number of digits exceed 7 or not. */ } getch(); }