[ create a new paste ] login | about

Link: http://codepad.org/DBKUcV9K    [ raw code | output | fork ]

C, pasted on Aug 13:
/*
* Write a program to compute x^y based on using base-3 presentation of a number
*/
#include <stdio.h>

void base3function(int, int);
 
int main() {
	int mybase, myexponent;

	printf("Enter base number in Base-3 format: ");
	scanf("%d", &mybase);

	printf("Enter exponent number in Base-3 format: ");
	scanf("%d", &myexponent);

	base3function(mybase, myexponent);

	printf("\n");

	return 0;
}

void base3function(int base, int exponent) {
	int decimalbase, decimalexponent, i, result=1, remainder, quotient=1, num, val=1, numdigits=0;
	int base3result[50], base3finalresult[50];

	/* Converting Base-3 numbers in Decimal */
	num = base;
	while (num > 0) {
		remainder = num % 10;
		decimalbase = decimalbase + remainder * val;
		num = num / 10 ;
		val = val * 3;
	}
	printf("Decimal Base: %d\n", decimalbase);

	/* Converting Base-3 numbers in Decimal */
	num = exponent;
	val=1;
	while (num > 0) {
		remainder = num % 10;
		decimalexponent = decimalexponent + remainder * val;
		num = num / 10 ;
		val = val * 3;
	}
	printf("Decimal Exponent: %d\n", decimalexponent);

     
	/* Calculate base^exponent */
	for(i = 0; i < decimalexponent; i++){
		result = result * decimalbase;
	}
	printf("%d^%d = %d\n", decimalbase, decimalexponent, result);

	/* Converting Decimal Result in Base-3 number */
	for(i=0; quotient != 0; i++) {
		remainder = result%3;
		base3result[i] = remainder; 

		quotient = result/3;
		result = quotient;

		printf("%d", base3result[i]);
	}
	numdigits = i-1;

	printf("\n%d^%d = ", base, exponent);

	for(i=0; numdigits>=0; i++) {
		base3finalresult[i] = base3result[numdigits]; 
		numdigits--;
		printf("%d", base3finalresult[i]);
	}
}


Output:
1
2
3
4
5
Enter base number in Base-3 format: Enter exponent number in Base-3 format: Decimal Base: -7968546
Decimal Exponent: -7985676
-7968546^-7985676 = 1
1
134513601^-7985784 = 1


Create a new paste based on this one


Comments: