Program to print all Armstrong numbers Between 1 and n using While loop
- Write a program to print all Armstrong numbers Between 1 and n using While loop in C
- Write a program to print all Armstrong numbers Between 1 and n using While loop in C++
- Write a program to print all Armstrong numbers Between 1 and n using While loop in Python
- Write a program to print all Armstrong numbers Between 1 and n using While loop in PHP
- Write a program to print all Armstrong numbers Between 1 and n using While loop in Java
- Write a program to print all Armstrong numbers Between 1 and n using While loop in Java Script
- Write a program to print all Armstrong numbers Between 1 and n 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 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 While loop
-
C
-
C++
-
Python
-
PHP
-
JAVA
-
Java Script
-
C#
#include <stdio.h> void primeFactors(int num) { int divisor = 2; while (num > 1) { if (num % divisor == 0) { printf("%d ", divisor); num /= divisor; } else { divisor++; } } } int main() { int num; printf("Enter a number: "); scanf("%d", &num); printf("Prime factors of %d are: ", num); primeFactors(num); return 0; }
#include <iostream> using namespace std; void primeFactors(int num) { int divisor = 2; while (num > 1) { if (num % divisor == 0) { cout << divisor << " "; num /= divisor; } else { divisor++; } } } int main() { int num; cout << "Enter a number: "; cin >> num; cout << "Prime factors of " << num << " are: "; primeFactors(num); return 0; }
def prime_factors(num): divisor = 2 while num > 1: if num % divisor == 0: print(divisor, end=" ") num //= divisor else: divisor += 1 num = int(input("Enter a number: ")) print(f"Prime factors of {num} are: ", end="") prime_factors(num)
<?php function primeFactors($num) { $divisor = 2; while ($num > 1) { if ($num % $divisor == 0) { echo $divisor . " "; $num = $num / $divisor; } else { $divisor++; } } } $num = (int)readline("Enter a number: "); echo "Prime factors of $num are: "; primeFactors($num); ?>
import java.util.Scanner; public class PrimeFactors { public static void primeFactors(int num) { int divisor = 2; while (num > 1) { if (num % divisor == 0) { System.out.print(divisor + " "); num /= divisor; } else { divisor++; } } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); int num = sc.nextInt(); System.out.print("Prime factors of " + num + " are: "); primeFactors(num); sc.close(); } }
function primeFactors(num) { let divisor = 2; while (num > 1) { if (num % divisor === 0) { console.log(divisor, " "); num = Math.floor(num / divisor); } else { divisor++; } } } let num = parseInt(prompt("Enter a number: ")); console.log(`Prime factors of ${num} are:`); primeFactors(num);
using System; class Program { static void PrimeFactors(int num) { int divisor = 2; while (num > 1) { if (num % divisor == 0) { Console.Write(divisor + " "); num /= divisor; } else { divisor++; } } } static void Main() { Console.Write("Enter a number: "); int num = int.Parse(Console.ReadLine()); Console.Write("Prime factors of " + num + " are: "); PrimeFactors(num); } }