Printing a number into Different Base

Problem Statement
Write a C program which reads two non-negative integers b and n,
b <= 9, obtains the equivalent representation of n to the base b in a
variable n To The Base b of type int and prints n, b and n To The Base b
with suitable message .
#include<stdio.h>
void convert_to_base(int n,int b);
int main()
{
  int n,b;
  printf("enter the number \n");
  scanf("%d",&n);
  printf("enterhte base whch is less then 9 \n");
  scanf("%d",&b);
  if(b>9)
    {
      printf("enter a valid base \n");
    }
  convert_to_base(n,b);
  return 0;
}
void convert_to_base(int n,int b)
{
  int a[10],counter,temp;
  printf("now i print the converted verson of %d to the base %d\n",n,b);
  counter=0;
  do
    {
      a[counter]=n%b;
      n=n/b;
      counter++;
    }
  while(n>0);
  for(temp=counter-1;temp>=0;temp--)
    {
      printf("%d",a[temp]);
    }
  printf("\n");
  return ;
}

No comments:

Post a Comment