Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
64
Task/Gapful-numbers/C-sharp/gapful-numbers.cs
Normal file
64
Task/Gapful-numbers/C-sharp/gapful-numbers.cs
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
using System;
|
||||
|
||||
namespace GapfulNumbers
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("The first 30 gapful numbers are: ");
|
||||
/* Starting at 100, find 30 gapful numbers */
|
||||
FindGap(100, 30);
|
||||
|
||||
Console.WriteLine("The first 15 gapful numbers > 1,000,000 are: ");
|
||||
FindGap(1000000, 15);
|
||||
|
||||
Console.WriteLine("The first 10 gapful numbers > 1,000,000,000 are: ");
|
||||
FindGap(1000000000, 10);
|
||||
|
||||
Console.Read();
|
||||
}
|
||||
|
||||
public static int firstNum(int n)
|
||||
{
|
||||
/*Divide by ten until the leading digit remains.*/
|
||||
while (n >= 10)
|
||||
{
|
||||
n /= 10;
|
||||
}
|
||||
return (n);
|
||||
}
|
||||
|
||||
public static int lastNum(int n)
|
||||
{
|
||||
/*Modulo gives you the last digit. */
|
||||
return (n % 10);
|
||||
}
|
||||
|
||||
static void FindGap(int n, int gaps)
|
||||
{
|
||||
int count = 0;
|
||||
while (count < gaps)
|
||||
{
|
||||
|
||||
/* We have to convert our first and last digits to strings to concatenate.*/
|
||||
string concat = firstNum(n).ToString() + lastNum(n).ToString();
|
||||
/* And then convert our concatenated string back to an integer. */
|
||||
int i = Convert.ToInt32(concat);
|
||||
|
||||
/* Modulo with our new integer and output the result. */
|
||||
if (n % i == 0)
|
||||
{
|
||||
Console.Write(n + " ");
|
||||
count++;
|
||||
n++;
|
||||
}
|
||||
else
|
||||
{
|
||||
n++;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue