PLAY WITH CODING

Home Basic Program If & Else Loops Conversion Pattern logo

Program Based on Conversion

Program to convert a decimal number into any base(decimal to penta)

Input - Output

Input
Enter a decimal number : 23

Output
Penta equivalent of given number : 43

Input
Enter a decimal number : 12

Output
Penta equivalent of given number : 22

Algorithm

Step 1: START

Step 2: Enter a number 'num' as an input.

Step 3: Calculate remainder i.e(num%5) and store it into an array

Step 4: Divide the num by 5 i.e (num/5)

Step 5: Repeat the step 3 and 4 until the num is greater than zero

Step 6: END

CODE


/* Decimal to Penta Conversion */

#include <stdio.h>
int main()
{
      int num,penta[10],i,j;
      printf(" Enter the decimal number : ");
      scanf("%d",&num);
      for(i=0;num>0;i++)
    {
        penta[i]=num%5;
        num/=5;
    }
      printf(" Penta notation of given number is = ");
      for(j=i-1;j>=0;j--)
      printf("%d",penta[j]);
    return 0;
}


/* Decimal to Penta Conversion */

#include <iostream>
using namespace std;
int main()
{
      int num,penta[10],i,j;
      cout<<" Enter the decimal number : ";
      cin>>num;
      for(i=0;num>0;i++)
    {
        penta[i]=num%5;
        num/=5;
    }
      cout<<" Penta notation of given number is = ";
      for(j=i-1;j>=0;j--)
      cout<<penta[j];
    return 0;
}

OutPut

Enter a decimal number : 23

Penta equivalent of given number : 43