20 June 2022

Stack -1 Top Programs in c#

1) Find out whether the expression is balanced or not using stack?

Input: exp = “[()]{}{[()()]()}” 
Output: Balanced

Input: exp = “[(])” 
Output: Un Balanced
Balanced 

Solution:

using System;

using System.Collections.Generic;

public class GFG {

static public void Main () {

string s="{(){(]}}";

Stack<char> st=new Stack<char>();

foreach(var i in s) {

    if(st.Count>0 && (i==')' && st.Peek()=='(') ||

    (i=='}' && st.Peek()=='{') || (i=='[' && st.Peek()==']') )  {     st.Pop();   }

    else   {    st.Push(i);    }

}

if(st.Count==0)  Console.WriteLine("Balanced"); 

else Console.WriteLine("Un balanced");

   }

}

27 October 2021

Top String programs in C#

using System;


public class GFG{

static public void Main (){

    string s=Console.ReadLine();

   //1) Print the input string

    Console.WriteLine(s);  

   //2) Length of the string without using library function

    int count=0;

    foreach(var literal in s)

    {

        count++;

    }

    Console.WriteLine(count);

    foreach(var literal in s)

    {

            Console.Write("{0} ",literal);

    }

    Console.WriteLine();

  //3) string in reverse order without inbuilt method

    char[] arr=s.ToCharArray();

    int i=0,j=s.Length-1;

    while(i<j)

    {

      char temp=arr[i];

      arr[i]=arr[j];

      arr[j]=temp;

      i++;

      j--;

    }

//4) count total number of the string

string s1="Welcome to India";

string[] res1=s1.Split(" ");

int count1=0;

foreach(var item in res1)

{

    count1++;

}

Console.WriteLine(count1);    

  //5) compare 2 string without using library function

    string s11="Hai";

    string s21="hai";

    int ii=0,jj=s21.Length-1;

   int count2=1;

    while(ii<jj)

    {

        if(s11[ii]!=s21[ii] || s11[jj]!=s21[jj])

        {

            count2=0;

            break;

        }

        ii++;

        jj--;

    }

    if(count2==1)

    Console.WriteLine("Equal");

    else

    Console.WriteLine("Not Equal");

}

}

  //6) Reverse each string without using split(" ")

i/p: Hai welcome to digital
o/p:
iaH emoclew ot latigid

using System;
using System.Text;

public class GFG{
    static public void Main (){
        StringBuilder str=new StringBuilder("Hai welcome to digital");
        int i=0,l=str.Length-1;
        int start=0;
        while(i<=l)
        {
            if(str[i]==' ')
            {
                 swap(str,start,i-1);
                 start=i+1;
            }
            if(i==l)
            {
                 swap(str,start,i);
            }
           i++;  
        }
        
        Console.WriteLine(str);
    }
    public static void swap(StringBuilder str,int i,int l)
    {
        while(i<=l)
        {
        char t=str[i];
        str[i]=str[l];
        str[l]=t;
            i++;
            l--;
        }
    }
}

11 October 2021

Patterns-1 in c#

 

1) print the below pattern based on n:

input:

3

o/p:

  *

 * *

* * *

 * *

  *

solution:

using System;

public class GFG{

     static public void Main (){

int n=3;

int k1=n,k2=n;

for(int i=1;i<n*2;i++)

{

    int t=1;

    for(int j=1;j<=k2;j++)

    {

        if(j>=k1 && j<=k2 && t==1){

        Console.Write("*");

        t=0;

        }

       else

       {

        Console.Write(" "); t=1;

       }      

    }

    if(i<n)

    {

        k1--;

        k2++;

    }

    else

    {

        k1++;

        k2--;    }

    Console.WriteLine("");

}

}

}

2) Make X pattern of the given string:

input:

Program

o/p:









solution:

class Program

    {

     public static void makePattern(string ch,int n,int i,int ptr1,int ptr2)  {

            if (i >= n)

                return;           

                for(int j=0;j<n;j++)   {

                    if (j == ptr1 || j == ptr2)

                        Console.Write(ch[i]);

                    else

                        Console.Write(" ");        }

                Console.WriteLine();                

            makePattern(ch, n, i+=1, ptr1+=1,ptr2-=1);

        }

        static void Main(string[] args)  {

            string s = Console.ReadLine();         

            makePattern(s, s.Length,0,0, s.Length-1);

            Console.ReadKey();   }


3) print snake pattern:

input:

4

o/p:










solution:

class Program{ 

public static void snakePattern(int n,int k,int i) {

            if (i >= n)

                return;           

                for (int j = 0; j < n; j++)

                {

                    Console.Write("{0} ", k);

                    if (j == n - 1)

                        break;

                    if (i % 2 == 0)

                        k++;

                    else 

                        k--;                    

                }

                k += n;

                Console.WriteLine();            

            snakePattern(n,k,i+=1);

        }

        static void Main(string[] args)

        {

            int n = Convert.ToInt32(Console.ReadLine());

            snakePattern(n,1,0);

            Console.ReadKey();

        }

}

08 October 2021

Part-1 Top 10 Array programs in c#

 1) Left Rotation in O(n):

i/p:

d=2

arr= 1 2 3 4 5 6 7

o/p:

3 4 5 6 7 1 2

Algorithm:

1) reverse the array from 0 to d-1

2) reverse the array from d to n

3) reverse the array from 0 to n

Solution:

using System;

class GFG

{

public static void Reverse(int[] arr,int l,int m){

 while(l<m){ 

int t=arr[l];

arr[l]=arr[m];

arr[m]=t;

l++;

m--;

}}

public static void leftRotate(int[] arr,int k){

         Reverse(arr, 0, k-1); 

         Reverse(arr, k, arr.Length-1); 

         Reverse(arr, 0, arr.Length-1);  

}

public static void printArray(int[] arr,int i){

         if(i==arr.Length) return;

     Console.WriteLine(arr[i]);

    printArray(arr,i+=1);

}

public static void Main(String[] args) {

  int[] arr=new int[]{1,2,3,4,5,6,7};

  int k=2;

  leftRotate(arr,k);

  printArray(arr,0); 

} 

}

2) check the sum & multiply of the own digits & print its maximum

i/p:









o/p:





Explanation: For index 0, the integer is 120. Summing the digits will give 3, and whereas Multiplying the digits gives 0. Thus, maximum of this two is 3.

Solution:

public class Program{

public static int sumOrMultiply(int k)

        {    int sum = 0,mul=1;

            while(k>0){

                sum += k % 10;

                mul *= k % 10;

                k = k / 10;  }

            if (sum > mul)   return sum;

            else  return mul;

        }

        static void Main(string[] args) {

            int n = Convert.ToInt32(Console.ReadLine());

            int[] arr=new int[n];

            for (int i = 0; i < n; i++)

                arr[i] = Convert.ToInt32(Console.ReadLine());

            for (int i = 0; i < n; i++)

                Console.Write("{0} ",sumOrMultiply(arr[i]));

            Console.ReadKey();   }

}

05 October 2021

Part-1 Tree in c#

1) Traverse Tree in PreOrder with recursion:

i/p:

pass values as,

root-1, root.left=2, root.right=3, root.left.right=4,root.right.left=5

o/p:

root = 1 

root->left = 2 

root->left->right = 4 

root->right = 3 

root->right->left = 5 

Solution:

using System;

//Node Creation

  class Node{

        public int value;

        public Node left, right;       

     public Node(int n){

       value=n;

       left=right=null;  

     }

}    

class HelloWorld {

  Node root=null;

  static void Main() {

     HelloWorld obj=new HelloWorld();

        //input passing

     obj.root=new Node(1);

     obj.root.left=new Node(2);

     obj.root.right=new Node(3);

     obj.root.left.right=new Node(4);

     obj.root.right.left=new Node(5);

     //call & print in preOrderTraversal

     obj.preOrderTraversal("root",obj.root);

  }

  void preOrderTraversal(string info,Node root) {

      if(root==null) return;       

        Console.WriteLine("{0} = {1} ",info,root.value);

        preOrderTraversal(info+"->left",root.left);

        preOrderTraversal(info+"->right",root.right);

  }

}

2) Traverse Tree in PreOrder without recursion:

Algorithm:

1) create an empty stack

2) push root node

3) run the loop till the stack is empty

4) pop the topmost element from the stack then print its value

5) push the left or right node into a stack-based on its value

Solution:

using System;

using System.Collections.Generic;

using System.Linq;

class Node

    {

        public int value;

        public Node left,right;

        public Node(int n)

        {

            value=n;

            left=right=null;

        }

    }

class GFG

{

    public void preOrderTraversal(Node root)

    {

        Stack<Node> st=new Stack<Node>();    

        st.Push(root);        

        while(st.Any()){

            Node current=st.Pop();

            Console.WriteLine(current.value);

            if(current.left!=null){

                st.Push(current.left); }

            if(current.right!=null) {

                st.Push(current.right); } }        

       }

 Node root=null;

  static void Main ()

  {

      GFG obj=new GFG();

      obj.root=new Node(1);

      obj.root.left=new Node(2);

      obj.root.right=new Node(3);

      obj.preOrderTraversal(obj.root);

  }

}

o/p:

1

3

2


04 October 2021

Part-2 Recursion in c#

6) print the even & odd numbers in separate line for n:

i/p:

20

o/p:

2 4 6 8 10 12 14 16 18 20

1 3 5 7 9 11 13 15 17 19 

Solution:

using System;

class Program {

  public static void printEven(int i,int n){

      if(i>n)  return;      //condition to break the recursion 

       if(i!=0)  Console.Write("{0} ",i);  

      printEven(i+=2,n);       

  }

  public static void printOdd(int i,int n){

      if(i>n) return;   //condition to break the recursion 

      Console.Write("{0} ",i);

      printEven(i+=2,n);    

  }

  static void Main() {

    printEven(0,20);

    Console.WriteLine("");

    printOdd(1,20);

  }

} 

7) print the number is prime or not:

i/p:

26

o/p:

Prime Number bcz its divisible by 2

i/p:

23

o/p:

Non Prime Number

Solution:

using System;

class Program {

  public static void printPrime(int i,int n)

  {

      if(i>=n)

      {

        Console.WriteLine("Non Prime number");

        return;

      }

        if(n%i==0 && i!=1){

            Console.WriteLine("Prime number Bcz divisible by {0}",i);

            return;

        }

        printPrime(i+=1,n);        

  }

  static void Main() {

    printPrime(1,27);

  }

}

8)  print the number is prime or not:

i/p:

MADAM

o/p:

Its Palindrome

i/p:

TATOO

o/p:

Its Not Palindrome

Solution:

using System;

class Program {

  public static void checkPalindrome(char[] ch,int i,int j)

  {

      if(i>=j) { Console.Write("Its Palindrome");  return;  }

      if(ch[i]==ch[j]) { checkPalindrome(ch,i+=1,j-=1);   }

      else{  Console.Write("Its not Palindrome");  return;   }  

  }

  static void Main() {

      string s="MADAM";

      char[] ch=s.ToCharArray();

    checkPalindrome(ch,0,s.Length-1);

  }

}

9)  print the factorial of the number n:

i/p:

5

o/p:

120

i/p:

3

o/p:

6

Solution:

using System;

class Program {

 public static void printFactorialOfNumber(int fact,int n)

 {

     if(n==0) {    Console.Write(fact);   return; }

     printFactorialOfNumber(fact*n,n-=1); 

 }

  static void Main() {

      int fact=3;

    printFactorialOfNumber(fact,fact-1);

  }

}

10) print the fibonnaci series of the number n:

i/p:

6

o/p:

0 1 1 2 3 5

Solution:

using System;

class Program {

 public static void printFibonacciSeries(int n,int i,int j)

 {

     if(n==0){ return; }

     if(i==0){ Console.Write("{0} ", i); }

      Console.Write("{0} ", j);

     int t=j;

     j=i+j;

     i=t;

     printFibonacciSeries(n-=1,i,j);

 } 

  static void Main() {

      int fib=10;

    printFibonacciSeries(fib-1,0,1);

  }

}


29 September 2021

Part-1 Recursion in C#

1) print the natural numbers 1 to n:

i/p:

20

o/p:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 

Solution:

using System;
public class GFG{
 public static int naturalNumbers(int a,int b){   

        //main logic- this condition should fail till reach our output values

        if(a>b) return a;

        Console.Write("{0} ",a);

        //Acting as iterator

        return naturalNumbers(a+1,b);

    }

static public void Main (){

       naturalNumbers(1,20);
}
}

2) print the natural number from n to 1(reverse order): 

i/p:

20

o/p:

20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 

Solution:

using System;
public class GFG{

    public static int naturalNumbers(int a)   {   

        if(a<1)  return a;  

        Console.Write("{0} ",a);

        return naturalNumbers(a-1);

    }

static public void Main (){

       naturalNumbers(20);

}

}

3) print the sum of the natural number from 1 to n: 

i/p:

3

o/p:

6 

sample: 1+2+3

Solution:

using System;

public class GFG{

    public static int naturalNumbers(int a,int k){   

        //main logic- this condition should fail till reach our output values

        if(a<1)

        return k;

        //Acting as iterator

        return naturalNumbers(a-1,k+=a);

    }

static public void Main (){

      Console.WriteLine(naturalNumbers(3,0));
}
}

4) print the natural number as separated: 

i/p: (int)

 31234

o/p:

3 1 2 3 4

Solution:

using System;

public class Program{  

    public static void naturalNumbers(int a)

    {
      if(a<=0)  return ;
    
      naturalNumbers(a/10);

      Console.WriteLine(a%10);
      return;

    }

static public void Main (){

    int a=1234;

  naturalNumbers(a);

}

}

5) Count & print the number of the natural number : 

i/p: (int)

 12345

o/p:

5

Solution:

using System;

class Program {

    public static int naturalNumbers(int a,int count)

    {

        //condition used to break the statement

        if(a<=0)

        return count;

         //method call used which will do the number reduction & count

        return naturalNumbers(a/10,count+=1);

    }

  static void Main() {

   Console.WriteLine(naturalNumbers(123465,0));

  }

}