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