Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6587147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:52:51+00:00 2026-05-25T16:52:51+00:00

I wrote a program to calculate the sum of a harmonic series (1 +

  • 0

I wrote a program to calculate the sum of a harmonic series (1 + 1/2 + 1/3 .. + 1/n), but I’m having trouble compiling it. I went over the code a bunch of times but I don’t see any syntax errors [2 are showing up when I try to compile]. Is my logic wrong, or is it syntactical?

#include <stdio.h>
int main( void ) {

 int v,p,i;
 double x=0;

 printf("Enter a value to calculate the value of this harmonic series: \n");
 scanf("%d",&v);

 if (v<=0) {
   printf("Please enter a POSITIVE number: \n");
   scanf("%d",&p);
   while (i=1; i<=v; i++) {
     x=x+(1/i);  }
     printf("The value for the series is %lf", x);
  }
    else {
      while (i=1; i<=v; i++) {
        x=x+(1/i);
      }
      printf("The value for the series is %lf", x);
    }
  return 0;
}

Thanks for any help

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:52:51+00:00Added an answer on May 25, 2026 at 4:52 pm

    All those while should be for, and that 1/i should be either 1./i or 1/(double)i, because otherwise an integer division is performed. Also, you should restructure your program flow to avoid that code duplication.

    But, there’s a subtle but more important mistake: due to how floating point arithmetic works, you should start to sum from smaller numbers to big ones, otherwise you may reach the point were each new addend is smaller than the current precision of that double, and the addition will have no effect1. So, your for should be reversed:

    for (i=v; i>=1; i--)
    

    Also: you should check the return value of the scanf to be 1, to make sure that the user actually inserted some valid numeric input. And, the printf specifier for doubles is just %f, without the l.

    Taking all this in account, you could rewrite the program as this:

    #include <stdio.h>
    
    int main( void )
    {
        int v=0,i,ch;
        double x=0.;
    
        printf("Enter a value to calculate the value of this harmonic series: ");
    
        /* The loop calls the scanf, and is repeated as far as the user continues to
           write garbage */
        while(scanf("%d",&v)==0 || v<=0)
        {
            printf("Please enter a POSITIVE number: ");
    
            /* Empty the input buffer to remove the eventual garbage; the logic is
               a bit convoluted to handle the case where the user enters EOF */
            while((ch=getchar())!='\n')
                if(ch==EOF)
                    return 1;
        }
    
        /* perform the actual sum - done from smallest to biggest term */
        for (i=v; i>=1; i--)
            x+=1./i;
    
        printf("The value for the series is %f\n", x);
        return 0;
    }
    

    1. For an harmonic series this is actually almost impossible – you would need to go to really big numbers to notice some difference – but with other series (whose terms get very small very fast) this suggestion can be really important.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just wrote a program to calculate employee's pay rates. To me, the program
I've wrote a program that gets a string, and then calculate a total value
I wrote a program to calculate nth root of a number upto 2 decimal
I wrote a program that simulates soft bodies using springs. It looks nice but
I am having a little problem. I wrote a program that extracts a set
When i run the code as is my output is: This program will calculate
I'm having some issues simplifying some functions in mathematica. In a program I wrote
I wrote a little program to calculate the first 18 triples (x,y,z) with x<y<z
I wrote a program in C# to calculate TF-IDF to rank documents. I used
Hi i wrote a method to calculate the number of times a number appears

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.