Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,924 questions

51,857 answers

573 users

How to get the first two digits of int number in C

2 Answers

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

int main(void) {
    int n = 8405961;
    printf("n: %d\n", n);

    int tmp = n, first_two_digits = n; 
    while (n) {
        first_two_digits = tmp;
        tmp = n;
        n /= 10;
    }   
    
    printf("first two digits: %d\n", first_two_digits);
    
    return 0;
}


  
/*
run:
  
n: 8405961
first two digits: 84
 
*/

 



answered Jan 15, 2021 by avibootz
0 votes
#include <stdio.h>
#include <math.h>

int main(void) {
    int n = 8405961;
    printf("n: %d\n", n);

    int totaldigits_minustwo = (int)log10(n) - 1; 
 
    int first_two_digits = (int)(n / pow(10, totaldigits_minustwo));
    
    printf("first two digits: %d\n", first_two_digits);
    
    return 0;
}



  
/*
run:
  
n: 8405961
first two digits: 84 
 
*/

 



answered Jan 15, 2021 by avibootz

Related questions

1 answer 158 views
1 answer 193 views
2 answers 199 views
1 answer 160 views
1 answer 161 views
161 views asked Dec 1, 2020 by avibootz
1 answer 156 views
...