what is the code for exponent in dev c++?
is it possible with just using int, scanf and printf? those are the only codes we're allowed to use
what exactly are you trying to do?
im trying to do exponent using something like this int a, b, c printf("Enter a number: "); scanf("%d", &a); printf("Enter second number: "); scanf("%d", b); c = a + b printf("The sum of %d + %d = %d", a, b, c); i believe that's the code for sum
in that case, to calculate a^b do int ii; c= 1; for (ii=1; ii<=b; ii++) c *= a; this will work for b>=0.
we could also handle -b, but we would have to go to float rather than int
what does your code mean?
short answer a^b you don't have any error checking on the input, so the code is making the assumption we have an integer "a" and a non-negative integer "b" it initializes the answer c to 1 it goes into a loop starting at 1. if b>=1 it executes the loop, otherwise it is done and c=1 is the answer (presumably because b is 0, though we don't check for neg values) if b=1 it executes the loop exactly once, and finds c= c*a or 1*a or a (c *= a is short for c= c*a) if b= n, it executes the multiplication n times. at the end, c= a^b
how would it look like in code form?
it is code. see if you can get it to compile (just make sure all the variables have been defined)
hmm ok..it's just that the only codes taught to us were "int", "printf", "scanf", "getch" and "if"..i don'tknow if my teacher will accept foreign codes. isn't it possible to produce exponents with just those basic codes?
because it seems you introduced some new codes
I assume you have not learned about functions which are more advanced than a loop. if then else can only do limited work. You could do something like if (b==0) c=1 else if (b==1) c= a; else if (b==2) c=a*a; end; but that is very, very , very ugly. look in your book for the loop construct, which is the most natural way to do this problem
lol we don't have a book :)) so a loop is the only way?
Join our real-time social learning platform and learn together with your friends!