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

In summary, the conversation discusses a problem with a C program that calculates the natural log of a number entered by the user. The program consistently returns incorrect answers and the source code is provided for review. The conversation also briefly touches on the use of the "&" sign in scanf and printf functions.
  • #1
DomBrown2406
3
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
  • #2


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);
}
 
  • #3
The & sign is how my compiler knows what variable i want to use for %f etc.
 
  • #4
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.
 
  • #5
Ok, like I said I am relatively new, but I removed those & signs from printf and it works fine now, thanks :)
 

1. How do I input a number into the program?

In order to input a number into the program, you can use the input() function in Python or the Scanner class in Java. These functions allow you to prompt the user to enter a value, which can then be stored in a variable for use in the program.

2. Can I input a negative number into the program?

Yes, you can input negative numbers into the program. The natural log of a negative number will result in a complex number, so the program may need to be modified to handle complex numbers.

3. How do I calculate the natural log of a number in the program?

The natural log of a number can be calculated using the math.log() function in Python or the Math.log() method in Java. These functions take the number as an argument and return the natural log of that number.

4. How can I make the program print the natural log of the input number?

In order to print the natural log of the input number, you can use the print() function in Python or the System.out.println() method in Java. These functions allow you to output the result of the natural log calculation to the console.

5. Is there a way to handle errors if the user inputs an invalid value?

Yes, you can use exception handling to handle errors if the user inputs an invalid value. This can be done using try/except statements in Python or try/catch blocks in Java. You can also add input validation to ensure the user enters a valid number before performing the natural log calculation.

Similar threads

  • Programming and Computer Science
Replies
4
Views
735
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
4
Views
901
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
6
Views
5K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
3
Views
997
  • Programming and Computer Science
2
Replies
47
Views
4K
  • Programming and Computer Science
Replies
6
Views
2K
Back
Top