[ create a new paste ] login | about

Link: http://codepad.org/LQKLC6rk    [ 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>
#include <stdlib.h>
#include <math.h>



void base3function(int, int);
 
int main() {
	int mybase=102, myexponent=2;

//	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];

	printf("Base: %d Exponent: %d \n", base, exponent);


	/* 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
6
Base: 102 Exponent: 2 
Decimal Base: -144089343
Decimal Exponent: -4111802
-144089343^-4111802 = 1
1
102^2 = 1


Create a new paste based on this one


Comments: