Q:

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

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
public class csharpExercise
{
    static void Main(string[] args)
    {
        int num, sum = 0, i, r;
 
        // Reading number
        Console.Write("Please enter a number: ");
        num = Convert.ToInt32(Console.ReadLine());
 
        //Finding armstrong number or not
        for (i = num; i > 0; i = i / 10)
        {
            r = i % 10;
            sum = sum + r * r * r;
        }
 
        if (num == sum)
        {
            Console.WriteLine(num + " is an armstrong number.");
        }
        else
        {
            Console.WriteLine(num + " is not an armstrong number.");
        }
 
        Console.ReadLine();
    }
}

Result:

Please enter a number: 407

407 is an armstrong number.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now