Write C# program check whether a number is Armstrong number or not


Introduction

I have used Visual Studio 2012 for debugging purpose. But you can use any version of visul studio as per your availability..


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. public class csharpExercise
  7. {
  8. static void Main(string[] args)
  9. {
  10. int num, sum = 0, i, r;
  11.  
  12. // Reading number
  13. Console.Write("Please enter a number: ");
  14. num = Convert.ToInt32(Console.ReadLine());
  15.  
  16. //Finding armstrong number or not
  17. for (i = num; i > 0; i = i / 10)
  18. {
  19. r = i % 10;
  20. sum = sum + r * r * r;
  21. }
  22.  
  23. if (num == sum)
  24. {
  25. Console.WriteLine(num + " is an armstrong number.");
  26. }
  27. else
  28. {
  29. Console.WriteLine(num + " is not an armstrong number.");
  30. }
  31.  
  32. Console.ReadLine();
  33. }
  34. }

Result

C# language techstudy

     





Related Topics




Popular Posts