
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
C# Program to Check Whether the Entered Number is an Armstrong Number or Not
For an Armstrong number, let us say a number has 3 digits, then the sum of cube of its digits is equal to the number itself.
For example, 153 is equal to −
1³ + 3³ + 5³
To check for it using C#, check the value and find its remainder. Here “val” is the number you want to check for Armstrong −
for (int i = val; i > 0; i = i / 10) { rem = i % 10; sum = sum + rem*rem*rem; }
Now compare the addition with the actual value. If it matches, that would mean the sum of cubes are the same and it is an Armstrong number −
if (sum == val) { Console.Write("Armstrong Number"); }else { Console.Write("Not an Armstrong Number"); }
Example
Let us see a complete example to check whether a number is Armstrong or not.
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Demo { class ApplicationNew { static void Main(string[] args) { int val = 153, sum = 0; int rem; // check for armstrong for (int i = val; i > 0; i = i / 10) { rem = i % 10; sum = sum + rem*rem*rem; } if (sum == val) { Console.Write("Armstrong Number"); } else { Console.Write("Not an Armstrong Number"); } Console.ReadLine(); } } }
Output
Armstrong Number
- Related Articles
- Java program to check whether the given number is an Armstrong number
- Write a C# program to check if the entered number is Armstrong number?
- Check the number is Armstrong or not using C
- Program to check whether the given number is Buzz Number or not in C++
- Program to check whether given number is Narcissistic number or not in Python
- Program to check whether a number is Proth number or not in C++
- C++ Program to Check Whether a Number is Prime or Not
- C++ Program to Check Whether a Number is Palindrome or Not
- C Program to Check Whether a Number is Prime or not?
- Java Program to Check Whether a Number is Prime or Not
- Check Whether a Number is an Autobiographical Number or Not in JAVA
- How To Check Whether a Number is an Ugly Number or Not in Java?
- How To Check Whether a Number is an Empire Number or Not in Java?
- Check whether the entered value is whitespace or not in Java
- Write a Golang program to check whether a given number is prime number or not

Advertisements