Wednesday, February 13, 2013

Write a program in C++ of Armstrong number - Armstrong number in C++ - C++ Armstrong number Program

A Armstrong Number Program in C++:

C program to find Armstrong number : Armstrong numbers are those numbers in which sum of cubes of individual digits is equal to the number. For example 371 = 33 + 73 + 13 = 27 + 343 +1 =371. Another example of Armstrong number is 153 =  13 + 53 + 33 = 1+ 125 + 27 = 153.  Now how we can achieve the same logic using C++ program.


#include<stdio.h>
#include<iostream.h>
#include<conio.h>
void main()
{
int sum=0,n,m;
clrscr();
cin>>n;
m=n;
while(n>0)
{
m=n%10;
sum=sum+(m*m*m);
n=n/10;
}
if(m==sum)
{
cout<<"the given number is amstrong";
}
else
{
cout<<"the given number is not amstrong";
}
getch();
}

A C++ program to count the Armstrong number 1 to 1000:


#include<stdio.h>

#include<conio.h>



main()

{

   int r;

   long number = 0, c, sum = 0, temp1;



   printf("Enter the maximum range upto which you want to find armstrong numbers ");

   scanf("%ld",&number);



   printf("Following armstrong numbers are found from 1 to %ld\n",number);



   for( c = 1 ; c <= number ; c++ )

   {

      temp1 = c;

      while( temp != 0 )

      {

         r = temp1%10;

         sum = sum + r*r*r;

         temp1 = temp1/10;

      }

      if ( c == sum )

         printf("%ld\n", c);

      sum = 0;

   }



   getch();

   return 0;

}


3 comments:

  1. Armstrong Program in C++

    Armstrong number is a number that is the sum of its own digits each raised to the power of the number of digits is equal to the number itself.
    For example 153 is armstrong number, 132 is not prime number. Armstrong program in c++ is very simple and easy to write.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete