w3resource

C Exercises: Evaluate the equation y=xn when n is a non-negative integer

C Basic Declarations and Expressions: Exercise-67 with Solution

Write a C program to evaluate the equation y=xn when n is a non-negative integer.

Sample Solution:

C Code:

#include <stdio.h>
int main(){
    int count, n;
    float x,y;
    
    // Input the values of x and n
    printf("Input the values of x and n:\n");
    scanf("%f%d", &x,&n);
    
    y=1.0;
    count=1;
    
    // Calculate x to the power n
    while(count<=n)
    {
        y=y*x;
        count++;
    }
    
    // Print the result
    printf("x=%f; n=%d; \nx to power n=%f", x, n,y);
    
    return 0;
}

Sample Output:

Input the values of x and n: 256
x=256.000000; n=0; 
x to power n=1.000000

Flowchart:

C Programming Flowchart: Evaluate the equation y=xn when n is a non-negative integer

C programming Code Editor:

Previous:Write a C program that generates 50 random numbers between -0.5 and 0.5 and writes them in a file rand.dat. The first line of ran.dat contains the number of data and the next 50 lines contains the 50 random numbers.
Next: Write a C program to print the powers of 2 table for the power 0 to 10, both positive and negative.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/c-programming-exercises/basic-declarations-and-expressions/c-programming-basic-exercises-67.php