i tried to pass one number by one within given range from for loop to while loop to check for armstrong. but the value passed from for loop into while loop is not accepting. so any suggestion

/*program to find armstrong within a given range */
#include<stdio.h>
#include<math.h>

int main(){


	int num,num1;
	
   	int i,sum=0;
	int dig=0;
	printf("\n enter the range to find armstrong no\n");
	scanf("%d",&num);
	num1=num;
        
		for(i=0;i<num;i++){
				
			dig=0;
			sum=0;
       	
			 while(i!=0)
			{
                        		      dig=i%10;
			      sum=sum+dig*dig*dig;
	   		      i=i/10;
        			 } 

	
		  	     if(sum==i)
			
                 			 printf("\n its armstrong :%d\n",i);
		
     }       
    
    
}

Recommended Answers

All 4 Replies

In the while loop you need to change variable i to something else because that while loop is destroying the value of i that the for loop is supposed to increment. Copy the value of i into another variable, such as k and use k in that while loop.

:(

hi thank you very much for the help and i didnt realized about this mistake till now...but even after i send only copy of i value in k within while loop its not working. i notice only first iteration from the range is passed to while loop from the for loop and give result but after that it goes into infinite loop and didnt receive value from for loop

what you have done is correct upto while after while when you try to compare with sum the value of i by then would be 0.so, check the following
1.assign i to two variables
k=i;
m=i;
2.in the while work with k
while(k!=0)
3.compare sum with m
if(sum==m)

/*program to find armstrong within a given range */
2.
#include<stdio.h>
3.
#include<math.h>
4.
#include<conio.h>
5.
int main(){
6.

7.

8.
int num,num1;
9.

10.
int i,sum=0;
11.
int dig=0;
12.clrscr();

printf("\n enter the range to find armstrong no\n");
13.
scanf("%d",&num);
14.
num1=num;
15.

16.
for(i=1;i<num;i++){
17.

18.k=i;
m=k;
dig=0;
19.
sum=0;
20.

21.
while(k!=0)
22.
{
23.
dig=k%10;
24.
sum=sum+dig*dig*dig;
25.
k=k/10;
26.
}
27.

28.

29.
if(sum==m)
30.

31.
printf("\n its armstrong :%d\n",i);
32.

33.
}
34.

35.getch();

36.
}
i have tried this this will give you solution
and was not working properly because you have initialised i to 0 and checking the value not to be 0
bye.

commented: The most puke-inducing attempt at posting code I've seen in many a year - do everyone a favour and learn how to post FORMATTED CODE!!!!! -3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.