Found another good problem...the idea is to find all 6 digit numbers where the sum of the first 3 digits is equal to the sum of the last 3 digits and calculate the total N of these.
Here is a function in C# that does the work...
public string GetLuckyNos()
{
string si = String.Empty;
ArrayList ar = new ArrayList();
for (int i = 999999; i>=100000 ; i--)
{
try
{
int sa = 0;
string a = i.ToString().Substring(0, 3);
for (int h = 0; h < a.Length; h++)
{
sa += int.Parse(a[h].ToString());
}
int sb=0;
string b = i.ToString().Substring(3, 3);
for (int h = 0; h < b.Length; h++)
{
sb += int.Parse(b[h].ToString());
}
if (sa == sb)
{
ar.Add(i);
}
}
catch
{
}
}//end of loop
double summ = 0;
summ = ar.Count;
return(summ.ToString());
}
The trick is to set up a loop counter that counts backwards from 999999 to 100000.
The rest is a piece of cake.
Here is a function in C# that does the work...
public string GetLuckyNos()
{
string si = String.Empty;
ArrayList ar = new ArrayList();
for (int i = 999999; i>=100000 ; i--)
{
try
{
int sa = 0;
string a = i.ToString().Substring(0, 3);
for (int h = 0; h < a.Length; h++)
{
sa += int.Parse(a[h].ToString());
}
int sb=0;
string b = i.ToString().Substring(3, 3);
for (int h = 0; h < b.Length; h++)
{
sb += int.Parse(b[h].ToString());
}
if (sa == sb)
{
ar.Add(i);
}
}
catch
{
}
}//end of loop
double summ = 0;
summ = ar.Count;
return(summ.ToString());
}
The trick is to set up a loop counter that counts backwards from 999999 to 100000.
The rest is a piece of cake.
Comments