You are here:

C/C program making.

Advertisement


Question
QUESTION: please help me to make a program to get the first 100 elements of the following series and get the addition of it.(for loop)
if u can teach me how to make it with using while loop as well.
1,1/2,1/3,1/4,,,,,1/n

Thank you so much

ANSWER: Hi Jude,

The following is a basic example using a for and while loop. I hope this answers your question.


#include<stdio.h>
int main()
{
  double count = 0, sum = 0, temp;
  for(;count < 100; count++)
  {
     temp = 1.0/(count+1.0);
     printf("1/%.0f = %.4f\n", (count+1.0), temp);
     sum += temp;
  }
  printf("sum = %.4f\n", sum);

  //using a while loop

  count = sum = temp = 0;
  while(count < 100)
  {
     temp = 1.0/(count+1.0);
     printf("1/%.0f = %.4f\n", (count+1.0), temp);
     sum += temp;
     count++;
  }
  
  printf("sum = %.4f\n", sum);
  return 0;
}




1/1 = 1.0000
1/2 = 0.5000
1/3 = 0.3333
1/4 = 0.2500
1/5 = 0.2000
1/6 = 0.1667
1/7 = 0.1429
1/8 = 0.1250
1/9 = 0.1111
1/10 = 0.1000
1/11 = 0.0909
1/12 = 0.0833
1/13 = 0.0769
1/14 = 0.0714
1/15 = 0.0667
1/16 = 0.0625
1/17 = 0.0588
1/18 = 0.0556
1/19 = 0.0526
1/20 = 0.0500
1/21 = 0.0476
1/22 = 0.0455
1/23 = 0.0435
1/24 = 0.0417
1/25 = 0.0400
1/26 = 0.0385
1/27 = 0.0370
1/28 = 0.0357
1/29 = 0.0345
1/30 = 0.0333
1/31 = 0.0323
1/32 = 0.0312
1/33 = 0.0303
1/34 = 0.0294
1/35 = 0.0286
1/36 = 0.0278
1/37 = 0.0270
1/38 = 0.0263
1/39 = 0.0256
1/40 = 0.0250
1/41 = 0.0244
1/42 = 0.0238
1/43 = 0.0233
1/44 = 0.0227
1/45 = 0.0222
1/46 = 0.0217
1/47 = 0.0213
1/48 = 0.0208
1/49 = 0.0204
1/50 = 0.0200
1/51 = 0.0196
1/52 = 0.0192
1/53 = 0.0189
1/54 = 0.0185
1/55 = 0.0182
1/56 = 0.0179
1/57 = 0.0175
1/58 = 0.0172
1/59 = 0.0169
1/60 = 0.0167
1/61 = 0.0164
1/62 = 0.0161
1/63 = 0.0159
1/64 = 0.0156
1/65 = 0.0154
1/66 = 0.0152
1/67 = 0.0149
1/68 = 0.0147
1/69 = 0.0145
1/70 = 0.0143
1/71 = 0.0141
1/72 = 0.0139
1/73 = 0.0137
1/74 = 0.0135
1/75 = 0.0133
1/76 = 0.0132
1/77 = 0.0130
1/78 = 0.0128
1/79 = 0.0127
1/80 = 0.0125
1/81 = 0.0123
1/82 = 0.0122
1/83 = 0.0120
1/84 = 0.0119
1/85 = 0.0118
1/86 = 0.0116
1/87 = 0.0115
1/88 = 0.0114
1/89 = 0.0112
1/90 = 0.0111
1/91 = 0.0110
1/92 = 0.0109
1/93 = 0.0108
1/94 = 0.0106
1/95 = 0.0105
1/96 = 0.0104
1/97 = 0.0103
1/98 = 0.0102
1/99 = 0.0101
1/100 = 0.0100
sum = 5.1874
1/1 = 1.0000
1/2 = 0.5000
1/3 = 0.3333
1/4 = 0.2500
1/5 = 0.2000
1/6 = 0.1667
1/7 = 0.1429
1/8 = 0.1250
1/9 = 0.1111
1/10 = 0.1000
1/11 = 0.0909
1/12 = 0.0833
1/13 = 0.0769
1/14 = 0.0714
1/15 = 0.0667
1/16 = 0.0625
1/17 = 0.0588
1/18 = 0.0556
1/19 = 0.0526
1/20 = 0.0500
1/21 = 0.0476
1/22 = 0.0455
1/23 = 0.0435
1/24 = 0.0417
1/25 = 0.0400
1/26 = 0.0385
1/27 = 0.0370
1/28 = 0.0357
1/29 = 0.0345
1/30 = 0.0333
1/31 = 0.0323
1/32 = 0.0312
1/33 = 0.0303
1/34 = 0.0294
1/35 = 0.0286
1/36 = 0.0278
1/37 = 0.0270
1/38 = 0.0263
1/39 = 0.0256
1/40 = 0.0250
1/41 = 0.0244
1/42 = 0.0238
1/43 = 0.0233
1/44 = 0.0227
1/45 = 0.0222
1/46 = 0.0217
1/47 = 0.0213
1/48 = 0.0208
1/49 = 0.0204
1/50 = 0.0200
1/51 = 0.0196
1/52 = 0.0192
1/53 = 0.0189
1/54 = 0.0185
1/55 = 0.0182
1/56 = 0.0179
1/57 = 0.0175
1/58 = 0.0172
1/59 = 0.0169
1/60 = 0.0167
1/61 = 0.0164
1/62 = 0.0161
1/63 = 0.0159
1/64 = 0.0156
1/65 = 0.0154
1/66 = 0.0152
1/67 = 0.0149
1/68 = 0.0147
1/69 = 0.0145
1/70 = 0.0143
1/71 = 0.0141
1/72 = 0.0139
1/73 = 0.0137
1/74 = 0.0135
1/75 = 0.0133
1/76 = 0.0132
1/77 = 0.0130
1/78 = 0.0128
1/79 = 0.0127
1/80 = 0.0125
1/81 = 0.0123
1/82 = 0.0122
1/83 = 0.0120
1/84 = 0.0119
1/85 = 0.0118
1/86 = 0.0116
1/87 = 0.0115
1/88 = 0.0114
1/89 = 0.0112
1/90 = 0.0111
1/91 = 0.0110
1/92 = 0.0109
1/93 = 0.0108
1/94 = 0.0106
1/95 = 0.0105
1/96 = 0.0104
1/97 = 0.0103
1/98 = 0.0102
1/99 = 0.0101
1/100 = 0.0100
sum = 5.1874


---------- FOLLOW-UP ----------

QUESTION: Could you please teach me why you've typed as " sum + = temp;"

what does it mean?
why is there a equation(=) symbol?

Answer
Hi Jude,

"sum += temp" is exactly the same as typing "sum = sum + temp". There are several similar  assignment operators in c:

%= (remainder assignment operator)
*= (multiplication assignment operator)
+= (addition assignment operator)
−= (subtraction assignment operator)
/= (division assignment operator)
&= (bitwise AND assignment operator)
|= (bitwise inclusive OR assignment operator)
^= (bitwise exclusive OR assignment operator)



-Tony

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tony Nazzal

Expertise

I will answer any c language programming and syntax questions.

Experience

I have almost a decade of c programming experience.

Education/Credentials
I have a bachelor of science degree and several years of industry experience.

©2016 About.com. All rights reserved.