Upcoming SlideShare
×

# Pattern printing programs

16,576 views

Published on

C# - Pattern printing programs

Published in: Education
1 Comment
1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• thnks

Are you sure you want to  Yes  No
Views
Total views
16,576
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
293
1
Likes
1
Embeds 0
No embeds

No notes for slide

### Pattern printing programs

1. 1. Generated by Foxit PDF Creator © Foxit Software http://www.foxitsoftware.com For evaluation only.Prof. Mukesh N Tekwani (9869 488 356) PATTERN PRINTING PROGRAMS1.1234567 8 9 1011 12 13 14 1516 17 18 19 20 2122 23 24 25 26 27 2829 30 31 32 33 34 35 3637 38 39 40 41 42 43 44 4546 47 48 49 50 51 52 53 54 55using System;namespace Pattern1{ class Program { static void Main() { int num = 1; int i, j; for (i = 1; num <= 55; i++) { for (j = 1; j <= i; j++) { Console.Write(num.ToString() + " "); num++; } Console.Write("n"); } Console.ReadLine(); } }} Page 1 of 8
2. 2. Generated by Foxit PDF Creator © Foxit Software http://www.foxitsoftware.com For evaluation only.Prof. Mukesh N Tekwani (9869 488 356)2.// Program to print the following output:// 0 1 0 1 0// 1 0 1 0 1// 0 1 0 1 0// 1 0 1 0 1// 0 1 0 1 0// Logic:// Consider first row as r = 0 and first col as c = 0.// From output, we observe that whenever r + c is an even no,// we display a 0 and whenever r + c is odd we display a 1.using System;namespace Pattern2{ class Program { static void Main() { int[,] x = new int[5, 5]; for (int r = 0; r < 5; r++) { for (int c = 0; c < 5; c++) { if ((r + c) % 2 == 0) x[r, c] = 0; else x[r, c] = 1; } } for (int r = 0; r < 5; r++) { for (int c = 0; c < 5; c++) { Console.Write("{0}t", x[r, c]); } Console.WriteLine(); } Console.ReadLine(); } }} Page 2 of 8
3. 3. Generated by Foxit PDF Creator © Foxit Software http://www.foxitsoftware.com For evaluation only.Prof. Mukesh N Tekwani (9869 488 356)3.I/p:Enter no. of rows8O/p:10 11 0 10 1 0 11 0 1 0 10 1 0 1 0 11 0 1 0 1 0 10 1 0 1 0 1 0 1using System;namespace Pattern6{ class Program { public static void Main() { int h; // height of triangle Console.WriteLine("Enter no. of rows "); h = int.Parse(Console.ReadLine()); Console.WriteLine("1"); for (int r = 1; r < h; r++) { for(int c = 0; c <= r; c++) { if((r + c) % 2 == 0) Console.Write("1t"); else Console.Write("0t"); } Console.WriteLine(); } Console.ReadLine(); } }} Page 3 of 8
4. 4. Generated by Foxit PDF Creator © Foxit Software http://www.foxitsoftware.com For evaluation only.Prof. Mukesh N Tekwani (9869 488 356)4.Enter no. of rows80 1 0 1 0 1 0 11 0 1 0 1 0 10 1 0 1 0 11 0 1 0 10 1 0 11 0 10 11using System;namespace Pattern7{ class Program { public static void Main() { int h; // height of triangle Console.WriteLine("Enter no. of rows "); h = int.Parse(Console.ReadLine()); for (int r = h; r >= 1; r--) { for (int c = 1; c <= r; c++) { if ((r + c) % 2 == 0) Console.Write("1t"); else Console.Write("0t"); } Console.WriteLine(); } Console.ReadLine(); } }} Page 4 of 8
5. 5. Generated by Foxit PDF Creator © Foxit Software http://www.foxitsoftware.com For evaluation only.Prof. Mukesh N Tekwani (9869 488 356)5.122333444455555using System;namespace Pattern100{ class Program { static void Main() { for (int r = 1; r <= 5; r++) { Console.WriteLine(" "); if (r >= 10) break; for (int c = 1; c <= 5; c++) { // Console.Write(" * "); Console.Write(r); if (c == r) goto loop1; } loop1: continue; } Console.ReadLine(); } }} Page 5 of 8
6. 6. Generated by Foxit PDF Creator © Foxit Software http://www.foxitsoftware.com For evaluation only.Prof. Mukesh N Tekwani (9869 488 356)6.//To print the pattern:// 1 2 3 4// 5 6 7 8// 9 10 11 12using System;namespace Arrays4{ class TwoD { public static void Main() { int r, c; int[,] x = new int[3, 4]; for (r = 0; r < 3; ++r) { for (c = 0; c < 4; ++c) { x[r, c] = (r * 4) + c + 1; Console.Write(x[r, c] + " "); } Console.WriteLine(); } Console.ReadLine(); } }} Page 6 of 8
7. 7. Generated by Foxit PDF Creator © Foxit Software http://www.foxitsoftware.com For evaluation only.Prof. Mukesh N Tekwani (9869 488 356)7. Bubble sort of a numeric arrayOrg list12 -20 1 -32 65 -44 11 370 9 19Sorted list-44 -32 -20 1 9 11 12 19 65 370using System;namespace BubbleSort{ class Program { static void Main() { int[] a = {12, -20, 1, -32, 65, -44, 11, 370, 9, 19}; int temp; int ind = 0; int n = a.Length; //display the original list Console.WriteLine("Org list"); for (int i = 0; i < n; i++) { Console.Write(a[i] + "t"); } // now sort do { ind = 0; for (int i = 0; i < n-1; i++) { if (a[i] > a[i + 1]) { temp = a[i]; a[i] = a[i + 1]; a[i + 1] = temp; ind = 1; // since swapping has taken ind = 1 } } } while (ind != 0); //display the sorted list Console.WriteLine("Sorted list"); for (int i = 0; i < n; i++) { Console.Write(a[i] + "t"); } Console.ReadLine(); } }} Page 7 of 8
8. 8. Generated by Foxit PDF Creator © Foxit Software http://www.foxitsoftware.com For evaluation only.Prof. Mukesh N Tekwani (9869 488 356)8. Exception – from question paper. WAP to display an error msg if name entered isconsisting of more than 15 charactersPlease enter a nameMumbai, MaharashtraName entered is too longusing System;namespace ThrowingExceptions{ class StringTooLong : Exception { } class MyCheck { static public string chkinput(string x) { if (x.Length > 15) throw (new StringTooLong()); return (x); } } class Program { static void Main() { string name; try { Console.WriteLine("Please enter a name "); name = Console.ReadLine(); MyCheck.chkinput(name); Console.WriteLine("Program ends here"); Console.ReadLine(); } catch (StringTooLong) { Console.WriteLine("Name entered is too long"); Console.ReadLine(); } } }} Page 8 of 8