What is the code to print only the first number of a value? ex: only the 8 of 8372883

Asked

Viewed 414 times

0

#include <stdio.h>

int main(){
    int a, b, c, d;
    printf("Insira um numero de quatro digitos: ");
    scanf("%i",&a);
    b=a%10;
    c=a%100;
    d=a%1000;
    printf("%i\n%i\n%i\n%i",a,d,c,b);
    return 0;
}

2 answers

4

First digit

To get only the first digit of a number, you can calculate the number of digits using base logarithm 10 and with that quantity you get only the first digit through a division.

Example with the number 123:

  • Calculate basic logarithm 10 that gives 2 which is the number of digits minus 1.
  • Create a factor of 10 raised to the previous number, 2, that is to say 10^2 that gives 100
  • Make an entire division between 123 and 100 that gives 1 and corresponds to the first digit

In c in your code:

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

int main(){
    int a;
    printf("Insira um numero de quatro digitos: ");
    scanf("%i",&a);

    int digitos_menos_1 = log10(a);
    int fator = pow(10, digitos_menos_1);
    int primeiro_digito = a / fator; //a é o numero
    printf("%i", primeiro_digito);

    return 0;
}

Watch it work on Ideone

Note that I additionally had to include the library math.h to use the function log10 and the function pow.

All digits

Now if the goal is to show all the digits as you think in your original code then it is better to go another way, this way that general rule is easier until.

For this you can take the original value and get the last digit with the operator module on 10, making % 10, then divide the value by 10 to delete the last digit that has already been used. Repeat this process until the number reaches 0:

#include <stdio.h>

int main(){
    int a;
    printf("Insira um numero de quatro digitos: ");
    scanf("%i",&a);

    printf("Digitos a começar pelo ultimo:\n");
    while(a != 0){
        printf("%d\n", a % 10); //imprimir só o ultimo
        a /= 10; //eliminar o ultimo
    }

    return 0;
}

See this also on Ideone

Note that the digits have been shown in reverse order, starting from the one to the right. It can also do the opposite on the left, but it is necessary to opt for other, slightly more complicated solutions.

The most direct is even using recursiveness, thus:

#include <stdio.h>

void imprime_digito(int numero){
    if (numero != 0){ //se não terminou por chegar a 0
        imprime_digito(numero / 10); //chama de novo para o próximo digito
        printf("%d\n", numero % 10); //imprime
    }
}

int main(){
    int a;
    printf("Insira um numero de quatro digitos: ");
    scanf("%i",&a);

    printf("Digitos:\n");
    imprime_digito(a); //chama a função recursiva

    return 0;
}

Code running on Ideone

  • 1

    +1 for using Log to calculate splitter without using while. Thanks, I learned something cool today :D

0

Thus?

#include <stdio.h>

int main(){
    int a, primeiro;
    printf("Insira um numero: ");
    scanf("%i",&a);

    primeiro=a;
    while(primeiro >= 10)
    {
       primeiro = primeiro / 10;
    }

    printf("Primeiro digito = %d", primeiro);

    return 0;
}

Val for any number, regardless of the number of digits.

  • is a code that goes along with %i, but I think it controls the decimals only...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.