Home > Blockchain >  how can convert decimals to any other bases without using arrays and strings
how can convert decimals to any other bases without using arrays and strings

Time:11-26

I know how can I convert decimals to any other kind of bases less than 10 but could we do this without using any arrays or strings in the C language?

  1. get n as a input number
  2. get b as a base that we want to convert to that.
  3. print the number in b base

CodePudding user response:

solve in this way:

  1. get n as an input number.
  2. get b as a base that we want to convert to that.
  3. print the number in b base here is a sum.

#include <stdio.h>

int main()
{
    long long int n;
    int b;
    long long int sum=0;
    int i=1;
    
    scanf("%lld %d",&n,&b);
    while(n>0)
    {
    
        sum =(n%b)*i;
        i*=10;
        n/=b;
    
    
    }
    printf("%lld",sum);
    return 0;
}
  •  Tags:  
  • c
  • Related