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

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;
}
    
asked by anonymous 05.09.2018 / 10:33

2 answers

4

First digit

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

Example with the number 123 :

  • Calculate base logarithm 10 giving 2 which is the number of digits minus 1.
  • Create a factor of 10 raised to the previous number, 2 , or 10^2 that gives 100
  • Perform an integer 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;
}

See it working on Ideone

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

All digits

Now if the goal is to show all the digits as it gives idea in its original code then it is better to follow by another way, this way that generally is easier until.

For this you can get the original value and get the last digit with the modulo operator on 10 , making % 10 , then divide the value by 10 to delete the last digit that was already 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 in Ideone

Notice that the digits have been shown in reverse order, starting from the far right. You can also do the reverse from the left but you have to opt for slightly more complicated solutions.

The most direct is even using recursion, like this:

#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 to run on Ideone

    
05.09.2018 / 11:58
0

So?

#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.

    
05.09.2018 / 11:53