Program to Check if Given Number is Armstrong or Not using While loop
- Write a program to Check if Give Number is Armstrong or Not using While loop in C
- Write a program to Check if Give Number is Armstrong or Not using While loop in C++
- Write a program to Check if Give Number is Armstrong or Not using While loop in Python
- Write a program to Check if Give Number is Armstrong or Not using While loop in PHP
- Write a program to Check if Give Number is Armstrong or Not using While loop in Java
- Write a program to Check if Give Number is Armstrong or Not using While loop in Java Script
- Write a program to Check if Give Number is Armstrong or Not 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:
- 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 While loop
-
C
-
C++
-
Python
-
PHP
-
JAVA
-
Java Script
-
C#
#include <stdio.h>
int isPalindrome(int num) {
int originalNum = num, reversedNum = 0, remainder;
while (num != 0) {
remainder = num % 10;
reversedNum = reversedNum * 10 + remainder;
num /= 10;
}
return (originalNum == reversedNum);
}
int main() {
int n, m;
printf("Enter the range (n and m) to find palindrome numbers: ");
scanf("%d %d", &n, &m);
int num = n;
while (num <= m) {
if (isPalindrome(num)) {
printf("%d ", num);
}
num++;
}
printf("\n");
return 0;
}
#include <iostream>
using namespace std;
bool isPalindrome(int num) {
int originalNum = num, reversedNum = 0, remainder;
while (num != 0) {
remainder = num % 10;
reversedNum = reversedNum * 10 + remainder;
num /= 10;
}
return (originalNum == reversedNum);
}
int main() {
int n, m;
cout << "Enter the range (n and m) to find palindrome numbers: ";
cin >> n >> m;
int num = n;
while (num <= m) {
if (isPalindrome(num)) {
cout << num << " ";
}
num++;
}
cout << endl;
return 0;
}
def is_palindrome(num):
original_num = num
reversed_num = 0
while num != 0:
remainder = num % 10
reversed_num = reversed_num * 10 + remainder
num //= 10
return original_num == reversed_num
n, m = map(int, input("Enter the range (n and m) to find palindrome numbers: ").split())
num = n
while num <= m:
if is_palindrome(num):
print(num, end=" ")
num += 1
print()
<?php
function isPalindrome($num) {
$originalNum = $num;
$reversedNum = 0;
while ($num != 0) {
$remainder = $num % 10;
$reversedNum = $reversedNum * 10 + $remainder;
$num = (int)($num / 10);
}
return $originalNum == $reversedNum;
}
list($n, $m) = explode(" ", readline("Enter the range (n and m) to find palindrome numbers: "));
$num = $n;
while ($num <= $m) {
if (isPalindrome($num)) {
echo $num . " ";
}
$num++;
}
echo "\n";
?>
<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-java">import java.util.Scanner;
public class Palindrome {
public static boolean isPalindrome(int num) {
int originalNum = num, reversedNum = 0, remainder;
while (num != 0) {
remainder = num % 10;
reversedNum = reversedNum * 10 + remainder;
num /= 10;
}
return originalNum == reversedNum;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the range (n and m) to find palindrome numbers: ");
int n = sc.nextInt();
int m = sc.nextInt();
int num = n;
while (num <= m) {
if (isPalindrome(num)) {
System.out.print(num + " ");
}
num++;
}
System.out.println();
sc.close();
}
}
function isPalindrome(num) {
let originalNum = num;
let reversedNum = 0;
while (num !== 0) {
let remainder = num % 10;
reversedNum = reversedNum * 10 + remainder;
num = Math.floor(num / 10);
}
return originalNum === reversedNum;
}
let [n, m] = prompt("Enter the range (n and m) to find palindrome numbers: ").split(" ").map(Number);
let num = n;
while (num <= m) {
if (isPalindrome(num)) {
console.log(num);
}
num++;
}
using System;
class Program {
static bool IsPalindrome(int num) {
int originalNum = num, reversedNum = 0, remainder;
while (num != 0) {
remainder = num % 10;
reversedNum = reversedNum * 10 + remainder;
num /= 10;
}
return originalNum == reversedNum;
}
static void Main() {
Console.Write("Enter the range (n and m) to find palindrome numbers: ");
string[] inputs = Console.ReadLine().Split(' ');
int n = int.Parse(inputs[0]);
int m = int.Parse(inputs[1]);
int num = n;
while (num <= m) {
if (IsPalindrome(num)) {
Console.Write(num + " ");
}
num++;
}
Console.WriteLine();
}
}