Code.GeekInterview.com
 
Code Samples C
 

Check if the input number is a Amstrong number or not


Code ResourceAuthor: Jimmy Zorald  

Difficulty Level:

Published: 25th May 2007   Read: 6279 times  

Filed in: C
Add Comment


 

 

Sponsored Links


 

 

This is a very simple code that checks if a number is a Amstrong number or not. This is mostly useful for beginners who are starting to learn C programming.

 


Sample Code
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <math.h>
  6.  
  7. void main()
  8. {
  9.         long num1,num2=0,mcount=0, nums;
  10.         char str1[35], strc;
  11.  
  12.         clrscr();
  13.         printf("Enter a number : ");
  14.         scanf("%ld",&num1);
  15.  
  16.          ltoa(num1,str1,10);
  17.          while (mcount <= (strlen(str1)-1))
  18.          {
  19.           strc = str1[mcount];
  20.           nums = atoi(&strc);
  21.           num2 = num2+pow(nums,3);
  22.           mcount = mcount+1;
  23.          }
  24.  
  25.          if (num1 == num2)
  26.          {
  27.           printf("Amstrong Number : %ld",num2);
  28.          }
  29.          else
  30.          {
  31.           printf("Not a Amstrong Number.");
  32.          }
  33.    getch();
  34. }
  35.  
Copyright GeekInterview.com


Next Article: Glittering Eyes - Animation Effect


 

Latest Code Samples

 

Popular Code Samples

 

Related Code Samples

 

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    



Popular Coders

# Coder NameHits
1. kaivalya198932322
2. mano.mithun19258
3. deepu0817249
4. meefriend4ever7826
5. chandrikakr7817
6. shashivaishnav6692
7. sadasivathavamani6568
8. venkatakrishnansvpr6442
9. Jimmy Zorald6280
10. sripri5433

Active Coders

Refined Tags