Hi i have this program and have a problem it is a basic program for exponents and now i need to add a while loop so that if the user enters a number greater that 10 it asks them to input a number lower or equal to 10, here is my code can someone help me?.

``````#include <stdio.h>
main( )
{
/*  y  */
/* calculate x  */
/*    */
/* where x and y are integers and y >= 0 */
int base, power;
long result = 1;
int counter = 0;

while ( x <= 10);

printf("Enter the base number : ");
scanf("%d",&base);
printf("Enter the nth power to which base will be raised");
scanf("%d",&power);
{
for (counter++ < power)
result = result * base;
}
printf("The base of %d raised to",base the %dth power is %
ld\n",power,result);

return 0;

}}}``````

## All 3 Replies

1. Please edit your post to use code tags to preserve spacing and tabs.

2. main() must be declared like this:

``````int main()
{
// blabla

return 0;
}``````

I realize neither of the above answer your questions, but fix those and we will look at your post again.

x is not declared as a variable anywhere that I can see. this should give a complier error.
Also, your while statement is terminated by a semicolon. This would work for do-while statement but not when the using a while by itself. If using a while in this way it should be followed by braces surrounding the code to be executed during each iteration of the loop.

``while ( x <= 10);``

Also, your while statement is terminated by a semicolon

This also has another problem, bacause when x is declared the program will just stay on this line ( while(1) )

``for (counter++ < power)``

I think what you're trying to do is:

``````for (counter = 0; counter < power; counter++)
{
}``````
``result = result * base;``

hmmm, int to long conversion....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.