+ 2
Is it possible to write a c program to find amstrong numbers within the given range using 2 for loops?
7 Réponses
+ 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);
}
}
+ 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..
+ 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);
}
}
+ 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?
+ 1
Thanks brother 😊👍
+ 1
You're Wel come
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?