Program to Check if Given Number is Armstrong or Not using For loop
- Write a program to Check if Give Number is Armstrong or Not using For loop in C
- Write a program to Check if Give Number is Armstrong or Not using For loop in C++
- Write a program to Check if Give Number is Armstrong or Not using For loop in Python
- Write a program to Check if Give Number is Armstrong or Not using For loop in PHP
- Write a program to Check if Give Number is Armstrong or Not using For loop in Java
- Write a program to Check if Give Number is Armstrong or Not using For loop in Java Script
- Write a program to Check if Give Number is Armstrong or Not using For loop in C#
Explanation:
Logic
What is an Armstrong number?
- A number is an Armstrong number if the sum of its digits raised to the power of the number of digits equals the number itself.
- For example, 153 = 13 + 53 + 33.
Steps:
- Count the number of digits in the number.
- Calculate the sum of each digit raised to the power of the number of digits.
- Compare the sum to the original number.
Program to Check if Give Number is Armstrong or Not using For loop
-
C
-
C++
-
Python
-
PHP
-
JAVA
-
Java Script
-
C#
#include <stdio.h> #include <math.h> int main() { int num, originalNum, remainder, result = 0, n = 0; printf("Enter a number: "); scanf("%d", &num); originalNum = num; // Find the number of digits while (originalNum != 0) { originalNum /= 10; ++n; } originalNum = num; // Calculate the sum of each digit raised to the power of n while (originalNum != 0) { remainder = originalNum % 10; result += pow(remainder, n); originalNum /= 10; } // Check if the number is Armstrong or not if (result == num) printf("%d is an Armstrong number.\n", num); else printf("%d is not an Armstrong number.\n", num); return 0; }
#include <iostream> #include <cmath> using namespace std; int main() { int num, originalNum, remainder, result = 0, n = 0; cout << "Enter a number: "; cin >> num; originalNum = num; // Find the number of digits while (originalNum != 0) { originalNum /= 10; ++n; } originalNum = num; // Calculate the sum of each digit raised to the power of n while (originalNum != 0) { remainder = originalNum % 10; result += pow(remainder, n); originalNum /= 10; } // Check if the number is Armstrong or not if (result == num) cout << num << " is an Armstrong number." << endl; else cout << num << " is not an Armstrong number." << endl; return 0; }
def is_armstrong(num): original = num n = len(str(num)) # Find the number of digits result = 0 while num > 0: remainder = num % 10 result += remainder ** n num //= 10 return result == original # Check if Armstrong n = int(input("Enter a number: ")) if is_armstrong(n): print(f"{n} is an Armstrong number.") else: print(f"{n} is not an Armstrong number.")
<?php function is_armstrong($num) { $original = $num; $n = strlen((string)$num); // Find the number of digits $result = 0; while ($num != 0) { $remainder = $num % 10; $result += pow($remainder, $n); $num = (int)($num / 10); } return $result == $original; // Check if Armstrong } $n = (int)readline("Enter a number: "); if (is_armstrong($n)) { echo "$n is an Armstrong number.\n"; } else { echo "$n is not an Armstrong number.\n"; } ?>
import java.util.Scanner; public class ArmstrongNumber { public static boolean isArmstrong(int num) { int original = num, n = 0, result = 0; // Find the number of digits while (original != 0) { original /= 10; ++n; } original = num; // Calculate the sum of each digit raised to the power of n while (original != 0) { int remainder = original % 10; result += Math.pow(remainder, n); original /= 10; } return result == num; // Check if Armstrong } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a number: "); int n = scanner.nextInt(); if (isArmstrong(n)) { System.out.println(n + " is an Armstrong number."); } else { System.out.println(n + " is not an Armstrong number."); } scanner.close(); } }
function isArmstrong(num) { let original = num; let n = num.toString().length; // Find the number of digits let result = 0; while (num > 0) { let remainder = num % 10; result += Math.pow(remainder, n); num = Math.floor(num / 10); } return result === original; // Check if Armstrong } let n = parseInt(prompt("Enter a number: ")); if (isArmstrong(n)) { console.log(`${n} is an Armstrong number.`); } else { console.log(`${n} is not an Armstrong number.`); }
using System; class Program { static bool IsArmstrong(int num) { int original = num, n = 0, result = 0; // Find the number of digits while (original != 0) { original /= 10; ++n; } original = num; // Calculate the sum of each digit raised to the power of n while (original != 0) { int remainder = original % 10; result += (int)Math.Pow(remainder, n); original /= 10; } return result == num; // Check if Armstrong } static void Main() { Console.Write("Enter a number: "); int n = int.Parse(Console.ReadLine()); if (IsArmstrong(n)) { Console.WriteLine($"{n} is an Armstrong number."); } else { Console.WriteLine($"{n} is not an Armstrong number."); } } }