top of page
Writer's picturesocialcontentclub

Program to check the given number is an Armstrong or not

What is Armstrong Number?

  • Armstrong Number is also known as narcissistic number.

  • A number which have n digits can be say that Armstrong number if its sum of nth powers of its digits is equal to original number.

  • Like Armstrong number of 3 digits is a number which is equal to the sum of cube of its digits.




C :

#include <stdio.h>

int main()

{

int num, originalNum, remainder, result = 0, n = 0;

printf("Enter an integer: ");

scanf("%d", &num);

originalNum = num;

while (originalNum != 0)

{

originalNum /= 10;

++n;

}

originalNum = num;

while (originalNum != 0)

{

remainder = originalNum % 10;

result += pow(remainder, n);

originalNum /= 10;

}

if (result == num)

printf("%d is an Armstrong number.", num);

else

printf("%d is not an Armstrong number.", num);

return 0;

}


C++ :

#include <iostream>

#include <cmath>

using namespace std;

int main()

{

int num, originalNum, remainder, result = 0, n = 0;

cout `oaicite:{"index":0,"invalid_reason":"Malformed citation << \"Enter an integer: \";\n cin >>"}` num;

originalNum = num;

while (originalNum != 0)

{

originalNum /= 10;

++n;

}

originalNum = num;

while (originalNum != 0)

{

remainder = originalNum % 10;

result += pow(remainder, n);

originalNum /= 10;

}

if (result == num)

cout << num << " is an Armstrong number.";

else

cout << num << " is not an Armstrong number.";

return 0;

}


Java:

import java.util.Scanner;


public class ArmstrongNumber {


public static void main(String[] args) {


int num, originalNum, remainder, result = 0, n = 0;

Scanner scanner = new Scanner(System.in);

System.out.println("Enter an integer: ");

num = scanner.nextInt();

originalNum = num;

while (originalNum != 0) {

originalNum /= 10;

++n;

}

originalNum = num;

while (originalNum != 0) {

remainder = originalNum % 10;

result += Math.pow(remainder, n);

originalNum /= 10;

}

if (result == num)

System.out.println(num + " is an Armstrong number.");

else

System.out.println(num + " is not an Armstrong number.");

}

}


Python:

num = int(input("Enter an integer: "))

originalNum = num

result = 0

n = len(str(num))

while originalNum != 0:

remainder = originalNum % 10

result += remainder ** n

originalNum //= 10

if result == num:

print(num, "is an Armstrong number.")

else:

print(num, "is not an Armstrong number.")


6 views0 comments

Related Posts

See All

Comments


bottom of page