简体   繁体   中英

evaluating “long long int ” in c

I need to make a user to type an long long int value in c and then evaluate this value example

long long int x = 1234567;

Then my evaluation needs to know the no. of elemente and digit at each position:

if(digit at 2nd position == 2)
{
do something;
}else{
 do something else;  
}

i had tried to convert it into an array but the problem is how do i get the array size in advance when i dont know what a user types eg one user might type 1234 some other might type 211212121212 ..

Print the long long to string with sprintf and then access the result by character.

Edit: if you use this approach you must also handle the negative value case.

Convert number to string then use char at index 1 to check second number as follows:

char buf[20] = 0;
long long int number = 1234567LL;
snprintf(buf, 20, "%lld", number);
if (buf[1] == '2'){ // second char is 2
}
else{
}

code:

#include<stdio.h>
char* str(char* buf, long long int number)
{
    snprintf(buf, 21, "%lld", number);
    return buf;
}
int main(){
    long long int number = 1234567LL;
    char buf[20] = "";
    if(str(buf, number)[1] == '2'){ 
        printf("yes");
    }
    else{   
        printf("No");
    }
    printf("\n");
    return 0;
}

Mathematically you could use ceil(log10()) to determine the # of digits, and/or a series of divisions/modulus/subtractions to determine the value of digits at X position. Converting to a char array would be the easiest solution as mentioned (sprintf, itoa).

#include <stdio.h>

int main(void)
{
    long long int x = 1234567, i = x;

    while (i >= 100) i /= 10;
    printf("%lld\n", i % 10); /* prints the second digit */
    return 0;
}

Or put it in a function (works also for negatives):

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

/* returns x digit or -1 */
int xdigit(long long int x, int pos)
{
    long long int e = pow(10, pos);

    if (x < 0) x = -x;
    if (x < e / 10) return -1;
    while (x >= e) x /= 10;
    return x % 10;
}

int main(void)
{
    long long int x = 1234567;

    printf("%s\n", xdigit(x, 2) == 2 ? "yes" : "no");
    return 0;
}

Must be compiled with -lm for math.h

try using the folowing

double num = 23.345;

int intpart = (int)num;

double decpart = num - intpart;

printf("Num = %f, intpart = %d, decpart = %f\\n", num, intpart, decpart);

now you have go the string from the decimal and integer part ... now proceed with the calculation by multiplying the decimal with 10 and save each in an array(say). Alternatively you can also multiply the long decimal by 1 followed by the num of digits(say 5 for 5 points after decimal) & later can took them with an % opeartor.

Do you really need a long long int? If the user inputs a number as a string,

char bignum[64];
int ii

fgets(bignum, sizeof(bignum), stdin);

for (ii = 0; ii < strlen(bignum); ++ii)
   printf("character %d is %c", ii, bignum[ii]);
please give me comment on the code    
Thy this code it uses division to find of number of digits
and uses division and modulo to digits at different positions
#include <stdio.h>
void main()
{
long long int i,x = 1234567;
int n=0;
i=x;    //make a copy of x
//block to find number of digit
for(;i!=0;i/=10)
{
n++;
}
printf("number of digits %d\n",n);
//Display digits
for(;x!=0;x/=10,n--)
{
printf("element at %d is %d\n",n,x%10);
}
}

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