Why it is not show all number??? Only get 0 but not get 1 , 153,370,371,407 | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
0

Why it is not show all number??? Only get 0 but not get 1 , 153,370,371,407

Armstrong between 100 and 500 in c++?? https://sololearn.com/compiler-playground/cE6eZw5J5dYK/?ref=app

18th Feb 2024, 12:39 AM
Anuj Khare
Anuj Khare - avatar
1 Réponse
+ 3
Hi Anuj Khare every time value of variable 'm' is always 0 #include<iostream> using namespace std ; int main(){ int s,m; for(int i=0;i<500;i++) { int sum=0; cout<<m; //0 while(m>0) /*(0>0) giving false so not going inside while loop*/ { s=i%10; sum+=s*s*s; m/=10; } if(sum==i){ /* for first iteration of for loop value of i is 0 so going inside if block and printing 0 as sum=0 */ cout<<"armstrong number :"<<i<<endl; } } return 0; } To fix it inside the for loop add m=i and inside the while loop do s=m%10 instead of s=i%10 as if we use latter it will change the value of i and is needed at the end to compare with variable 'sum'.
18th Feb 2024, 1:39 AM
𝘕𝘉
𝘕𝘉 - avatar