简体   繁体   中英

calculating x to power y using for loop

I've tried to use the for loop for calculating x to power y. The program is running but giving errors.

To my knowledge, the error must be in "z" statements but I cannot figure it out. Help me if you encounter my mistakes.

#include<stdio.h>
#include<conio.h>

void main()
{

    int x,y,i;
    long int z=x;

    printf("Enter the values of x and y: ");
    scanf("%d %d",&x,&y);

    for(i=2;i<=y;i++)   
        z*=x;     ```
                  /*e.g-  Let x=2, y=3, then as per intialization z=x=2
                          since,from the for condition, (i=2)<=3, which is true
                          z= z*x =>2*2 => 4 now z=4
                          now i++ => i=3
                          (i=3)<=3,which is true
                          z= z*x =>4*2 => 8
                          therefore, 2 to power 3 is 8 */ 

    printf("%d to power %d is %ld",x,y,z);
    getch();

}

You are assigning z to x before x is assigned a value. z then has an indeterminate value, which messes up calculation of the power.

You have to wait until x is assigned from user input before using its value to initialize z .

Also, when reading input from scanf , it's a best practice to check its return value to ensure that all intended values were read successfully:

if(scanf("%d %d", &x, &y) != 2)
{
    // x and y were not properly read - handle error
}

z = x;

EDIT: @chux - Reinstate Monica pointed out in the comments that if y == 0 the code still has a problem. Anything to the power of zero (except zero itself, since x y is not continuous at the origin) is 1. You have to handle that special case as well.

You are initializing your z variable (to be equal to x ) before you have assigned a value to x ! To fix this, move the declaration/initialization of z to after your scanf call:

    //..
    int x,y,i;
//  long int z=x; // Means nothing: as "x" is here undefined, so "z" will also be!

    printf("Enter the values of x and y: ");
    scanf("%d %d",&x,&y);
    long int z = x; // Here, we have (probably) a value for "x" so we can copy it to "z"
    //..

EDIT: Maybe I'm drifting a bit 'off-topic' here, but you may have a background in programming languages that use reference variables (C++ or C#)? In such languages, what you are trying to do may work! For example, in C++, you could have int& z = x; (where you have your current declaration), and that could work in some circumstances (although, in your code, it actually won't, as pointed out in the comments). However, in "plain old C," code is executed where you put it, and there's no such thing as a "reference variable."

First you might want to initialize those variables

long int x = 0, y = 0;
long int z = 0;

Here you should check if scanf was successful

printf("Enter the values of x and y: ");
scanf("%ld %ld",&x,&y);

About scanf return value. From cppreference

Return value 1-3) Number of receiving arguments successfully assigned (which may be zero in case a matching failure occurred before the first receiving argument was assigned), or EOF if input failure occurs before the first receiving argument was assigned. 4-6) Same as (1-3), except that EOF is also returned if there is a runtime constraint violation.

Now the problem is you're assigning z the value of x before either is initialized properly. So that is an undefined behavior.

This is what you want

long int x = 0, y = 0;
long int z = 0;

printf("Enter the values of x and y: ");
scanf("%ld %ld",&x,&y);

z = x;

Also you can define a new int variable inside the loop. I personally find this method better.

for(int i = 2; i <= y; i++) 
    z *= x;

For the print statement, you might want to use %ld format for long int

printf("%ld to power %ld is %ld",x,y,z);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM