Skip to main content

Posts

Showing posts from October, 2019

Beginner's BUTTERFLY PATTERN

PRINT BUTTERFLY PATTERN USING C# using System; public class Test { public static void Main() { var no = Console.ReadLine(); int n = Convert.ToInt32(no); Pattern print = new Pattern(); print.Butterfly(n); } } public class Pattern { public void Butterfly(int n) {   int totalNoofStars = n*2;   int lefttriangle = 1;   int righttriangle = totalNoofStars;   for(int i=1;i<=totalNoofStars;i++)   {   if(i<=n)   {   lefttriangle++;   righttriangle--;   }   else   {    if(i==n+1)   {   lefttriangle= lefttriangle;   righttriangle = righttriangle;   }   else   {   lefttriangle--;   righttriangle++;   }   }   for(int j=1;j<=totalNoofStars;j++)   {   if(j<lefttriangle || j> righttriangle)   {   Console.Write("*");   }   else   {   Console.Write(" ");   }   }   Console.WriteL

Beginner's hollow rectangle

using System; public class Test { public static void Main() { var length = Console.ReadLine(); int l = Convert.ToInt32(length); var breadth = Console.ReadLine(); int b = Convert.ToInt32(breadth); PrintRectangle print = new PrintRectangle(); print.PrintHollowRectangle(l,b); } } public class PrintRectangle { public void PrintHollowRectangle(int l,int b) { for(int i=0;i<b;i++) { for(int j=0;j<l;j++) { if(i!=0 && i!=b-1 && j!=0 && j!=l-1) { Console.Write("   "); } else { Console.Write(" * "); } } Console.WriteLine(); } } } INPUT 5  4 OUTPUT * * * * * * * * * * * * * *

Beginner's Rectangle

using System; public class Test { public static void Main() { var length = Console.ReadLine(); int l = Convert.ToInt32(length); var breadth = Console.ReadLine(); int b = Convert.ToInt32(breadth); PrintPattern print = new PrintPattern(); print.PrintRectangle(l,b); } } public class PrintPattern { public void PrintRectangle(int l,int b) { for(int i=0;i<b;i++) { for(int j=0;j<l;j++) { Console.Write("*"); } Console.WriteLine(); } } } Input: 5 3 Output ***** ***** *****

BEGINNER's INVERTED HALF PYRAMID

using System; public class Test { public static void Main() { var number = Console.ReadLine(); int n = Convert.ToInt32(number); PrintPattern print = new PrintPattern(); print.Printpyramid(n); } } public class PrintPattern { public void Printpyramid(int n) { int m = n/2; int dummy=1; for(int i=0;i<n;i++) { for(int j=n;j>i;j--) {          if(i!=0 && i!=n-1 && i!=n-2 && j!=n && j!=dummy)          {           Console.Write(" ");          }          else          {           Console.Write("*");          } }      dummy=dummy+1; Console.WriteLine(); } } }

BEGINNER's HOLLOW DIAMOND

using System; public class Test { public static void Main() { var no = Console.ReadLine(); int n = Convert.ToInt32(no); Pattern print = new Pattern(); //declaration print.HollowDiamond(n); } } //definition public class Pattern { public void HollowDiamond(int n) { int lefttriangle = n; int righttriangle=lefttriangle; int dummy=n; int m = n*2; for(int i=0;i<m;i++) { for(int j=0;j<m;j++) { if(j==lefttriangle || j==righttriangle) { Console.Write("*"); } else { Console.Write(" "); } } if(i<n-1) {   lefttriangle--;   righttriangle++; } else if(i==(n-1)) { lefttriangle=lefttriangle ; righttriangle=righttriangle; } else {       lefttriangle++;   righttriangle--; } Console.WriteLine(); } } } OUTPUT * * * *

BEGINNER's REMOVE QUESTION MARKS AND PRINT STRING

Given a string with elements having lowercase letters and question marks. Replace each question mark with lowercase letter such that the element is not equal to the element before it and the element after it. Ex:- Input String:- abcab??bac? Output:- abcabacbaca (There can be a lot of different outputs, anyone of them was accepted) Input String:- ???????? Output:- abcdefgh SOLUTION: using System ; using System. Linq ; public class RemoveQMark { public string getcleantext ( string text ) { char [ ] textarray = text. ToCharArray ( ) ; int n = text. Length ; char [ ] cleantextarray = new char [ n ] ; int j = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( textarray [ i ] != '?' ) { cleantextarray [ j ] = textarray [ i ] ; j ++; } } string cleantext = new String ( cleantextarray. Where ( c => ! char . IsWhiteSpace ( c ) ) . Distinct ( ) . ToArray ( ) ) ; string clean = cleantext. Trim ( ) ; return

Print word pattern - odd letters - beginner's code

1. Print the word with odd letters as P M R A O R G O R R A P M Solution: using System; public class Test { public static void Main() { string text = Console.ReadLine(); int n=text.Length; Pattern print = new Pattern (); print.XPattern(text,n); } } public class Pattern { public void XPattern(string text,int n) { char[] textarray = text.ToCharArray(); int reverseprinter = n-1; int middleLetter = n/2; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { if(i==j && j!=middleLetter) { Console.Write(textarray[j]); } else if(j==reverseprinter) { Console.Write(textarray[reverseprinter]); reverseprinter--; } else { Console.Write("*"); } } Console.WriteLine(); } } } OUTPUT Please Enter text: INPUT : program p m r a o r g o r r a p m

Beginner's Code for Diamond Pattern

using System; public class Test {       public static void Main()       {             var n = Console.ReadLine();             int no = Convert.ToInt32(n);             Pattern print = new Pattern();             print.Diamond(no);       } } public class Pattern {       public void Diamond(int n)       {             int m = n*2;                         int dummy=n;             for(int i=0;i<=m;i++)             {                   if(i<n)                   {                         dummy --;                   }                   else                   {                         if(i==n)                         {                               dummy=dummy;                         }                         else                         {                         dummy++;                         }                   }                   for(int j=0;j<n;j++)                   {                         if(j<dummy)                         {       

Beginner's Code for Inverted Full Pyramid

using System; public class Test { public static void Main() { var number = Console.ReadLine(); int n = Convert.ToInt32(number); int dummy = 0; for(int i=0;i<n;i++) { if(i>0) { dummy = i ; } for(int j=0;j<n;j++) { if(j<dummy ) { Console.Write(" "); } else { Console.Write("* "); } } Console.WriteLine(); } } } stdout Please Enter n * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Inverse Half Hollow Pyramid in C# - Beginner

using System; public class Test { public static void Main() { var number = Console.ReadLine(); int n = Convert.ToInt32(number); PrintPattern print = new PrintPattern(); print.Printpyramid(n); } } public class PrintPattern { public void Printpyramid(int n) { int dummy=1; for(int i=0;i<n;i++) { for(int j=n;j>i;j--) { if(i==0||j==n||j==dummy) { Console.Write("* "); } else { Console.Write(" "); } } dummy++; Console.WriteLine(); } } } OUTPUT * * * * * * * * * * * * * * *

Beginner's Code For Half Pyramid in C#

To print the Half Pyramid pattern program using stars in C# Solution: using System;  namespace Reference  { public class Pattern { public void PrintPattern(int no) { for(int i=0;i<no;i++)   {         for(int j=0;j<=i;j++) { Console.Write("*");          } Console.WriteLine();           }     }     }     public class Test     { public static void Main() { Console.WriteLine("Please Enter n:"); var n=Console.ReadLine(); int number =Convert.ToInt32(n); Pattern po = new Pattern();      po.PrintPattern(number); } } } Output Please Enter n: * ** *** **** *****