Monday 17 September 2012

c program to generate and print armstrong numbers


DESCRIPTION:
armstrong number in c: This program prints armstrong number. In our program we ask the user to enter a number and then we use a loop from one to the entered number and check if it is an armstrong number and if it is then the number is printed on the screen. Remember a number is armstrong if the sum of cubes of individual digits of a number is equal to the number itself. For example 371 is an armstrong number as 33 + 73 + 13 = 371. Some other armstrong numbers are 0, 1, 153, 370, 407.
C code
#include<stdio.h>
#include<conio.h>

main()
{
   int r;
   long number = 0, c, sum = 0, temp;

   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++ )
   {
      temp = c;
      while( temp != 0 )
      {
         r = temp%10;
         sum = sum + r*r*r;
         temp = temp/10;
      }
      if ( c == sum )
         printf("%ld\n", c);
      sum = 0;
   }

   getch();
   return 0;
}
Armstrong number program executable.
OUTPUT;
Enter the maximum range upto which you want to find armstrong numbers1000
 Following armstrong numbers are found from 1 to 1000
1
153
370
371
407
purpose of returning zero

Every c program begins with function main,
and main is called by operating system of your computer .
 So we return a value to os indicating whether our program
has successfully executed or an error has occurred. Returning zero
indicates success i.e. program has been executed successfully.armstrong number c program
.CASE1:
armstrong number c program: c programming code to check
whether a number is armstrong or not
DESCRIPTION:
 A number is armstrong
if the sum of cubes of individual digits of a number
is equal to the number itself. For example 371 is an
armstrong number as 33 + 73 + 13 = 371.
Some other armstrong numbers are: 0, 1, 153, 370, 407.
C programming code
#include <stdio.h>

main()
{
   int number, sum = 0, temp, remainder;

   printf("Enter a number\n");    
   scanf("%d",&number);

   temp = number;

   while( temp != 0 )
   {
      remainder = temp%10;
      sum = sum + remainder*remainder*remainder;
      temp = temp/10;
   }

   if ( number == sum )
      printf("Entered number is an armstrong number.");
   else
      printf("Entered number is not an armstrong number.");        

   return 0;
}
OUTPUT;
ENTER A NUMBER
407
ENTER NUMBER IS AN ARMSTRONG NUMBER

2 comments: