Program to print all Armstrong numbers Between n and m using For loop
- Write a program to print all Armstrong numbers Between n and m using For loop in C
- Write a program to print all Armstrong numbers Between n and m using For loop in C++
- Write a program to print all Armstrong numbers Between n and m using For loop in Python
- Write a program to print all Armstrong numbers Between n and m using For loop in PHP
- Write a program to print all Armstrong numbers Between n and m using For loop in Java
- Write a program to print all Armstrong numbers Between n and m using For loop in Java Script
- Write a program to print all Armstrong numbers Between n and m 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:
- Define a function to check if a number is an Armstrong number.
- Start from n and keep checking numbers until m
- Print each Armstrong number as it is identified.
Program to print all Armstrong numbers Between n and m using For loop
-
C
-
C++
-
Python
-
PHP
-
JAVA
-
Java Script
-
C#
#include <stdio.h> #include <math.h> int is_armstrong(int num) { int original = num, remainder, result = 0, n = 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) { remainder = original % 10; result += pow(remainder, n); original /= 10; } return result == num; // Check if Armstrong } int main() { int n, m; printf("Enter the value of n: "); scanf("%d", &n); printf("Enter the value of m: "); scanf("%d", &m); printf("Armstrong numbers between %d and %d are:\n", n, m); // Loop to find and print Armstrong numbers between n and m for (int num = n; num <= m; num++) { if (is_armstrong(num)) { printf("%d ", num); } } return 0; }
#include <iostream> #include <cmath> using namespace std; bool is_armstrong(int num) { int original = num, remainder, result = 0, n = 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) { remainder = original % 10; result += pow(remainder, n); original /= 10; } return result == num; // Check if Armstrong } int main() { int n, m; cout << "Enter the value of n: "; cin >> n; cout << "Enter the value of m: "; cin >> m; cout << "Armstrong numbers between " << n << " and " << m << " are:" << endl; // Loop to find and print Armstrong numbers between n and m for (int num = n; num <= m; num++) { if (is_armstrong(num)) { cout << num << " "; } } 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 the value of n: ")) m = int(input("Enter the value of m: ")) print(f"Armstrong numbers between {n} and {m} are:") # Loop to find and print Armstrong numbers between n and m for num in range(n, m+1): if is_armstrong(num): print(num, end=" ")
<div class="contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950"><div class="overflow-y-auto p-4" dir="ltr"><code class="!whitespace-pre hljs language-php"><?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 the value of n: "); $m = (int)readline("Enter the value of m: "); echo "Armstrong numbers between $n and $m are:\n"; // Loop to find and print Armstrong numbers between n and m for ($num = $n; $num <= $m; $num++) { if (is_armstrong($num)) { echo "$num "; } } ?>
import java.util.Scanner; public class ArmstrongNumbers { 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 the value of n: "); int n = scanner.nextInt(); System.out.print("Enter the value of m: "); int m = scanner.nextInt(); System.out.println("Armstrong numbers between " + n + " and " + m + " are:"); // Loop to find and print Armstrong numbers between n and m for (int num = n; num <= m; num++) { if (isArmstrong(num)) { System.out.print(num + " "); } } 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 the value of n: ")); let m = parseInt(prompt("Enter the value of m: ")); console.log(`Armstrong numbers between ${n} and ${m} are:`); // Loop to find and print Armstrong numbers between n and m for (let num = n; num <= m; num++) { if (isArmstrong(num)) { console.log(num); } }
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 the value of n: "); int n = int.Parse(Console.ReadLine()); Console.Write("Enter the value of m: "); int m = int.Parse(Console.ReadLine()); Console.WriteLine($"Armstrong numbers between {n} and {m} are:"); // Loop to find and print Armstrong numbers between n and m for (int num = n; num <= m; num++) { if (IsArmstrong(num)) { Console.Write(num + " "); } } } }