Thread: How do I see if a variable I entered is an integer?

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    2

    Question How do I see if a variable I entered is an integer?

    Hi everyone,

    So I'm kinda new to C programming. I took a bit of Java back in high school, but since C is a bit different, I don't know what commands and functions I can use.

    I'm trying to write a program as a practice exercise that scans input fractions and prints the sum. It's pretty simple, and I have the basic code down, but I'm trying to modify the code so that it simplifies the fraction.
    What I'm trying to do is write an if statement so that if the fraction can be simplified, i.e. the numerator and denominator can both be divided and result in an integer, the program keeps dividing until it can't anymore. So if the numerator or denominator can't be divided into integers anymore, it prints the simplified fraction.

    Any thoughts?

    Thanks!

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Read it in as a string, and then try to parse it with sscanf, using the %d format specifier. If sscanf returns a positive number, then it was a valid integer.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    In regards to your question in the title, you just have to compare the floored( floor(x) ) version of the number with the actual number. If those two are the same, the number is an integer. "floor()" rounds real numbers always towards zero. If you use it, remember to include <math.h> at the beginning of your file.

    Another way is to convert the number to int/long and then compare it with its float/double version. Same result.
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    The usual way to reduce a fraction to lowest terms is to find the greatest common divisor using Euclid's algorithm:
    Code:
    #include <stdio.h>
    
    int gcd(int a, int b) {  // Euclid's gcd algorithm
        while (b != 0) {
            int t = b;
            b = a % b;
            a = t;
        }
        return a;
    }
    
    int main() {
        char line[500];
    
        while (1) {
            int num, den;
    
            printf("Enter a fraction: ");
            fgets(line, sizeof line, stdin);
            int pos = 0;
            if (sscanf(line, "%d%n", &num, &pos) != 1)
                break;
            if (sscanf(line+pos, " /%d", &den) != 1)
                den = 1;
    
            int g = gcd(num, den);
            num /= g;
            den /= g;
    
            printf("Lowest terms: %d", num);
            if (den != 1)
                printf("/%d", den);
            putchar('\n');
        }
    
        return 0;
    }

  5. #5
    Registered User
    Join Date
    Feb 2017
    Posts
    2
    algorism, could you explain all the commands and functions and whatnot? I'm still new to C, so I have no idea what most of those terms do.

    Thanks!

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by TemaRN View Post
    could you explain all the commands and functions and whatnot? I'm still new to C, so I have no idea what most of those terms do.
    I was just demonstrating the gcd function. To learn how it works, look up "Euclid's algorithm" on Wikipedia.

    If you're going to learn the language then you need to be able to look up standard library functions yourself. The standard library functions I used are:
    printf
    fgets
    sscanf
    putchar

    Look them up and then ask questions if you still don't understand something.

    Handling negative values:
    Code:
    #include <stdio.h>
    
    int gcd(int a, int b) {
        while (b != 0) {
            int t = b;
            b = a % b;
            a = t;
        }
        return a;
    }
    
    int main() {
        char line[500];
    
        while (1) {
            int num, den;
    
            printf("Enter a fraction: ");
            fgets(line, sizeof line, stdin);
            int pos = 0;
            if (sscanf(line, "%d%n", &num, &pos) != 1)
                break;
            if (sscanf(line+pos, " /%d", &den) != 1)
                den = 1;
    
            if (den == 0) {
                printf("Error: Division by zero\n");
                continue;
            }
    
            // Handle negatives (ignoring 2's comp INT_MIN problem)
            int neg = 0;
            if (num < 0) { num = -num; neg = 1; }
            if (den < 0) { den = -den; neg = !neg; }
    
            int g = gcd(num, den);
            num /= g;
            den /= g;
    
            printf("Lowest terms: %s", neg ? "-" : "");
            if (num > den) {
                printf("%d ", num / den);
                num %= den;
            }
            if (num != 0)
                printf("%d/%d", num, den);
            putchar('\n');
        }
    
        return 0;
    }
    Last edited by algorism; 02-02-2017 at 03:03 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-18-2013, 02:57 PM
  2. variable of type enum to be entered by user
    By Clayg in forum C Programming
    Replies: 4
    Last Post: 12-07-2011, 08:22 AM
  3. Replies: 7
    Last Post: 09-19-2011, 01:37 PM
  4. Replies: 7
    Last Post: 09-04-2011, 09:29 PM
  5. What does it mean when a integer variable has a word value
    By Nathan the noob in forum C++ Programming
    Replies: 2
    Last Post: 06-26-2008, 02:44 AM

Tags for this Thread