C# Program to find Armstrong Number
An Armstrong Number is an integer such that the sum of the cubes of its digits is equal to the number itself.
CODE:
using System;
namespace armstrongNumber
{
class Program
{
static void Main(string[] args)
{
int no,r,sum=0,n=0;
Console.WriteLine("Enter number\n");
no = int.Parse(Console.ReadLine());
n = no;
while (n > 0)
{
r = n % 10;
n = n / 10;
sum += r*r*r;
}
if (sum == no)
{
Console.WriteLine(no+" is a Amstrong number" );
}
else
{
Console.WriteLine(no+" is not a Amstrong number");
}
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ProjectArmstrong
{
class Armstrong
{
static void Main(string[] args)
{
int no, sum = 0;
Console.WriteLine("Enter number\n");
no = int.Parse(Console.ReadLine());
int[] numberArray = Array.ConvertAll(no.ToString().ToArray(), x => (int)x - 48);
foreach (int n in numberArray)
{
sum =sum+ (int)Math.Pow(n, numberArray.Length);
}
if (sum == no)
{
Console.WriteLine(no+" is a Amstrong number");
}
else
{
Console.WriteLine(no+" is not a Amstrong number");
}
Console.ReadLine();
}
}
}
CODE:
1)Finding Armstrong number till 3 digits only
using System;
namespace armstrongNumber
{
class Program
{
static void Main(string[] args)
{
int no,r,sum=0,n=0;
Console.WriteLine("Enter number\n");
no = int.Parse(Console.ReadLine());
n = no;
while (n > 0)
{
r = n % 10;
n = n / 10;
sum += r*r*r;
}
if (sum == no)
{
Console.WriteLine(no+" is a Amstrong number" );
}
else
{
Console.WriteLine(no+" is not a Amstrong number");
}
Console.ReadLine();
}
}
}
2) Finding Armstrong number 10 digits
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ProjectArmstrong
{
class Armstrong
{
static void Main(string[] args)
{
int no, sum = 0;
Console.WriteLine("Enter number\n");
no = int.Parse(Console.ReadLine());
int[] numberArray = Array.ConvertAll(no.ToString().ToArray(), x => (int)x - 48);
foreach (int n in numberArray)
{
sum =sum+ (int)Math.Pow(n, numberArray.Length);
}
if (sum == no)
{
Console.WriteLine(no+" is a Amstrong number");
}
else
{
Console.WriteLine(no+" is not a Amstrong number");
}
Console.ReadLine();
}
}
}
The output will be:-
No comments:
Post a Comment