Showing posts with label Octal Number. Show all posts
Showing posts with label Octal Number. Show all posts

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;
}

C Program to Convert a Decimal number to Octal Number

How to write a C Program to Convert a Decimal number to Octal Number in C Programming Language ?


Solution:

  1. #include <stdio.h>
  2. #include <conio.h>
  3. void main()
  4. {
  5.  int a,no[100],i,c,n=0;
  6.  clrscr();
  7.  printf("\nEnter Decimal Number: ");
  8.  scanf("%d",&a);
  9.  for(i=0;a!=0;++i)
  10.  {
  11.   c=a%8;
  12.   no[i]=c;
  13.   a=a/8;
  14.   ++n;
  15.  }
  16.  printf("\nThe Octal equivalant of decimal number is: ");
  17.  for(i=n-1;i>=0;--i)
  18.  {
  19.   printf("%d",no[i]);
  20.  }
  21.  getch();
  22. }