C Program dec to oct

How to write a C Program Decimal to Octal Number,  ?


Solution:

#include <stdio.h>
#include <stdlib.h>

int dodaj()
{
    unsigned short int n;
    printf("Podaj liczbe: ");
    while(scanf("%hu", &n)!=1 || n<=0){
    printf("Blad danych!");
        while(getchar()!='\n');
    }
    return n;
}
int zamiana(unsigned short int n, int tab[31])
{
    int i=0,j;
    while(n>0){
    tab[i++]=n%8;
    n/=8;
    }
    j=i-1;
    return j;
}
void wyswietl(int tab[31], int j)
{
    for(j;j>=0;j--){
        printf("%d", tab[j]);
    }
}
int main()
{
    unsigned short int n;
    int j;
    int tab[31];
    n=dodaj();
    j=zamiana(n, tab);
    wyswietl(tab,j);
    return 0;
}


Learn More :