Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Practical 5: Armstrong Number Check

Lesson 6 of 14 in the free C# Programming Lab notes on Siksha Sarovar, written by Rohit Jangra.

Aim

To test whether an integer is an Armstrong (narcissistic) number — a number equal to the sum of its own digits each raised to the power of the digit count.

Theory

A number with d digits is Armstrong when the sum of each digit raised to the d-th power equals the number itself: 153 = 1³ + 5³ + 3³, and 9474 = 9⁴ + 4⁴ + 7⁴ + 4⁴. The generalised d-power definition used here is stronger than the fixed-cube version usually taught for 3-digit numbers. Two idioms in the implementation deserve close study. (1) Digit extraction via the remainder/quotient pair: temp % 10 peels off the units digit and temp /= 10 discards it — C# integer division truncates toward zero, guaranteeing the loop terminates in exactly d iterations, i.e. O(log₁₀ n). (2) Digit counting via num.ToString().Length — concise and allocation-cheap for a lab; the pure-math alternative is (int)Math.Floor(Math.Log10(num)) + 1. A subtle advanced point: Math.Pow computes in double and returns double, and the (int) cast truncates. That is safe for small digit powers, but binary floating point can return values like 124.99999999 for larger arguments, so production-grade code uses an integer power loop — an excellent viva talking point about the danger of mixing floating point into integer algorithms.

Requirements

  • .NET SDK 8.0+; commands dotnet new console, dotnet run; or the RUN CODE button.

Procedure

  1. Create the project: dotnet new console -n ArmstrongLab, cd ArmstrongLab, paste the code.
  2. Run dotnet run with the built-in value 153.
  3. Change num to 9474 (a 4-digit Armstrong number) and to 154 (not Armstrong) and re-run so both verdict branches are recorded.

Explanation of the Code

  • int temp = num, sum = 0, digits = num.ToString().Length;temp is a working copy so the original survives for the final comparison; digits fixes the exponent d once, before the loop mutates anything.
  • Inside while (temp > 0): int d = temp % 10; extracts the current units digit; sum += (int)Math.Pow(d, digits); accumulates d raised to the digit count; temp /= 10; shifts the number right by one decimal place.
  • Console.WriteLine(sum == num ? ... : ...) — the ternary picks the verdict; comparing against the untouched num is precisely why the copy into temp mattered.

Expected Output

153 is Armstrong.

With num = 94749474 is Armstrong.; with num = 154154 is not Armstrong.

🎯 Viva Questions

  1. Define an Armstrong number. — An n-digit number equal to the sum of its digits each raised to the n-th power.
  2. What does temp % 10 yield? — The last (units) digit of temp.
  3. Why copy num into temp? — The extraction loop destroys its operand; the original is still needed for the final equality test.
  4. Complexity of the digit loop? — O(d) iterations, i.e. O(log₁₀ n).
  5. What is the risk of (int)Math.Pow(...)?Math.Pow works in double; rounding error can make the cast truncate wrongly for large values — use an integer power loop when exactness matters.
  6. How does integer division behave in C#? — It truncates toward zero: 17 / 10 == 1 and -7 / 10 == 0.