Wednesday, February 6, 2019

Given Number is Armstrong Number or Not - using "C-Language" in Ubuntu.

Armstrong Number(Or Narcissistic Number):
An Armstrong number is a number such that the sum of its digits raised to the no.of digits power is equal to the number itself.
Explnation:To declare a number is an Armstrong number, first determine how many digits are in the number. Call that n. Then take every digit in the number and raise it to the n power. Add all those together, and if your answer is the original number then it is an "Armstrong Number".
FileName : armstrong.c

#include <stdio.h>
void main()
{
  int n, t, rm, res=0; 
  printf("Enter a three digit integer: ");
  scanf("%d",&&n);
  t=n;
  while (t!=0)
  {                                             
    rm=t%10;             
    res=res+rm*rm*rm;     
    t=t/10;                     
  }
  if(res==n)
     printf("%d Is an Armstrong Number.",n);
  else
     printf("%d Is not an Armstrong Number.",n);
}

Output1:
Enter a three digit integer:153
153 Is an Armstrong Number.
Output2:
Enter a three digit integer:370
370 Is an Armstrong Number.
Output3:
Enter a three digit integer:371
371 Is an Armstrong Number.
Output4:
Enter a three digit integer:125
125 Is not an Armstrong Number.

Note 1:The above code works exclusively with 3-digit numbers, however you may play with the code by replacing the line "res=res+rm*rm*rm;", with your requirement.
Ex:
For  3-digit  ---> res=res+rm*rm*rm;
For 4-digit --->res=res+rm*rm*rm*rm;
For 5-digit --->res=res+rm*rm*rm*rm*rm;
and so on....
Note 2:
All single digit numbers are Armstrong numbers. For example, test 6. One digit.
 6^1 = 6. Armstrong number.
There are no 2-digit Armstrong numbers.
There are four 3-digit Armstrong numbers.
Ex : 153, 370, 371, 407
Test 371. Three digits. 3^3 + 7^3 + 1^3 = 27 + 343 + 1 = 371. Armstrong number.
There are three 4-digit Armstrong numbers.
Ex: Test 1634. Four digits. 1^4 + 6^4 + 3^4 + 4^4 = 1 + 1296 + 81 + 256 = 1634. Armstrong number.
Ex : 1634, 8208, 9474
There are three 5-digit Armstrong numbers.
Ex : 54748, 92727, 93084
Test : 5^5 + 4^5 + 7^5 + 4^5 + 8^5 = 3125 + 1024 + 16807 + 1024 + 32768 = 54748

No comments:

Post a Comment

Hadoop Commands

HADOOP COMMANDS OS : Ubuntu Environment Author : Bottu Gurunadha Rao Created: 31-Jan-2022 Updated: 31-Jan-2022 Release  : 1.0.1 Purpose: To ...

Search This Blog