7 Replies - 26638 Views - Last Post: 24 January 2011 - 11:53 AM Rate Topic: -----

#1 Kaustubh_Gogoi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 9
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • 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;
}


Was This Post Helpful? 0
  • +
  • -

#4 Guest_Stepler*


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
Was This Post Helpful? -1

#5 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 614
  • View blog
  • 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

View PostStepler, 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!
Was This Post Helpful? 1
  • +
  • -

#6 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • 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. :hammer:
Was This Post Helpful? 0
  • +
  • -

#7 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1654
  • View blog
  • Posts: 3,133
  • 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.
Was This Post Helpful? 0
  • +
  • -

#8 IngeniousHax  Icon User is offline

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

Reputation: 78
  • View blog
  • Posts: 1,358
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1