Hey Bucky can u figure out the mistake i did in the following loop? it gives value 0 after 4 loops

0 rasen itachi · May 31, 2015
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int x;
    int y;
    int z=0;


    printf("Enter the number. \n");
    scanf(" %d", &x);
       printf("Enter the power. \n");
       scanf(" %d", &y);

       while(z<y){printf("The %d is %d \n",z+1,x);
       x*=x;
       z++;

       }
return 0;
}

Post a Reply

Replies

Oldest  Newest  Rating
0 rasen itachi · June 1, 2015
Thanks Rich Sharma and c student
Also on wiki i found out that we can also directly use the pow(x,y) which gives x^y.
I'll rewrite the code again and check it out.
Thanks for the help again. 
0 c student · June 1, 2015
working fine for me, except that your algorithm seem to be wrong.  you are just squaring the number by y amount of times instead of multiplying it by its original number.

Enter the number.
2
Enter the power.
5
The 1 is 2
The 2 is 4
The 3 is 16 /* should be eight */
The 4 is 256 /* should be sixteen */
The 5 is 65536 /* should be thirty two */
0 Rich Sharma · June 1, 2015
one thing i would do if i were you is i would try to set a different value, of type int, call it anything you want, and set it to 1;
in your while loop multiply x to that integer, because in your example, it would run to long, giving x to the power of y +1, not y.
also, check if you got scan f right. i will test it on code blocks too and tell you what i found you did wrong.
  • 1

C

107,111 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator