Program to print all Armstrong numbers Between n and m using While loop
- Write a program to print all Armstrong numbers Between n and m using While loop in C
- Write a program to print all Armstrong numbers Between n and m using While loop in C++
- Write a program to print all Armstrong numbers Between n and m using While loop in Python
- Write a program to print all Armstrong numbers Between n and m using While loop in PHP
- Write a program to print all Armstrong numbers Between n and m using While loop in Java
- Write a program to print all Armstrong numbers Between n and m using While loop in Java Script
- Write a program to print all Armstrong numbers Between n and m using While 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 While loop
-
C
-
C++
-
Python
-
PHP
-
JAVA
-
Java Script
-
C#
#include <stdio.h>
#include <math.h>
int power(int base, int exp) {
int result = 1;
for (int i = 1; i <= exp; i++) {
result *= base;
}
return result;
}
int isArmstrong(int num) {
int temp = num, sum = 0, digits = 0;
while (temp != 0) {
temp /= 10;
digits++;
}
temp = num;
while (temp != 0) {
sum += power(temp % 10, digits);
temp /= 10;
}
return sum == num;
}
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Armstrong numbers between 1 and %d are:\n", n);
for (int i = 1; i <= n; i++) {
if (isArmstrong(i)) {
printf("%d ", i);
}
}
return 0;
}
#include <iostream>
#include <cmath>
using namespace std;
int power(int base, int exp) {
int result = 1;
for (int i = 1; i <= exp; i++) {
result *= base;
}
return result;
}
bool isArmstrong(int num) {
int temp = num, sum = 0, digits = 0;
while (temp != 0) {
temp /= 10;
digits++;
}
temp = num;
while (temp != 0) {
sum += power(temp % 10, digits);
temp /= 10;
}
return sum == num;
}
int main() {
int n;
cout << "Enter a number: ";
cin >> n;
cout << "Armstrong numbers between 1 and " << n << " are:" << endl;
for (int i = 1; i <= n; i++) {
if (isArmstrong(i)) {
cout << i << " ";
}
}
return 0;
}
def is_armstrong(num):
digits = len(str(num))
temp = num
sum = 0
while temp != 0:
sum += (temp % 10) ** digits
temp //= 10
return sum == num
n = int(input("Enter a number: "))
print(f"Armstrong numbers between 1 and {n} are:")
for i in range(1, n + 1):
if is_armstrong(i):
print(i, end=" ")
<?php
function isArmstrong($num) {
$digits = strlen((string)$num);
$temp = $num;
$sum = 0;
while ($temp != 0) {
$sum += pow($temp % 10, $digits);
$temp = (int)($temp / 10);
}
return $sum == $num;
}
$num = (int)readline("Enter a number: ");
echo "Armstrong numbers between 1 and $num are: ";
for ($i = 1; $i <= $num; $i++) {
if (isArmstrong($i)) {
echo "$i ";
}
}
?>
import java.util.Scanner;
public class Armstrong {
public static boolean isArmstrong(int num) {
int temp = num, sum = 0, digits = 0;
while (temp != 0) {
temp /= 10;
digits++;
}
temp = num;
while (temp != 0) {
sum += Math.pow(temp % 10, digits);
temp /= 10;
}
return sum == num;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = sc.nextInt();
System.out.println("Armstrong numbers between 1 and " + n + " are:");
for (int i = 1; i <= n; i++) {
if (isArmstrong(i)) {
System.out.print(i + " ");
}
}
sc.close();
}
}
function isArmstrong(num) {
let temp = num, sum = 0;
let digits = num.toString().length;
while (temp !== 0) {
sum += Math.pow(temp % 10, digits);
temp = Math.floor(temp / 10);
}
return sum === num;
}
let n = parseInt(prompt("Enter a number: "));
console.log(`Armstrong numbers between 1 and ${n} are:`);
for (let i = 1; i <= n; i++) {
if (isArmstrong(i)) {
console.log(i);
}
}
<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-csharp">using System;
class Program {
static bool IsArmstrong(int num) {
int temp = num, sum = 0, digits = 0;
while (temp != 0) {
temp /= 10;
digits++;
}
temp = num;
while (temp != 0) {
sum += (int)Math.Pow(temp % 10, digits);
temp /= 10;
}
return sum == num;
}
static void Main() {
Console.Write("Enter a number: ");
int n = int.Parse(Console.ReadLine());
Console.WriteLine($"Armstrong numbers between 1 and {n} are:");
for (int i = 1; i <= n; i++) {
if (IsArmstrong(i)) {
Console.Write(i + " ");
}
}
}
}