One of the questions given to us for the lab exam was to generate the first 10 armstrong numbers. I could generate upto 5 numbers correctly..the rest are wrong.

#include<stdio.h>
#include<conio.h>

void main()
{
 int n=1,a,b,i=1,r;
 clrscr();
 while(i<=10)
 {
  a=0;
  b=n;
  do
  {
   r=b%10;
   a=a+r*r*r;
   b=b/10;
  }while(b>0);
  if(a==n)
  {
   printf("\t%d",n);
   i++;
   n++;
  }
  else
   n++;
 }
 getch();
}

the output i got is :
1 153 370 371 407 -729 -216 -125 -64 -1

What should I do to get the correct output?

Is it possibe to generate ten armstrong numbers?? I could not even find what the 6th armstrong number is?

An n-digit number that is the sum of the nth powers of its digits is called an n-narcissistic number. It is also sometimes known as an Armstrong number, perfect digital invariant (Wolfram MathWorld)

To me, the reason that you fail to produce those numbers is because you have defined the Armstrong Number incorrectly.

i did it that way 'cos thats how my teacher explained...and when i searched for Armstrong numbers in Daniweb..I found this link posted by Ancient Dragon.
http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap04/arms.html
It says "An Armstrong number is a number such that the sum
of its digits raised to the third power is equal to the number
itself."

Anyway Thanks..I'll try it this way..

If an Armstrong number is a number such that the sum of its digits raised to the third power is equal to the number itself, then I am sure that there are only 5 Armstrong numbers that does exist in this world. Practically, when the number of digit grow, the sum of raising to the third power of each digit won't able to produce larger enough result to be equal to the number.

For example:
99999 = 9^3 + 9^3 + 9^3 + 9^3 + 9^3 = 3645.

The above example has proved that even we have the largest number in each digit we still cannot produce larger enough result to be equal to its number.

From http://homepages.cwi.nl/~dik/english/mathematics/armstrong.html:

An Armstrong number is an n-digit base b number such that the sum of its (base b) digits raised to the power n is the number itself.

a = a^1 { 1, 2, 3, 4, 5, 6, 7, 8, 9 }
ab = a^2 + b^1 {}
abc = a^3 + b^3 + c^3 { 153, 370, 371, 407 }
abcd = a^4 + b^4 + c^4 + d^4 { 1634, 8208, 9474 }
and so on...

#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
 int n1=1,a,n2,i=1,r,dig=0;
 clrscr();
 while(i<=10)
 {
  a=0;
  n2=n1;
  do
  {
   n2=n2/10;
   dig++;
  }while(n2>0);
  n2=n1;
  while(n2>0)
  {
   r=n2%10;
   a=a+pow((double)r,(double)dig);
   n2=n2/10;
  }
  if(a==n1)
  {
   printf("\n%d",n1);
   i++;
   n1++;
  }
  else
   n1++;
 }
 getch();
}

The program is not even running..
Can somebody tell me where I went wrong?

The problem that your code doesn't run properly is because you keep increase the dig without reseting it. To solve this, you can simply reset the dig to 0 in the beginning of the loop.

while(i<=10) {
   a=0;
   dig=0; // reset it to 0
   n2=n1;

   do {
       n2 = n2 / 10;
       dig++;
   } while (n2 > 0);
....
....
}

One more thing, next time, you should try to choose better name for your variable. It is idea good to name your variable that could express its purpose. To be honest, your code is very hard to read since there are n1, n2, a, b which look so similar to each other.

In addition to invisal's post:
Don't compute p raised to the power dig with floating point arithmetic pow function. It's too expensive. Better write your own simple integer pow function, for example:

int ipow(int k, int n)
{
    int kn = k;
    while (n-->1)
        kn *= k;
    return kn;
}

It's far from the fastest method but it's better than pow(). Think about using table (array) of powers (there are only ten digits for base 10 ;)).

Think about better condition for the last loop. No need to continue calculations if the partial sum of powers is greater than the number (it's not one of Armstrong numbers).

Look at for loops in C textbook: your while loops are for loops simulations.

Use "%d\n", not "\n%d" format to print the next number.

Thank you everybody..for your help and suggestions..

Thanks everybody..for your help and suggestions..

If you want to calculate Armstrong Numbers with more than three digits, it is important to re-iterate the loop uptil the Number of Digits in that Integer. It is also commonly known as Narcissistic Numbers. Here, the loop calculates till the end of the Number of Digits in that given integer. Here, they have described a very good example and a perfectly executable code for Narcissistic Numbers:

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.