Trying to make a program to take and print the natural log of a number input

Click For Summary

Discussion Overview

The discussion revolves around a C programming issue where a user is attempting to create a program that calculates and prints the natural logarithm of a user-inputted number. The focus is on debugging the code to ensure it produces the correct output.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Homework-related

Main Points Raised

  • One participant reports that their program returns incorrect values for the natural logarithm, specifically mentioning that inputting 1 yields an unexpected large value.
  • Another participant questions the use of the & sign in the printf function, seeking clarification on its purpose.
  • It is explained that the & sign is used in scanf to provide the address of the variable, while printf requires just the value of the variable.
  • A later reply indicates that removing the & signs from the printf function resolved the issue for the original poster.

Areas of Agreement / Disagreement

Participants generally agree on the function of the & sign in the context of scanf and printf, with some clarification provided on its necessity. The original poster's issue appears to be resolved after the clarification, but no consensus on broader programming practices is established.

Contextual Notes

The discussion does not address potential limitations in the original code, such as the infinite loop in the do-while structure or the handling of invalid input values for the logarithm function.

DomBrown2406
Messages
3
Reaction score
0
I'm relatively new to C programming and I am trying to make a program to take and print the natural log of a number input by the user but for some reason my program always returns incorrect answers. For example I entered 1 as my number, and the answer came back as some ridiculously big value which is clearly wrong as I know that ln 1 = 0.
My source code is given below, any help would be greatly appreciated :)

Code:
#include <stdio.h>
#include <math.h>

int main(void)
{
    float in;
    float out;    
    do {
    
    printf("Enter the number to take ln of...\n");
    scanf("%f", &in);
    out = log(in);
    printf("Ln of %f is %f\n", &in, &out);
    printf("If you want to do this again press 1, if not press any other number\n");
    getchar();}
    while(1==1);
}
 
Last edited:
Technology news on Phys.org


DomBrown2406 said:
printf("Ln of %f is %f\n", &in, &out);

What & means?

Use [noparse]
Code:
[/noparse] tags to format your source code:

Code:
#include <stdio.h>
#include <math.h>

int main(void)
{
   float in;
   float out; 
   do {
      printf("Enter the number to take ln of...\n");
      scanf("%f", &in);
      out = log(in);
      printf("Ln of %f is %f\n", &in, &out);
      printf("If you want to do this again press 1, if not press any other number\n");
      getchar();
   } while(1==1);
}
 
The & sign is how my compiler knows what variable i want to use for %f etc.
 
Not exactly. & doesn't mean what variable, & means where the variable is. It is necessary when you use scanf, as scanf needs a reference to be able to put the value in the variable, but printf needs just a value.
 
Ok, like I said I am relatively new, but I removed those & signs from printf and it works fine now, thanks :)
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
Replies
7
Views
2K
Replies
14
Views
4K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 6 ·
Replies
6
Views
6K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
47
Views
5K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K