Practice Program







  • Program of Converting Decimal to Octal.
  • Solution: through While loop
    void main()
    {
        int i,j=0,n;
        int a[30];
    
        printf("Enter the decimal number:");
        scanf("%d",&n);
    
    
        while(n!=0)
        {
            a[j] = n%8;//storing the remainder at every iteration as element in an array
            n = n/8;//at every iteration we are dividing 'n' with 2
               //and storing its quotient as a new value in of  'n' variable.
         j++;
        }
         j = j-1;
         printf("Octal Number is: ");
        while(j>=0) //printing the binary form of  a number. 
        {
            printf("%d",a[j]);
            j--;
        }
    
    }
    

    Output1
    Enter the decimal number: 12

    Octal Number is: 14 

    Output2
    Enter the decimal number: 32

    Octal Number is: 40 

    Output3
    Enter the decimal number: 44

    Octal Number is: 54




    Solution:from do-while loops
    void main()
    {
        int i,j=0,n;
        int a[30];
    
        printf("Enter the decimal number:");
        scanf("%d",&n);
    
    
       do
        {
            a[j] = n%8;//storing the remainder at every iteration as element in an array
            n = n/8;//at every iteration we are dividing 'n' with 2
               //and storing its quotient as a new value in of  'n' variable.
         j++;
        }while(n!=0);
         j = j-1;
          printf("Octal Number is: ");
         do
        //printing the binary form of  a number. 
        {
            printf("%d",a[j]);
            j--;
        }while(j>=0);
    
    }
    





    defining a function 'do_octal' which will print decimal number in a Octal form.
    void do_octal(int n)
    {
         int j=0;
        int a[10];
    
    
    
    
        while(n!=0)
        {
            a[j] = n%8;//storing the remainder at every iteration as element in an array
            n = n/8;//at every iteration we are dividing 'n' with 2
               //and storing its quotient as a new value in of  'n' variable.
         j++;
        }
         j = j-1; //decrementing the value of j by 1,
            //because at current j value there is a garbage value.
    
        while(j>=0)
        {
            printf("%d",a[j]);
            j--;
        }
    }
    

    Using above define function 'do_octal()':
    void main()
    {
    printf("Octal Number is:` ");
    do_octal(12);
    }
    

    Output1
    Octal Number is: 14

    Using above define function 'do_octal()':
    void main()
    {
    printf("Octal Number is: ");
    do_octal(32);
    }
    

    Output2
    Octal Number is: 40

    Using above define function 'do_octal()':
    void main()
    {
    printf("Octal Number is: ");
    do_octal(44);
    }
    

    Output3
    Octal Number is: 54