Whenever i type include zero as an input, it doesn't run

Viewed 41

This program converts binary to octal

#include <stdio.h>
#include <math.h>


int main (){
    int num, i, j;
    
    printf("Enter number: ");
    scanf("%d", &num);
    int temp1 = 0;
    int temp2 = 0;
    int oct = 0;
    int nw; 
    
    for (i = 0; num != 0; i++){ 
        nw = num%1000;
        
        
        printf("\ni: %d \nnw: %d\n", i, nw);
        for (j = 0; nw != 0; j++){
                if (nw%10==1){
                temp1 += (po1w(2, j));
                nw/=10;
                }
            }
            
        temp2 = temp1 * pow(10,i); 
            
        printf("\nOct for now: %d\n", temp2);
        
        oct += temp2;
        
        temp1 = 0;
        temp2 = 0;
        num/=1000;
        
    
    }
    
    printf("\nThe Octal is %d", oct);
};

But whenever it is ran, it doesn't except when the inputted numbers are just few binary numbers and without zero, but whenever it has zero in the binary it doesn't run at all

1 Answers

Nevermind, I actually solved it. The problem lies inside the nested loop, if statement,

 for (j = 0; nw != 0; j++){
        if (nw%10==1){
        temp1 += (po1w(2, j));
        nw/=10;
        }
    }

Instead of

for (j = 0; nw != 0; j++){
            if (nw%10==1){
            temp1 += (po1w(2, j));
            }
            nw /= 10;
        }
Related