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

AI Thread Summary
The discussion revolves around a user's issue with a C program designed to calculate the natural logarithm of a user-input number. The user reports that the program returns incorrect values, specifically a large number when inputting 1, which should yield 0 since ln(1) = 0. The source code provided contains a misunderstanding regarding the use of the ampersand (&) symbol in C. It is clarified that the ampersand is necessary for the `scanf` function to reference the variable's memory address, allowing it to store the input value. However, for the `printf` function, the ampersand should be omitted, as it requires the actual value rather than the address. After removing the ampersands from the `printf` statements, the user finds that the program works correctly. This highlights the importance of understanding variable references in C programming, particularly when using input and output functions.
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 :)
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top