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;
 
}}}

Dani AI

Generated

A few small issues combine to break the program: an undeclared variable was referenced, the while was effectively empty because of a trailing semicolon, main should return an int, input isn't validated, and the loop that computes the power needs to iterate the correct number of times. , and already pointed pieces of this out — below is a compact, working example that forces the user to enter a power in the range 0..10 and handles bad input cleanly.

#include <stdio.h>

int main(void)
{
    int base, power;
    long result;
    int i, ch;

    printf("Enter the base number: ");
    if (scanf("%d", &base) != 1) return 1;

    /* require 0 <= power <= 10 */
    while (1) {
        printf("Enter the nth power (0..10): ");
        if (scanf("%d", &power) != 1) {
            /* discard the rest of the line on bad input */
            while ((ch = getchar()) != '\n' && ch != EOF) ;
            printf("Invalid input. Please enter an integer.\n");
            continue;
        }
        if (power < 0) {
            printf("Power must be non-negative.\n");
            continue;
        }
        if (power > 10) {
            printf("Please enter a number less than or equal to 10.\n");
            continue;
        }
        break;
    }

    result = 1;
    for (i = 0; i < power; ++i)
        result *= base;

    printf("The base %d raised to the %dth power is %ld\n", base, power, result);
    return 0;
}

Notes: always check scanf's return to avoid stuck input, and flush the input buffer after a failed read. Avoid stray semicolons after loop headers (they create empty loops). Limit the exponent to avoid overflow — even long can overflow for large bases; use long long or big-integer libraries if you need larger ranges. If you want fractional or negative exponents use double and pow() from math.h (but watch rounding). Compile with warnings enabled (for example: gcc -Wall -Wextra -std=c11) and fix any warnings the compiler reports.

Recommended Answers

All 3 Replies

1. Please edit your post to useto 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.