Sunday 30 June 2013

Write a program using user defined function to print all the Armstrong numbers between 1 and 500.

01.
#include<stdio.h>
main()
{
int N, temp, N1, N2, N3;
printf("All Armstrong numbers between 1 and 500:\n\n");
N = 001;
while (N <= 500)
{
N1 = N - ((N / 10) * 10);
N2 = (N / 10) - ((N / 100) * 10);
N3 = (N / 100) - ((N / 1000) * 10);
temp = (N1*N1*N1) + (N2*N2*N2) + (N3*N3*N3);
if (temp == N)
{
printf("\nAmstrong Number:%d", temp);
}
N++;
}
getch ();
}


02.


#include<stdio.h>
int main(){
    int num,r,sum,temp;

    for(num=1;num<=500;num++){
         temp=num;
         sum = 0;

         while(temp!=0){
             r=temp%10;
             temp=temp/10;
             sum=sum+(r*r*r);
         }
         if(sum==num)
             printf("%d ",num);
    }

    return 0;
}

No comments:

Post a Comment