Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,493 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,717 people online right now. Registration is fast and FREE... Join Now!




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

 
Reply to this topicStart new topic

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

Kaustubh_Gogoi
6 Mar, 2008 - 06:33 AM
Post #1

New D.I.C Head
*

Joined: 3 Jan, 2008
Posts: 1

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:

CODE

#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!!!!


User is offlineProfile CardPM
+Quote Post

bhandari
RE: Sum Of The Series: SUM=1+1/2+1/3+1/4+1/5....1/n
6 Mar, 2008 - 06:45 AM
Post #2

D.I.C Addict
Group Icon

Joined: 31 Jan, 2008
Posts: 747


Dream Kudos: 900
My Contributions
0/10 for that. Don't go for exam

Don't mind that

This post has been edited by bhandari: 6 Mar, 2008 - 06:45 AM
User is offlineProfile CardPM
+Quote Post

letthecolorsrumble
RE: Sum Of The Series: SUM=1+1/2+1/3+1/4+1/5....1/n
6 Mar, 2008 - 07:22 AM
Post #3

Student of The Sun
Group Icon

Joined: 7 Nov, 2007
Posts: 550



Thanked: 1 times
My Contributions
Well, you will to practice more, if you wish to do good, but here is the code.

cpp

#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;
}

User is offlineProfile CardPM
+Quote Post

Stepler
RE: Sum Of The Series: SUM=1+1/2+1/3+1/4+1/5....1/n
6 Mar, 2008 - 08:08 AM
Post #4

New D.I.C Head
*

Joined: 4 Mar, 2008
Posts: 27



Thanked: 2 times
My Contributions
Your code works incorrectly!
Here is how should be:
CODE

#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
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 07:48PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month