Code:#include<stdio.h> //to print armstrong no between 100 to 500 int main() { int a,n,i,j,c; for(j=100;j<=500;j++) { int d=0; n=100; n++; a=n; for(i=1;i<=3;i++) { c=a%10; a=a/10; d=d+c*c*c; } if(d==n) printf("%d armstrong no\n",d); } return 0; }
Code:#include<stdio.h> //to print armstrong no between 100 to 500 int main() { int a,n,i,j,c; for(j=100;j<=500;j++) { int d=0; n=100; n++; a=n; for(i=1;i<=3;i++) { c=a%10; a=a/10; d=d+c*c*c; } if(d==n) printf("%d armstrong no\n",d); } return 0; }
What's wrong with your program, besides having poor indentation and variable names?
You need to give us more detail. What output do you get when you run it? What output are you expecting?
Also, using better variable names that describe what each variable is used for, would be helpful. That would let both you and us more easily understand what the logic in your code is doing, and possibly spot mistakes. For example, c could be called digit, d could be called digit_sum, etc.
program is running successfully but it is not printing armstrong no. between 100 to 500
It's not printing anything.
Code:#include<stdio.h> int main() { int temdigit,digit,i,j,c; for(j=100;j<=500;j++) // for running code 500 times { int d=0; digit=100; digit++; temdigit=digit; for(i=1;i<=3;i++) { c=a%10; // c= last digit of the number a=a/10; //a=remaining digit of the number d=d+c*c*c; //d= total sum of the cubes of the digit } if(d==n) //checking whether total sum ==original number printf("%d armstrong no\n",d); } return 0; }
So how do you know if it's running successfully if it's not printing the numbers? That doesn't sound successful to me. However, it prints them just fine for me:
EDIT: Sounds like a IDE config issue. Perhaps look here: http://faq.cprogramming.com/cgi-bin/...&id=1043284385Code:$ ./foo 153 armstrong no 370 armstrong no 371 armstrong no 407 armstrong no
n undeclared
a undeclared
Don't forget to rename your variables in your code.
Your variable digit gets the value 101 every loop and does not change.
Therefore your program will only print stuff if d is equal to 101Code:digit=100; digit++;
You need to put this before your first for loop:Code:if(d==digit) //checking whether total sum ==original number printf("%d armstrong no\n",d);
Code:digit=100;
Fact - Beethoven wrote his first symphony in C