## Archive for the ‘ Number ’ Category

`        public static long ReverseNumber(this long number)`
`        {`
`            long reversedNumber = 0;`
`            bool isNegative = false;`
` `
`            if (number < 0)`
`            {`
`                isNegative = true;`
`            }`
`            if (number < 10 && number >= 0)`
`            {`
`                reversedNumber = number;`
`                return reversedNumber;`
`            }`
` `
`            number = Math.Abs(number);`
` `
`            while (number > 0)`
`            {`
`                reversedNumber = _base * reversedNumber + number % _base;`
`                number = number / _base;`
`            }`
` `
`            if (isNegative)`
`            {`
`                reversedNumber = reversedNumber * -1;`
`            }`
`            return reversedNumber;`
`        }`

Test Case

Number 1234567890 is reversed to 987654321

Number -123456 is reversed to -654321

Number 9 is reversed to 9

Number 545 is reversed to 545

Number 123454321 is reversed to 123454321

Number -123454321 is reversed to -123454321

Floyd’s triangle is a right-angled triangular array of natural numbers, used in computer science education. It is named after Robert Floyd. It is defined by filling the rows of the triangle with consecutive numbers, starting with a 1 in the top left corner.

Code

```        public static void PrintFloydTriangle(int rows)
{
int number = 1;
for (int rowCounter = 0; rowCounter <= rows; rowCounter++)
{
for (int columnCounter = rowCounter; columnCounter >= 1;                  columnCounter--)
{
Console.Write(number++ + " ");
}
Console.WriteLine("");
}
}```
`Test`
`Input : 6`
`Output`

1

2 3

4 5 6

7 8 9 10

11 12 13 14 15

16 17 18 19 20 21

Input : 10

1

2 3

4 5 6

7 8 9 10

11 12 13 14 15

16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31 32 33 34 35 36

37 38 39 40 41 42 43 44 45

46 47 48 49 50 51 52 53 54 55

If an array is having integers/Char/special Char… Ex: "PST456DA85M2A!!23++46", find out the sum of integers.

Note: If we find consecutive digits in array we need to treat it as number, let say 456, we need to treat it as [ four hundred and fifty six]. Write a program to get the output by summing 456+85+2+23+46.

Also this needs to be done in less number of iterations.

Solution

```            string inputString = "PST456DA85M2A!!23++4600";
char[] inputChars = inputString.ToCharArray();
double sum = 0;
double tempnum = 0;
double powerCounter = 0;
double num;
for (int i = inputChars.Length - 1; i >= 0; i--)
{
if (inputChars[i] >= 48 && inputChars[i] <= 57)
{
num = inputChars[i] - 48;
tempnum = num * Math.Pow(10, powerCounter) + tempnum;
powerCounter = powerCounter + 1;
}

else
{
sum = sum + tempnum;
powerCounter = 0;
tempnum = 0;
}

}
Console.WriteLine();
Console.WriteLine(inputString);
Console.WriteLine(sum);

A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. A natural number greater than 1 that is not a prime number is called a composite number. For example, 5 is prime, as only 1 and 5 divide it.

For computing prime numbers we will use Sieve of Eratosthenes algorithm. The sieve of Eratosthenes is one of the most efficient ways to find all of the smaller primes (below 10 million or so).

# Algorithm

To find all the prime numbers less than or equal to a given integer n by Eratosthenes’ method:

1. Create a list of consecutive integers from 2 to n: (2, 3, 4, …, n).
2. Initially, let p equal 2, the first prime number.
3. Starting from p, count up in increments of p and mark each of these numbers greater than p itself in the list. These numbers will be 2p, 3p, 4p, etc.; note that some of them may have already been marked.
4. Find the first number greater than p in the list that is not marked. If there was no such number, stop. Otherwise, let p now equal this number (which is the next prime), and repeat from step 3.

When the algorithm terminates, all the numbers in the list that are not marked are prime

``` public static void PrimeNumber(int n)
{
int[] numbers = new int[n + 1];
if (n < 0)
{
Console.WriteLine("invalid value");
return;
}
if (n < 3)
{
Console.WriteLine("Prime number is : 2");
return;

}

int p = 0;
for (int i = 2; i <= n; i++)
{
if (numbers[i] == 0)
{
p = i;
for (int j = 2; p * j <= n; j++)
{
numbers[p * j] = 1;
}
}
}

for (int i = 2; i <= n; i++)
{
if (numbers[i] == 0) Console.Write(i + " , ");
}

}```

``` public static void RepeatedEvenTimes(int[] inputArray)
{
Dictionary<int, int> numbers = new Dictionary<int, int>();
int length = inputArray.Length;
int num = -1;
for (int i = 0; i < length; i++)
{
if (numbers.Keys.Contains(inputArray[i]))
{
numbers[inputArray[i]] = numbers[inputArray[i]] + 1;
}
else
{

}
}

Console.Write("input Array ");
Console.WriteLine(string.Join(",", inputArray));

foreach (var item in numbers)
{
if (item.Value % 2 == 0)
{
num = item.Key;
Console.WriteLine("{0} Repeated {1} times ", item.Key, item.Value);
break;
}

}

if (num == -1)
{
Console.WriteLine("No number Repeated even number of times");
}

Console.WriteLine("-----------------------------------------------");
}```
` `

input Array 3,3,3,5,5,5,2,2,2
No number Repeated even number of times
———————————————–
input Array 100,1,1,3,5,3,3
1 Repeated 2 times
———————————————–
input Array 2,2,2,2,2
No number Repeated even number of times
———————————————–

To find the sum of all the multiples of 3 or 5 below or equal to 1000, first we have to find the sum of all the numbers which are divisible by 3, than sum of numbers which are divisible by 5. Once we are done with this we have to subtract the sum of numbers which are divisible by 3 and 5.

There are two approaches to solve this problem.

1. The first approach is to write a loop and find the numbers which are divisible by 3,5 and add them, after this subtract the numbers which are divisible by 3 and 5.
2. The other approach is to find the count of numbers which are divisible by 3,5 and 15. After this apply the arithmetic progressionâ€™s sum formula to find the sum.
```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int sum = 0;
int range = 1000;
int Number1 = 3; int Number2 = 5;
for (int i = 0, j = 0; i <= range; i += Number1, j += Number2)
{
sum += i;
if (j < range && j % Number2 == 0)
{
sum += j;
}
if (i <= range && i % Number2 * Number1 == 0)
{
sum -= i;
}

}
Console.WriteLine("\n");
Console.WriteLine("Sum of all the multiples of 3 or 5 below or equal to 1000" +
"using Loop "  + sum);

int Total_Number1 = range / Number1;
int Total_Number2 = range / Number2;
int Total_Number1AndNumber2 = range / (Number1 * Number2);

int sumTotal_Number1 = Total_Number1 * (Number1 + Total_Number1 * Number1) / 2;
int sumTotal_Number2 = Total_Number2 * (Number2 + Total_Number2 * Number2) / 2;
int sumTotal_Number1andNumber2 = Total_Number1AndNumber2 * (Number2 * Number1
+ Total_Number1AndNumber2 * Number2 * Number1) / 2;
int total = sumTotal_Number1 + sumTotal_Number2 - sumTotal_Number1andNumber2;
Console.WriteLine("Sum of all the multiples of 3 or 5 below or equal to 1000 " +
"using arithmetic progressionâ€™s sum formula  " + total);