Program to print all Armstrong numbers Between 1 and n using For loop
- Write a program to print all Armstrong numbers Between 1 and n using For loop in C
- Write a program to print all Armstrong numbers Between 1 and n using For loop in C++
- Write a program to print all Armstrong numbers Between 1 and n using For loop in Python
- Write a program to print all Armstrong numbers Between 1 and n using For loop in PHP
- Write a program to print all Armstrong numbers Between 1 and n using For loop in Java
- Write a program to print all Armstrong numbers Between 1 and n using For loop in Java Script
- Write a program to print all Armstrong numbers Between 1 and n 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 1 and keep checking numbers until n
- Print each Armstrong number as it is identified.
Program to print all Armstrong numbers Between 1 and n 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; printf("Enter the value of n: "); scanf("%d", &n); printf("Armstrong numbers between 1 and %d are:\n", n); // Loop to find and print Armstrong numbers for (int num = 1; num <= n; 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; cout << "Enter the value of n: "; cin >> n; cout << "Armstrong numbers between 1 and " << n << " are:" << endl; // Loop to find and print Armstrong numbers for (int num = 1; num <= n; 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: ")) print(f"Armstrong numbers between 1 and {n} are:") # Loop to find and print Armstrong numbers for num in range(1, n+1): if is_armstrong(num): print(num, end=" ")
<?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: "); echo "Armstrong numbers between 1 and $n are:\n"; // Loop to find and print Armstrong numbers for ($num = 1; $num <= $n; $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.println("Armstrong numbers between 1 and " + n + " are:"); // Loop to find and print Armstrong numbers for (int num = 1; num <= n; 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: ")); console.log(`Armstrong numbers between 1 and ${n} are:`); // Loop to find and print Armstrong numbers for (let num = 1; num <= n; 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.WriteLine($"Armstrong numbers between 1 and {n} are:"); // Loop to find and print Armstrong numbers for (int num = 1; num <= n; num++) { if (IsArmstrong(num)) { Console.Write(num + " "); } } } }