kagushajeiel
kagushajeiel

Reputation: 81

Scanning and printing variable data type "double" in C

I'm studying algorithms and would love to learn. My problem is this, I can't output a number "600851475143" using the integer data type in C. So I switched to doubles. However the output is "0.0000". Can someone kindly tell me what I'm doing wrong? I simply want to scan and print any number in the double data type then I'm going to focus on getting the highest prime factor :)

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

int main()
{
double number;
double div = 2;
double highest = 2;

printf("Please input a number: ");
scanf("%lf", &number);

printf("\n You entered: %lf", &number);

while(number!=0){
    if(fmod(number,div) != 0){div = div + 1;}
    else{
    number = number / div;
    printf("\n %lf", div);

    if(highest < div){highest = div;}
    if(number == 1){break;}
    }
}
printf("\n The highest number is %lf", highest);

return 0;}

What I did:

  1. Searched for "scan double in c" in google, learned the "%lf" is the right way to go, but the program doesn't show anything.

  2. I checked out various questions in Stackoverflow like:

Why does scanf need lf for doubles

Reading in double values with scanf in c

Difference between float and double

Read and Write within a file in C (double it)

Reading and writing double precision from/to files

float vs. double precision

Reading in double values with scanf in c

  1. Other sites I searched:

http://www.technoburst.net/2011/07/reading-double-in-c-using-scanf.html

Thank you for enlightening me with your knowledge.

Upvotes: 1

Views: 25083

Answers (2)

kirbyfan64sos
kirbyfan64sos

Reputation: 10727

Compiling with a compiler with warnings shows the issue:

dbl.c:13:31: warning: format specifies type 'double' but the argument has type
     'double *' [-Wformat]
printf("\n You entered: %lf", &number);
                       ~~~   ^~~~~~~
1 warning generated.

Change that line to:

printf("\n You entered: %lf", number);

You're trying to print the number's address instead of the number.

Also, try adding a newline to the end:

printf("\n The highest number is %lf\n", highest);

Since, you're working with integral numbers, though, I'd advise you to just use a long long, assuming you have a C99 compiler:

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

int main()
{
long long number;
long long div = 2;
long long highest = 2;

printf("Please input a number: ");
scanf("%lld", &number);

printf("\n You entered: %lld", number);

while(number!=0){
    if(number % div != 0){div = div + 1;}
    else{
    number = number / div;
    printf("\n %lld", div);

    if(highest < div){highest = div;}
    if(number == 1){break;}
    }
}
printf("\n The highest number is %lld\n", highest);

return 0;}

You can also reformat your code a bit for readability:

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

int main() {
    long long number, div, highest;
    div = highest = 2;

    printf("Please input a number: ");
    scanf("%lld", &number);

    printf("\n You entered: %lld", number);

    while(number != 0) {
        if (number % div != 0) div += 1;
        else {
            number /= div;
            printf("\n %lld", div);

            if (highest < div) highest = div;
            if (number == 1) break;
        }
    }
    printf("\n The highest number is %lld\n", highest);

    return 0;
}

I would also advise doing the newlines a little differently, which leaves you with:

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

int main() {
    long long number, div, highest;
    div = highest = 2;

    printf("Please input a number: ");
    scanf("%lld", &number);

    printf("\n You entered: %lld\n", number);

    while(number != 0) {
        if (number % div != 0) div += 1;
        else {
            number /= div;
            printf("%lld\n", div);

            if (highest < div) highest = div;
            if (number == 1) break;
        }
    }
    printf("The highest number is %lld\n", highest);

    return 0;
}

Upvotes: 0

Jurica Penjgušić
Jurica Penjgušić

Reputation: 100

The problem is in function printf("%lf",&number) should be printf("%lf",number)

Upvotes: 1

Related Questions