Display String in a Pattern – Nagarro Interview Question with Answer

How to display a string in the pattern below

Example : HELLO
Output :
Nagaro



Here are the three ways given below to solve the above problem .

using System;

namespace Nagarro
{
    class Program
    {
        static void Main(string[] args)
        {
            Print();
            Console.ReadKey();
        }

        /// <summary>
        /// Print any word with this format . Example : ABC - 
        /// Output--> ABC , AB , C , BC , B , C - Replace ',' with new Line
        /// </summary>
        private static void Print()
        {
            Console.Write("Enter a string : ");
            string result = Console.ReadLine();
            
            while (result.Length > 0)
            {
                string temp = result;
                while (temp.Length > 0)
                {
                    Console.WriteLine(temp);
                    temp = temp.Remove(temp.Length - 1);
                }
                Console.WriteLine("------------------------------");
                result = result.Remove(0, 1);
            }
        }
    }
}
using System;

namespace Nagarro
{
    class Program
    {
        static void Main(string[] args)
        {
            Print();
            Console.ReadKey();
        }

        /// <summary>
        /// Print any word with this format . Example : ABC  
        /// Output--> ABC , AB , C , BC , B , C - Replace ',' with new Line
        /// </summary>
        private static void Print()
        {
            Console.Write("Enter a  string : ");
            string result = Console.ReadLine();
            int imax = result.Length;
            for (int i = 1; i <= imax; i++)
            {
                MethodA(result, imax, i, i - 1);
                Console.WriteLine("-----------------------");
            }
        }

        private static void MethodA(string a, int max, int x, int y)
        {
            int imax = max;
            {
                for (int i = y; i < imax; imax--)
                {
                    for (int j = i; j < imax; j++)
                    {
                        Console.Write(a[j] + " ");
                    }
                    Console.Write("\n");
                }
            }
        }
    }
}
using System;

namespace Nagarro
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter a string : ");
            string result = Console.ReadLine();
            Print(result);
            Console.ReadKey();
        }

        /// <summary>
        /// Print any word with this format . Example : ABC 
        /// Output--> ABC , AB , C , BC , B , C - Replace ',' with new Line
        /// </summary>
        private static void Print(string result)
        {
            if (result.Length <= 0) return;
            string temp = result;
            MethodA(temp);
            Console.WriteLine("..........................");
            Print(result.Remove(0, 1)); // Recursive
        }

        private static void MethodA(string tem)
        {
            if (tem.Length <= 0) return;
            Console.WriteLine(tem);
            MethodA(tem.Remove(tem.Length - 1));// Recursive
        }
    }
}