// TERNARY to DECIMAL
#include <stdio.h>
#include <math.h>
#include <string.h>
int main()
{
int n=0,POWER=0,i,j;
char ar1[20];
char ar2[3] = {'0','1','2'};
printf("Enter any Ternary Number : ");
scanf("%s",&ar1);
for(i=strlen(ar1)-1;i>=0;i--)
{
for(j=0;j<3;j++)
{
if(ar1[i]==ar2[j])
{
n=n+j*pow(3,POWER);
}
}
POWER++;
}
printf("\nDecimal Number = %d",n);
}
//Alternate Method
//Ternary to Decimal
#include <stdio.h>
int main()
{
int n,ter=0,temp=1,REM;
printf("Enter any Ternary Number : ");
scanf("%d",&n);
while(n>0)
{
REM = n%10;
ter = ter + REM*temp;
n = n/10;
temp = temp*3;
}
printf("Decimal Number : %d",ter);
return 0;
}
No comments:
Post a Comment
Note: only a member of this blog may post a comment.