Hi y'all. I was wondering if someone could help me out a little bit. I have a couple problems due in my Scientific Programming class, and I am fairly lost. I posted another question in the C forum, but I think I was able to solve it on my own. It may not be pretty, but at least it works.

Now, I am stumped on another problem. The question is as follows:

Write a program that reads a positive number and then computes and prints the logarithm of the value to base 8. For example, the logarithm of 64 to base 8 is 2 because....etc

So, being that it has been about 13 years since I have taken math classes, coupled with the fact that I apparently am not too sharp when it comes to programming, I am not exactly sure how I am to get to the answer I want. I understand that to get to the answer, I need to take the (log x)/(log of 8), but I am really unsure of how to enter that into C.

Any tips would be greatly appreciated.

Recommended Answers

All 4 Replies

I understand that to get to the answer, I need to take the (log x)/(log of 8), but I am really unsure of how to enter that into C.

for what you just wrote, in C/C++ would be:

double result = log(x) / log(8.);

you will need to include the math header ("math.h" or "cmath")

Okay, so maybe I am not as slow as I had thought. That seems fairly simple enough. Another question that I have is with the "positive" number portion of the question. Would I want to set up an if/then deal that would berate (I kid, but it would be fun) if they entered a negative value? Also, I imagine that since we are not dealing with any constants other than log 8 I really do not have to declare/define anything. The log deal is standard in the math.h library, correct?

And thank you for the reply. I appreciate it. When I finish my coding, would you (or anyone) mind looking it over just to see if I can improve on anything. And for the record, I am rocking the C program. Haven't gotten to C++ stuff yet.

So, is this all I really have to do for the question stated in the original post? Code is as follows:

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

int main(void)
{
    int x ;
    double result = log(x) / log(8.);
    
    printf("Enter a Positive value: \n") ;
    scanf("%i", &x) ;
    printf("The log in base 8 is: %lf\n", log(x) / log(8.) ) ;
    
    
    system("Pause") ;
    return (0) ;
}

Seems like it should be more complicated than that.

Close enough. You don't need the line declaring result, and you probably would want to be able to enter a non-integer number; i.e.

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

int main(void){
    double x ;
        
    printf("Enter a Positive value: \n") ;
    scanf("%lf", &x) ;
    printf("The log in base 8 is: %lf\n", log(x) / log(8.) ) ;
    
    system("Pause") ;
    return (0) ;
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.