简体   繁体   English

使用 while 循环计算 x 的 y 次方的程序

[英]program for calculating x raised to the power of y using while loop

int main()
{ unsigned int x, y;

    printf_s("Enter value for x: ");
    scanf_s("%u", &x);

    printf_s("Enter value for y: ");
    scanf_s("%u", &y);

    unsigned int count = 1;
    while (count < y);
    {
        x *= x;
        count++;
    }

    printf_s("x raised to the power of y is: %u", x);
}

Hi, I'm not sure where I went wrong.嗨,我不确定我哪里出错了。 When I run it, I enter two values as prompted and then nothing else happens.当我运行它时,我按照提示输入两个值,然后没有其他反应。 It just stops after I enter they value for y.它只是在我输入它们的 y 值后停止。

Enter value for x: 3 Enter value for y: 2为 x 输入值:3 为 y 输入值:2

Like this.像这样。 Could someone point me in the right direction?有人能指出我正确的方向吗? I understand that this way will not work if y <= 1. But shouldn't it work for when y > 1?我知道如果 y <= 1,这种方式将不起作用。但是当 y > 1 时它不应该起作用吗?

I've searched for it.我已经搜索过了。 There is another question using for loop.使用 for 循环还有另一个问题。 I can see that it could be done with for loop but I think while loop is more appropriate since it gives more freedom.我可以看到它可以用 for 循环来完成,但我认为 while 循环更合适,因为它提供了更多的自由。 Please and thank you!谢谢,麻烦您了!

  1. Use functions使用函数
  2. you have ;你有; past the while and your code in braces is not executed in the loop过去while ,大括号中的代码不会在循环中执行
  3. use a larger integer type as the result as a "normal" unsigned int will wraparound quickly.使用更大的整数类型作为结果,因为“正常” unsigned int 将快速回绕。
  4. x *= x is definitely wrong. x *= x绝对是错误的。
unsigned long long mypow(unsigned x, unsigned y)
{
    unsigned long long result = 1;
    while(y--) result *= x;
    return result;
}


int main(void)
{
    unsigned x, y;

    scanf("%u,%u", &x, &y);

    printf("%u ^ %u = %llu\n", x, y, mypow(x, y));
}

https://godbolt.org/z/xzx8Mo5aW https://godbolt.org/z/xzx8Mo5aW

#include <iostream>

int main()
{

    unsigned int long x; // assign x
    printf_s("Enter value for x: "); // prompt
    scanf_s("%u", &x); // read input

    unsigned int y; // assign y
    printf_s("Enter value for y: "); // prompt
    scanf_s("%u", &y); // read input

    unsigned int count = 0; // initialize count
    unsigned int power = 1; // initialize power
    while (count < y) // loop while y is less than count
    {
        power *= x; // multiply power by x
        count++; // increment count

    } // loop ends

    printf_s("x raised to the power of y is: %u", power); // display result
}

I've deleted ;.我已经删除了;。 I've added long to unsigned int.我在 unsigned int 中添加了 long。 I've set up a new variable, power, in order to contain the value of x raised to a power of y.我设置了一个新变量 power,以包含 x 的 y 次幂的值。 I've checked if this works when y = 0 or 1 and it does.我已经检查了这在 y = 0 或 1 时是否有效,并且确实有效。 Thank you everyone for your help!谢谢你们每一个人的帮助!

PS Textbook answer has pritnf( "%s", "Enter first integer: "); PS教科书答案有 pritnf( "%s", "Enter first integer: "); for prompting for the first integer, x.用于提示输入第一个整​​数 x。 I'm not sure why they add %s as it works perfectly fine without it.我不确定他们为什么要添加 %s,因为没有它就可以正常工作。 Does anyone know why one would add %s?有谁知道为什么要添加 %s?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 for 循环计算 x 到 y 的幂 - calculating x to power y using for loop 计算幂到一个数 - Calculating power raised to a number 在循环中计算功率时值错误 - wrong value while calculating power in a loop 将x提升至n次方 - Getting x raised to the nth power 为什么我对这个问题的方法是错误的:Write a function power (int a, int b), to calculate the value of a raise to b using while loop in c - Why is my method for this question wrong: Write a function power (int a, int b), to calculate the value of a raised to b using while loop in c 使用 SSE Intrinsics 在浮点 x、y、z 数组上向量化循环计算长度和差异 - Vectorizing a loop over float x,y,z arrays calculating length and differences using SSE Intrinsics 使用循环计算long double的能力在c中给出错误的答案 - calculating power of long double using loop gives wrong answer in c 程序查找总和,如果输入“y”,则使用 do while 循环重复该过程 - Program to find sum and if entered 'y' then repeat the process using do while loop 如何解释这个使用位运算计算 3/4*x 的程序? - How to explain this program calculating 3/4*x using bit operation? -nan返回值/ e(欧拉)提升至功效计算循环 - -nan return value / e (euler) raised to a power calculation loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM