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,951 questions

51,893 answers

573 users

How to convert decimal to octal in C

2 Answers

0 votes
#include <stdio.h>

int main(void)
{
    long decimal = 253;
    int octal[8], i = 1;

    while(decimal != 0) {
        octal[i++] = decimal % 8;
        decimal = decimal / 8;
    }
    
    for (i = i - 1; i > 0; i--)
        printf("%d", octal[i]);

    return 0;
}

 
 
  
/*
run:
      
375
 
*/

 



answered Aug 24, 2021 by avibootz
0 votes
#include <stdio.h>

int decimalToOctal(int decimal) {
    int octal = 0, i = 1;

    while (decimal != 0) {
        octal += (decimal % 8) * i;
        decimal /= 8;
        i *= 10;
    }

    return octal;
}
 
int main(void)
{
    long decimal = 253;

    printf("%d", decimalToOctal(decimal));
 
    return 0;
}
 
  
  
   
/*
run:
       
375
  
*/

 



answered Jul 23, 2022 by avibootz

Related questions

1 answer 120 views
120 views asked Aug 25, 2021 by avibootz
1 answer 144 views
1 answer 169 views
1 answer 114 views
114 views asked Aug 25, 2021 by avibootz
2 answers 178 views
178 views asked Aug 24, 2021 by avibootz
1 answer 132 views
...