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 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Back
Top