Is it possible to write a c program to find amstrong numbers within the given range using 2 for loops? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Is it possible to write a c program to find amstrong numbers within the given range using 2 for loops?

24th Mar 2020, 9:55 AM
Muhammed Shafi
Muhammed Shafi - avatar
7 Answers
+ 1
Muhammed Shafi You just need to set sum=0 for next all iteration. Edit: Corrected code: #include<stdio.h> int main() { int sum,n,m,temp,i,j; printf("Enter the first number:"); scanf("%d",&n); printf("Enter the second number:"); scanf("%d",&m); sum=0; for(i=n;i<m;i++) { sum=0; //added this... for(j=i;j!=0;j=j/10) { temp = j%10; sum = sum +( temp*temp*temp); } if(sum==i) printf("%d,",sum); } }
24th Mar 2020, 3:25 PM
Jayakrishna 🇮🇳
+ 1
Please find it by yourself and ask if you not succeed by sharing code... Reason: Yes. It is possible.. Infact that is common solution..
24th Mar 2020, 10:35 AM
Jayakrishna 🇮🇳
+ 1
I tried once. I have written the program using a for loop and a while loop. Need to know whether it is possible by using 2 for loops instead of a for and a while loop like this:- #include<stdio.h> void main() {int sum,n,m,temp,i,j; printf("Enter the first number:"); scanf("%d",&n); printf("Enter the second number:"); scanf("%d",&m); sum=0; for(i=n;i<m;i++) {for(j=i;j!=0;j=j/10) {temp = j%10; sum = sum +( temp*temp*temp); } if(sum==i) printf("%d,",sum); } }
24th Mar 2020, 12:40 PM
Muhammed Shafi
Muhammed Shafi - avatar
+ 1
I couldn't get favourable output with the above code. I need to know what is the change I need to implement in my program?
24th Mar 2020, 2:57 PM
Muhammed Shafi
Muhammed Shafi - avatar
+ 1
Thanks brother 😊👍
24th Mar 2020, 3:28 PM
Muhammed Shafi
Muhammed Shafi - avatar
+ 1
You're Wel come
24th Mar 2020, 3:32 PM
Jayakrishna 🇮🇳
0
I don't understand it. You have already solution.. What else your question? It there any changes needed in your program? Is this not what you need? Are you got your answer or not?
24th Mar 2020, 2:37 PM
Jayakrishna 🇮🇳