# For loop practice help

Not sure where I’m going wrong here. In this program, I’m trying to find if scanned variable k is a power of the scanned variable n. So, if you input 243 and 5, your program finds any numbers that would equal 243 if brought to the 5th power. The program printfs if there are no such numbers. Below you can see my attempt: https://code.sololearn.com/cjk6JcHtoRC3/?ref=app

4/11/2021 3:45:49 PM

Alejandro7 Answers

New AnswerAlejandro Piña Change sum=1; Also I have made some changes in for loop check it #include <stdio.h> int main(void) { int n,k,i,j; int sum=1,done=0; scanf("%i %i",&n,&k); for(i=1;i<=n/2; i++) { for(j=1;j<=k; j++) { sum *= i; if(sum==n) { done=1; printf("%i = %i^%i",n,i,k); break; } } sum = 1; } if(done == 0) { printf("%i is not a %ith power",n,k); } Also refer this for taking input as 1 and 0 https://code.sololearn.com/cfviDdFxjG9S/?ref=app

Hi Alejandro Piña, I have done some mods on your code. I took some help from math.h (pow(), sqrt()) , Hope Thats ok. https://code.sololearn.com/cUtk982RMowB/?ref=app

Hey Arun Ruban SJ, Your code unfortunately fails for numerous tests. For example, it fails for 1 and 7. 17 and 1. Also for 5096 and 3, oddly it prints the output twice. Just a couple things.

Sorry the if statement comes outside the 2nd for loop #include <stdio.h> int main(void) { int n,k,i,j; int sum=1,done=0; scanf("%i %i",&n,&k); for(i=1;i<n/2; i++) { for(j=1;j<=k; j++) { sum *=i; } if(sum==n) { done=1; printf("%i = %i^%i",n,i,k); break; } sum = 1; } if(done == 0) { printf("%i is not a %ith power",n,k); } return 0; } I don't understand what's with 5096 and 3 ,it worked when I checked. For numbers 1 and 0 the above program won't give answers, you have to give special conditions. Please refer this and change accordingly https://code.sololearn.com/cfviDdFxjG9S/?ref=app