Thursday 9 August 2012

Loop control structure017


Write a program to print out all Armstrong numbers between 1 and 500. For example, 153 = ( 1 * 1 * 1 ) + ( 5 * 5 * 5 ) + ( 3 * 3 * 3 ).
N.B. [ Armstrong Numbers : - 1, 153, 370, 371, 407. ]

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

void main ( )
{
        int           no = 1, no1, md, s;
        clrscr ( );
                          
        printf ("\n\t ARMSTRONG NUMBER FROM 1 TO 500 \n\n");
                    
        while ( no <= 500 )
        {                        
                       no1 = no;
                       s = 0;
                       while ( no1 > 0 )
                       {                    
                                        md = no1 % 10;
                                        no1 = no1 / 10;
                                        s = s + ( md * md * md );
                       }                                  
                       if ( no == s)
                                   printf ("\t %d", no);
                       no ++;
        }
        getch ( );
}

Click here to contact us

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...