How to find the number of times the digit 1 present in the decimal reresenation of all positive integers that doesn't exceed given number

For those who are confused with question, we will go to a simple example our given number is 15,
so its positive representation is 1 to 15 and number of times 1 present is 8   i.e 1,10,11,12,13,14,15
now I hope you have understood what we want to achieve


so now we will write a function which returns the number count as result.
we will pass all numbers in a for loop and we will take the mod of that number with 10  inside a while loop
and the result is 1 we will add the count by 1  i.e 1 is present there. and divide that number by 10 for exiting the while loop and finding the count of 1's in integers value more than 10.

if you are checking the mod result equal to 2 you can find out the count of 2, if its 3 count of 3 like wise

  

public static int Solution(int n)
        {
            int nums = 0;
            int digits = 0;
            int count = 0;
            for (int i = 1; i <= n; i++)
            {
                nums = i;
                while (nums != 0)
                {
                    digits = nums % 10;
                    if (digits == 1)
                    {
                        count++;
                    }
                    nums = nums / 10;
                }
            }
            return count;

        }

        static void Main(string[] args)
        {
            Console.WriteLine("Enter the number");
            int num =Convert.ToInt32(Console.ReadLine());

            int val = Solution(num);

            Console.WriteLine("Count of 1s is :" + val);

        }

String reversing in C# without using default function

Here we are going to discuss about two string reversing techniques.
 First of all we will check this question so our input string is "my name is tony" we need to revers it like this "tony is name my" how we can achieve it ??
string test="my name is c#";
string[] words = test.Split(' ');
so first we will split our string by space so we will get it by word  and insert that words into a string array.
so we have an array which is having value like this arr[0]=my,arr[1]=name etc.
so we will use a for loop and we will call the last index value first and print it and again it will decrement by 1 then next value until it reach us the index 0;
         
            for (int i = words.Length - 1; i >= 0; i--)
            {
                Console.Write(words[i] + " ");

            }
so finally we will get our string reversed like this "c# is name my"
In second string reverse technique  we will  simply pass the string and reverse it.  by default we can use  Array.Reverse(testarray) function  but we are not using that we will use custom method.
            string  revrs="androidsharp";
            char[] arr = revrs.ToCharArray();
            StringBuilder sb = new StringBuilder();
            for (int j = arr.Length - 1; j >= 0; j--)
            {
                sb.Append(arr[j]);

            }
            
            Console.WriteLine(sb.ToString());

Out Put:
prahsdiordna


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

namespace Shape
{
    class Array
    {
        public static void Main(string[] args)
        {
            //Method 1
            Console.WriteLine("Enter the String");
            string test = Console.ReadLine();
            string[] words = test.Split(' ');
            for (int i = words.Length - 1; i >= 0; i--)
            {
                Console.Write(words[i] + " ");

            }
            Console.WriteLine();

            //Method 2
            Console.WriteLine("Enter the String to reverse");
            string revrs = Console.ReadLine();
            char[] arr = revrs.ToCharArray();
            StringBuilder sb = new StringBuilder();
            for (int j = arr.Length - 1; j >= 0; j--)
            {
                sb.Append(arr[j]);

            }
            
            Console.WriteLine(sb.ToString());
        }

    }
}


How to sort an array of integers in ascending or descending order using c# .net

Consider we have an array of integers  int [] arr={90,60,20,30,100}  we need to format it in ascending order like this  20,30,60,90,100  so what we have to do?
we need to use two for loops and one temp variable using first loop it will send first index value of the array to the last, then inside we have given the second for loop it will do the  sorting so it will compare with next place value and  assign the small value to the temp variable and move the large value to second place so our syntax look like this in second for loop
 declare a temp variable of type integer outside for loops.

                 if (arr[sorts] > arr[sorts + 1])
                    {
                        temp = arr[sorts + 1];
                        arr[sorts + 1] = arr[sorts];
                        arr[sorts] = temp;
                    }

In our case arr[sorts]=90 and arr[sort+1]=60  so it will assign 60 into temp
temp=60 and arr[sort+1]=90 and arr[sorts]=60
so our current list become 60,90, then our for loop pass 20 now our arr[sorts]=90 and arr[sort+1]=20
it will follow the procedure until the loop exit
so we will get current array like this 60,20,30,90,100
then again it will go to the first for loop increment the count to 1 and it will sort the array again. it will continue till the first for loop exit,by the time we will get our array sorted.
here in our case we will get sorted array in second stage itself
20,30,60,90,100
 for getting it in descending order change the if condition like this
if(arr[sorts]<arr[sorts+1])

Console Application for getting sorting array in ascending order




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

namespace Shape
{
    class sort
    {

        static void Main(string[] args)
        {
            
            Console.WriteLine("Give first num and press enter then seceond vice versa");
            Console.WriteLine("Please Enter the 5 Integers need to sort");
            int[] arr= new int[5];

            for (int d = 0; d < arr.Length; d++)
            {
                arr[d] =Convert.ToInt32(Console.ReadLine());
            }
            

            int temp = 0;

            for (int j = 0; j < arr.Length - 1; j++)
            {
                for (int sorts = 0; sorts < arr.Length - 1; sorts++)
                {
                    if (arr[sorts] > arr[sorts + 1])
                    {
                        temp = arr[sorts + 1];
                        arr[sorts + 1] = arr[sorts];
                        arr[sorts] = temp;
                    }

                }

            }
            Console.WriteLine("sorted array");

            for (int i = 0; i < arr.Length; i++)
                Console.Write(arr[i] + " ");
               
                Console.ReadLine();
                
        }
    }
}




So Now we can check the code if we are using Linq how simple is the sorting no need to use temp concept at all.

It will write sorted  array in descending order.

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

namespace Shape
{
    class Linqex
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Give first num and press enter then seceond vice versa");
            Console.WriteLine("Please Enter the 5 Integers need to sort");
            int[] arr = new int[5];

            for (int d = 0; d < arr.Length; d++)
            {
                arr[d] = Convert.ToInt32(Console.ReadLine());
            }

            Console.WriteLine("Sorted array :");

                 arr.OrderByDescending(i=>i)
                .ToList()
                .ForEach(i=>Console.WriteLine(i));
        }
    }
}




if you want sorted array  in ascending order
change  arr.OrderByDescending(i=>i)      into arr.OrderBy(i=>i)