Sum of the series: SUM=1+1/2+1/3+1/4+1/5....1/n

Page 1 of 1

7 Replies - 52675 Views - Last Post: 24 January 2011 - 11:53 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=45340&amp;s=b6534e2967342a9dd46fe529f04937ca&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 Kaustubh_Gogoi

• New D.I.C Head

Reputation: 0
• Posts: 1
• Joined: 03-January 08

Sum of the series: SUM=1+1/2+1/3+1/4+1/5....1/n

Posted 06 March 2008 - 07:33 AM

My exams are thr tomorrow plz help me wth ths problem:

SUM=1+1/2+1/3+1/4+1/5....1/n

n should be input by the programmer.
I coded the program as follows:

```#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
{
clrscr();
int x=1, y=2, counter;
float sum, number, n;
printf("Enter the value of n: ");
scanf("%f",&n);
for(counter=1;counter<=n;counter++)
{
number=(1/y);
sum=(x+number);
y++;
x=1/(++y);
printf("%f",sum);
}
return 0;
}

```

Plz help!!!!

Is This A Good Question/Topic? 0

Replies To: Sum of the series: SUM=1+1/2+1/3+1/4+1/5....1/n

#2 bhandari

Reputation: 9
• Posts: 754
• Joined: 31-January 08

Re: Sum of the series: SUM=1+1/2+1/3+1/4+1/5....1/n

Posted 06 March 2008 - 07:45 AM

0/10 for that. Don't go for exam

Don't mind that

This post has been edited by bhandari: 06 March 2008 - 07:45 AM

#3 letthecolorsrumble

• Student of The Sun

Reputation: 27
• Posts: 555
• Joined: 07-November 07

Re: Sum of the series: SUM=1+1/2+1/3+1/4+1/5....1/n

Posted 06 March 2008 - 08:22 AM

Well, you will to practice more, if you wish to do good, but here is the code.

```#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

main() {

int x=1, y=2, n; //n is the integer..see task
float sum=1.0f;

printf("Enter the value of n: ");
scanf("%d",&n);

for(;y<=n;) {
printf("%f\n",sum); //intermediate sums
sum = sum+(1.0f/(y++));
}
printf("%f\n\n",sum); //final sum
return 0;
}

```

Reputation:

Re: Sum of the series: SUM=1+1/2+1/3+1/4+1/5....1/n

Posted 06 March 2008 - 09:08 AM

Your code works incorrectly!
Here is how should be:
```#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
int n;
float sum=0;
printf("Enter the value of n: ");
scanf("%d",&n);
for(int i=1;i<=n;i+=1)
sum+=1./i;
printf("%f",sum);
}

```

I Write programs to order, If there will be questions my ICQ 392172602

#5 Aphex19

• Born again Pastafarian.

Reputation: 618
• Posts: 1,873
• Joined: 02-August 09

Re: Sum of the series: SUM=1+1/2+1/3+1/4+1/5....1/n

Posted 24 January 2011 - 07:09 AM

Stepler, on 06 March 2008 - 09:08 AM, said:

Your code works incorrectly!
Here is how should be:
```#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
int n;
float sum=0;
printf("Enter the value of n: ");
scanf("%d",&n);
for(int i=1;i<=n;i+=1)
sum+=1./i;
printf("%f",sum);
}

```

I Write programs to order, If there will be questions my ICQ 392172602

This isn't the way dreamincode works, you can't just do their work for them. Also, it's int main, not void main!

#6 r.stiltskin

• D.I.C Lover

Reputation: 2032
• Posts: 5,435
• Joined: 27-December 05

Re: Sum of the series: SUM=1+1/2+1/3+1/4+1/5....1/n

Posted 24 January 2011 - 07:37 AM

Wake up! Those posts were almost 3 years old.

#7 Salem_c

• void main'ers are DOOMED

Reputation: 2154
• Posts: 4,219
• Joined: 30-May 10

Re: Sum of the series: SUM=1+1/2+1/3+1/4+1/5....1/n

Posted 24 January 2011 - 07:58 AM

There was a now deleted driveby post which bumped the thread.
Deploy the padlocks so it never again escapes the bone yard.

#8 IngeniousHax

• |>|20-514<|{3|2

Reputation: 84
• Posts: 1,385
• Joined: 28-March 09

Re: Sum of the series: SUM=1+1/2+1/3+1/4+1/5....1/n

Posted 24 January 2011 - 11:53 AM

Quick note, the series 1/n never converges, so there shouldn't be a sum for it unless you are going from x --> y where y is a finite number.