integers... help

This is a discussion on integers... help within the C Programming forums, part of the General Programming Boards category; hi, i'm trying to do run this program... but there's something wrong... when i tried entering 34567 the result would ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    1

    Post integers... help

    hi, i'm trying to do run this program...
    but there's something wrong...
    when i tried entering 34567
    the result would be negative integers, don't know why
    please i need your help

    insert
    Code:
     
    #include<stdio.h>
    int main()
    {
    int input;
    int first;
    int second;
    int third;
    int fourth;
    int fifth;
    
    printf("Enter your digit: ");
    scanf("%d", & input);
    printf("\n");
    
    first = input % 10;
    second = (input % 100)/10;
    third = (input % 1000)/100;
    fourth = (input % 10000)/1000;
    fifth = (input % 100000)/10000;
    
    printf("first: %d", first);
    printf("second: %d", second);
    printf("third: %d", third);
    printf("fourth: %d", fourth);
    printf("fifth: %d", fifth);
    
    return 0;
    
    }

  2. #2
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    I dont see the output as negative integer....

  3. #3
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    999
    Are you compiling this under DOS or somewhere else where integers would be 16-bits and bound between -32768 and 32767? There's nothing wrong with your code that I can see...you could try it with longs instead of ints.

  4. #4
    Registered User
    Join Date
    Dec 2009
    Location
    /dev/pts/0
    Posts
    29
    May I suggest you indent your source code? It helps dramatically with source code readability . . .

    Code:
    #include <stdio.h>
    
    int main() {
        int input;
        int first, second, third, fourth, fifth;
    
        printf("Enter your digit: ");
        scanf("%d", & input);
        printf("\n");
    
        first = input % 10;
        second = (input % 100)/10;
        third = (input % 1000)/100;
        fourth = (input % 10000)/1000;
        fifth = (input % 100000)/10000;
    
        printf("first: %d", first);
        printf("second: %d", second);
        printf("third: %d", third);
        printf("fourth: %d", fourth);
        printf("fifth: %d", fifth);
    
        return 0;
    }
    And yes, as previously mentioned, old compilers (such as Turbo C) and old platforms (such as the 8086) had a maximum range for ints of -32,768 to 32,767 (32-bit platforms and compilers usually have a range of -2,147,483,647 to 2,147,483,648, some 64-bit platforms may define the long type to be from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807).

    If you are using a compiler old enough to have the int type as 16 bits, then I strongly suggest you find another compiler.
    -- strange

    There is no Darkness in Eternity
    Only Light too dim for us to see

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  2. Integers into array.
    By livestrng in forum C Programming
    Replies: 10
    Last Post: 10-30-2008, 12:35 AM
  3. Pass by reference
    By jrice528 in forum C++ Programming
    Replies: 4
    Last Post: 10-30-2007, 02:02 PM
  4. Scanning integers from a file:
    By xarmenx in forum C Programming
    Replies: 6
    Last Post: 11-29-2004, 04:57 PM
  5. Replies: 6
    Last Post: 08-04-2003, 11:57 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21