Write a Program in C# to add the given number till it get Single digit

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ADDINTTILLONEDIGIT
{
    class Program
    {
        static void Main(string[] args)
        {
        start:
            Console.WriteLine("Enter the integers to make it to single int");
            string input = Console.ReadLine();

            if (!input.All(char.IsDigit))
                goto start;
            else
            {
                while (input.Length > 1)
                {
                    input = input.Sum(x => x - '0').ToString();
                }
                Console.WriteLine("Single digit number is {0} :-)",input);
                Console.ReadKey();
            }
        }
    }

}


Output:

Read More »

Write a program to find the minimum denomination for amount 5365 using 1000, 500, 100 and 5

Program:
========

using System;
using System.Collections.Generic;

namespace MinimumDenomination
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<int, int> dicOutPut = new Dictionary<int, int>();
            int[] denominations = { 1000, 500, 100, 5 };

            dicOutPut = getMinumumDemonitation(1850, denominations);
           
            foreach (var item in dicOutPut)
            {
                Console.WriteLine("{0} * {1} = {2}", item.Key, item.Value, item.Key * item.Value);
            }
            Console.ReadKey();

        }


        public static Dictionary<int, int> getMinumumDemonitation(int amount, int[] availableNotes)
        {
            Dictionary<int, int> deno = new Dictionary<int, int>();
            foreach (int i in availableNotes)
            {
                int k = i; int count = 0;
                while (k <= amount)
                {
                    count = count + 1;
                    k = i * count;
                }
                if (count > 1 && !deno.ContainsKey(i))
                {
                    deno.Add(i, count - 1);
                    amount = amount - i * (count - 1);
                }
            }
            return deno;
        }
    }
}


output:



Read More »

Dependency Injection in .Net with example using Ninject

Reducing the dependency between two or more classes by creating an Interface to its respective classes and making loosely coupled architecture, this is called Inversion of Control.  Here objects are created at run-time.

There are some IOCs in market for .net: Ninject, Unity, Autofac etc

Dependency injection will assign respective objects automatically.

There are Constructor Injection Property Injection, Method injection etc. Commonly used real-time injection is constructor injection.

Below is the example for dependency injection:


Open visual studio. Create new project [ I have selected Console application]

Go to Tools  ->  NuGet Package Manager Console



In package manager console, type: Install-Package Ninject

Internet connection is mandatory to install the package; else you can manually add reference of Ninject downloaded DLL.



Now we can view the Ninject.dll in our solution reference.
Create an Interface as below.



Below is the code for interface:

using System;

namespace DependencyInjectionExample
{
    public interface IStaff
    {
        string getStaffDetails();
    }
}

Screenshot:


Create class file where we need to define the function.

Created a class as ‘TechnicalStaff’ and inherits the interface ‘IStaff’ as below:


using System;

namespace DependencyInjectionExample
{
    public class TechnicalStaff : IStaff
    {
        public string getStaffDetails()
        {
            return "19 Technical staff";
        }
    }
}

Screenshot:



Create a new class as Employee as sown below:




Place the below code in the employee class.


using System;

namespace DependencyInjectionExample
{
    public class Employee
    {
        private readonly IStaff staf;

        public Employee(IStaff staffing) //creating contructor
        {
            staf = staffing;    //Inversion of control
        }

        public string Getdetails()
        {
            return staf.getStaffDetails();
        }
    }
}


screenshot:



Now coming into program.cs file, we need to add namespaces of Ninject.

using Ninject;
using Ninject.Modules;

Below is the complete code for Program.cs:

using System;
using System.Text;
using Ninject;
using Ninject.Modules;
using System.Reflection;

namespace DependencyInjectionExample
{
    class Program
    {
        static void Main(string[] args)
        {
            var kernel = new StandardKernel();
            kernel.Load(Assembly.GetExecutingAssembly());
            var details = kernel.Get<IStaff>();

            var employee = new Employee(details);
            string result = employee.Getdetails();

            Console.WriteLine(result);
            Console.ReadKey();           
        }
    }
    public class DependInj : NinjectModule
    {
        public override void Load()
        {
            Bind<IStaff>().To<TechnicalStaff>();
        }
    }

}



In the above code, we can see that TechnicalStaff.cs and Employee.cs doesn’t have any direct communication. Interface plays a key role. Sending the interface as a parameter for constructor defines the Inversion of Control.


OUTPUT :


Read More »

Rounding decimals in MongoDB with $ceil

$Ceil is the function that is used to round a double to floor or ceiling. $ceil comes under the mongo aggregate function, below is the example Mongo statement to retrieve data based on ceiling

Let’s assume below is the cloths collection.

{"_id" : 110, "Price" : 10.35 }
{"_id" : 120, "Price" : 17.83 }
{"_id" : 130, "Price" : 16.52 }
{"_id" : 140, "Price" : -99.54}


db.cloths.aggregate([{
$project:"beforeRound":"$Price","roundedvalue":$ceil:"$Price"}}
}])


Output for cloths collection and above Statement is:


{ "_id" : 110, "beforeRound" : 10.35, "roundedvalue" : 11 }
{ "_id" : 120, "beforeRound" : 17.83, "roundedvalue" : 18 }
{ "_id" : 130, "beforeRound" : 16.52, "roundedvalue" : 17 }
{ "_id" : 140, "beforeRound" : -99.54, "roundedvalue" : -99 }


$ceil is used to round the decimal values.
$project is used to retrieve the specific columns from collection.
$price is used to project the price value from the mongo node.



Read More »

JavaScript Expressions in MongoDB Querying

Basically, MongoDB is an Object-Oriented Database where we not only use MongoDB Query to fetch the results, we can also use this JavaScript like expressions to operate the data from database. We can use some basic operations like ‘&&’, ‘>’, ‘!=’, etc. below is the example for both JavaScript and MongoDB Querying.

Example: trying to get the results with style number greater than 600000 and style description not equal to MM GRAPHIC PO HOOD.

Query using JavaScript expression:

db.styles.find(
"this.style.number > 600000 && this.style.description !='MM GRAPHIC PO HOOD'"

)


Query using MongoDB Syntax:

db.style.find(
{
"style.number":{$gt:"600000"},
"style.description":{$ne:"MM GRAPHIC PO HOOD"}}
)



Performance matters:

                 To get the best performance, use the Mongo Query rather than javaScript expression to fetch the results.



Read More »