Thread: What is wrong with my code?

  1. #1
    Registered User
    Join Date
    Jun 2017
    Posts
    3

    What is wrong with my code?

    Question:
    What is wrong with my code?-untitled-jpg
    My code:
    insert
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    main()
    {
        int a,i,t=1,x,temp;
        float log=0;
        printf("Enter x\n");
        scanf("%d",&x);
    
        temp=(x-1)/x;
    
    
        for(i=1;i<=7;i++)
        {
            a=1/i;
    
            t=t*temp;
    
            log=log+(a*t);
        }
    
        printf("The sum of first 7 terms is %f",log);
    
    }

  2. #2
    Registered User
    Join Date
    Apr 2017
    Posts
    80
    The syntax colouring in your post is giving one clue: You've used a reserved word, log (which is a function), as a variable. Try changing that variable name to something else. Not sure if that's it as you haven't posted the error you're seeing, assuming you are seeing an error.
    Last edited by BpB; 07-04-2017 at 01:07 AM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,674
    > a=1/i;
    Beware of integer division.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    3,747
    For those who did NOT know of the series
    Natural Logarithm Series

    X must be greater than 1/2 to give an answer that is approximately right.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Jul 2017
    Posts
    4
    temp=(x-1)/x;
    ........

    Many integer division expressions......
    I recommend that all variables use double type

  6. #6
    Registered User
    Join Date
    Jul 2017
    Posts
    4
    temp=(x-1)/x;
    ........

    Many integer division expressions......
    I recommend that all variables use double type

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what's wrong with this code?
    By patishi in forum C Programming
    Replies: 14
    Last Post: 05-28-2014, 12:41 PM
  2. What is the wrong in this code?
    By Mohamed Shokry in forum C Programming
    Replies: 7
    Last Post: 05-03-2013, 03:30 PM
  3. what is wrong in my code?
    By sjcc in forum C Programming
    Replies: 3
    Last Post: 05-01-2009, 08:18 AM
  4. what's wrong with this code ?
    By abhiboy007 in forum C Programming
    Replies: 2
    Last Post: 05-21-2002, 09:16 PM
  5. what is wrong with this code
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 11-21-2001, 03:46 PM

Tags for this Thread