C# Code for ATM Notes logic

<pre>
1.
using System;
					
public class Program
{
	public static void Main()
	{
		int amount = 1100; 
        countCurrency(amount); 
		Console.ReadLine();
		
	}
	public static void countCurrency(int amount) 
    { 
        int[] notes = new int[]{ 1000, 500, 100, 50, 10}; 
        int[] noteCounter = new int[5]; 
      
        // count notes using Greedy approach 
        for (int i = 0; i < 5; i++) { 
            if (amount >= notes[i]) { 
                noteCounter[i] = amount / notes[i]; 
                amount = amount - noteCounter[i] * notes[i]; 
            } 
        } 
      if(amount%10==0)
	  {
        // Print notes 
        Console.WriteLine("Currency Count ->"); 
        for (int i = 0; i < 5; i++) { 
            if (noteCounter[i] != 0) 
			{ 
                Console.WriteLine(notes[i] + " : "+ noteCounter[i]); 
            } 
        } 
	 }
		else
		{
			Console.WriteLine("Enter Valid Amount"); 
		}
    } 
      
}



2.
using System;
					
public class Program
{
	public static void Main()
	{
		int amount = 40; 
        countCurrency(amount); 
		Console.ReadLine();
		
	}
	public static void countCurrency(int amount) 
    { 
        int[] notes = new int[]{ 1000, 500, 100, 50, 10}; 
        int[] noteCounter = new int[5]; 
      
        // count notes using Greedy approach 
        for (int i = 0; i < 5; i++) 
		{ 
            if (amount >= notes[i]) 
			{ 
                noteCounter[i] = amount / notes[i]; 
                amount = amount - noteCounter[i] * notes[i]; 
            } 
        } 
      if(amount%10==0)
	  {
        // Print notes 
        for (int i = 0; i < 5; i++) { 
            if (noteCounter[i] != 0) 
			{ 
                Console.WriteLine(notes[i] + " * "+ noteCounter[i] + " = " +(notes[i]*noteCounter[i]) ); 
            } 
        } 
	 }
		else
		{
			Console.WriteLine("Enter Valid Amount"); 
		}
    } 
      
}

3.
using System;
					
public class Program
{
	public static void Main()
	{
		int amount = 10400; 
        countCurrency(amount); 
		Console.ReadLine();
	}
	public static void countCurrency(int amount) 
    { 
        int[] notes = new int[]{ 1000, 500, 100, 50, 10}; 
        int[] noteCounter = new int[5]; 
        // count notes using Greedy approach 
        for (int i = 0; i < 5; i++) 
		{ 
            if (amount >= notes[i]) 
			{ 
                noteCounter[i] = amount / notes[i]; 
                amount = amount - noteCounter[i] * notes[i]; 
            } 
        } 
      if(amount%10==0)
	  {
        // Print notes 
        for (int i = 0; i < 5; i++) { 
            if (noteCounter[i] != 0) 
			{ 
                Console.WriteLine(notes[i] + " * "+ noteCounter[i] + " = " +(notes[i]*noteCounter[i]) ); 
            }
			else
			{
				Console.WriteLine(notes[i] + " * "+ noteCounter[i] + " = " +(notes[i]*noteCounter[i]) ); 
			}
        } 
	 }
		else
		{
			Console.WriteLine("Enter Valid Amount"); 
		}
    } 
      
}

</pre>

Leave a Reply

Your email address will not be published. Required fields are marked *