简体   繁体   中英

odd numbers in language C? pb 'ram or algorithm'!

This program gives us the position of the odd numbers in a given integer, this program works well, but when I give him an integer in its numbers are greater than 10 -like 123456789123-, it doesn't work. I do not know if is a problem of ram or algorithm ?

#include<stdio.h>
#include<stdlib.h>

main(){

    int a,b;
    int i = 0;

    scanf("%d",&a);

    while(a/10!=0){
        b=a%10;
        if(b%2!=0)
            printf("\nodd number position: %d",i);
        a=a/10;
        i++;
    }

    if(a%2!=0)
        printf("\nodd number position: %d",i);

    system("pause");
}

The problem is one of processor (architecture) rather than RAM. On your platform the size of an int seems to be 32 bits which cannot hold a number as large as 123456789123. As Groo commented to Raon, you could use a string instead (if you don't plan to do any calculations on the number):

char a[1024] = {0}; /* should be plenty, but extend the array even more if needed */

fgets(a, sizeof a, stdin); /* fgets is recommended as it doesn't overflow */

int i, length = strlen(a);

for(i = 0; i < length; i++){
    /* count your odd digits here
       left as an exercise to the reader */
    /* note that you must access the individual digits using a[i] */
}
#include<stdio.h>

void main() {

    int i;
    char s[256];

    scanf("%s",s);

    for( i=0; s[i]!=0; ++i ) {
        /*int digit = s[i]-48;
        if( digit%2==1 ) break;
        - or even shorter: */
        if( s[i]%2==1 ) break;
    }

    if( s[i]!=0 )
        printf( "First odd digit position: %d", i );
    else
        printf( "All digits are even" );
}

Here is working sample: http://cfiddle.net/sempyi

Every data type is limited to specific range.for example char is limited to range -128 to 128 . if you use the beyond this range. You might get unexpected results.

In your program if you give any number which is beyond the range of integer, then you will get unexpected results

if your int size is 4 byte/32-bit you can give input with in this range –2,147,483,648 to 2,147,483,647

if Your int size is 2 byte/16-bit you can give input with in this range –32,768 to 32,767

Check this Data Type Ranges .

And if you want to give large Numbers You can declare variable as long int/long long int and don't forgot to change format specifier when using long int(%ld) and long long int(%lld)

You can also use string and check whether all characters are digits are not by using isdigit() function in ctype.h header and convert character digit into integer digit by substracting '0' (character zero) . and check whether is that odd or not.

The problem is that 123456789123 exceed the storage limit for an integer data type, try using a string to store the value and parse it, something like

#include<stdio.h>
int main(){
    char a[] = "12345678912345678913246798";
    int i = 0;
    for (i=0; a[i] != '\0'; i++){
        if ( a[i] % 2 != 0 ) printf("%c is odd\n", a[i]);
    }
    return 0;
}

1234567891230x1CBE991A83因此,如果int是32位,则您的数字将被截断(到31977048350xBE991A83 )。

I think this program will not give proper answer if you give more than 10 digits! please correct me if I am wrong. The max Unsigned integer value is 4294967295 (in any 32 bit processor). if the given value is more than that then it will either limit to that max value or overflow will happen. So if you give a integer which is more than 4294967295 it will not work as it supposed to.

try printing the input. In that case you will know whether complete number is sent or Max number is sent to find the odd number's position.

One way to make it work is read the input number as array of characters and then try to figure out the odd number position.

Note: for signed integer maximum is 2147483647

Number you are giving input is greater than range of int. You need to change the data type Below link should help you.

http://www.tutorialspoint.com/ansi_c/c_basic_datatypes.htm

You need to choose a data type that matches the expected data range.

If you want your program to work for any number it is probably best to read the number one character at a time.

Code (not that in this code, position is counted with the most significant digit = 1, which is the other direction compared to your code):

  int c;
  unsigned long long pos = 0;
  while (++pos) {
    c = getc();
    if (c < '0' || c > '9') break; // Not a digit
    if ((c - '0')%2 != 0) {
            printf("\nodd number position: %ulld", pos);
    }
  }

The code can handle numbers that have a ridiculus amount of digits. Eventually the pos variable will overflow, though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM