首页 > 解决方案 > 二进制到八进制的转换。但是二进制数字在数组中?

问题描述

//这个对吗?

#include<stdio.h>  
#include<math.h>
main()  
{  
    int a[1000], n, i,sum=0,b=0,p=0,t=1,c,j=0; 
    printf("Enter the number to convert:");
    scanf("%d",&n);
    for(i=0; n>0; i++)    
    {    
        a[i]=n%2;    
        n= n/2;  
    }    
    printf("Binary of the given number= ");    
    for(i=i-1 ;i>=0 ;i--)    
    {    
        printf("%d",a[i]); 
        j++;     
    }      
    printf("\n");
    for(c=j-1;c>=0;c--)
    {
        t=pow(8,c)*a[p];
        sum=sum+t;
        p++;
    }
    printf("\noctal %d",sum);
}
//this code is working perfectly till binary conversion but the last for loop is not working properly. Eventhough i think the logic is correct.

标签: cbinaryoctal

解决方案


推荐阅读