How do I write the code for:
int a, b;
b = a^4 ; /* that doesn't work */
b = a * a * a; have I done so far...
Is there a right way
thank you all.
How do I write the code for:
int a, b;
b = a^4 ; /* that doesn't work */
b = a * a * a; have I done so far...
Is there a right way
thank you all.
here my code should help you...
#include <math.h>
#include <stdio.h>
int main()
{
int x, y, i ;
char yes;
char var;
int result = 1 ;
again:
printf("Please enter your calculation in this format: 6*5..\n\n");
scanf("%d%c%d",&x,&var,&y);
switch(var)
{
case '+':
printf("answer = %d\n",x+y);
break;
case '-':
printf("answer = %d\n",x-y);
break;
case '*':
printf("answer = %d\n",x*y);
break;
case '/':
printf("answer = %d\n",x/y);
break;
case '^':
{
for(i=0; i<y; i++)
result *=x;
printf("answer = %d\n\n",result);
break;
default:
printf("Wrong syntax please try again\n");
}
}
}
>b = a^4 ; /* that doesn't work */
Well, that depends on what you want to do. The ^-operator is the XOR operation.
>b = a * a * a; have I done so far...
So you want to work with powers? You could use the following pseudocode.
Code:pwr = 1; for i = 1 to n pwr = pwr * a;
there's the pow function in <math.h>
True. However it is slower than doing a*a*a*a.