Вы находитесь на странице: 1из 8

Generated by Foxit PDF Creator Foxit Software http://www.foxitsoftware.com For evaluation only.

Prof. Mukesh N Tekwani (9869 488 356) PATTERN PRINTING PROGRAMS

1.
1 23 456 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
using 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

Generated by Foxit PDF Creator Foxit Software http://www.foxitsoftware.com For evaluation only.

Prof. Mukesh N Tekwani (9869 488 356) 2.


// // // // // // Program 0 1 0 1 0 to print the following output: 1 0 1 0 0 1 0 1 1 0 1 0 0 1 0 1 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 < { for (int c = 0; { if ((r + c) x[r, c] else x[r, c] } } 5; r++) c < 5; c++) % 2 == 0) = 0; = 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

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 rows 8 O/p: 1 0 1 0 1 0 1 0

1 0 1 0 1 0 1

1 0 1 0 1 0

1 0 1 0 1

1 0 1 0

1 0 1

1 0

using 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("1\t"); else Console.Write("0\t"); } Console.WriteLine(); } Console.ReadLine(); } } }

Page 3 of 8

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 rows 8 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 1 0 1 0 1 1

using 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("1\t"); else Console.Write("0\t"); } Console.WriteLine(); } Console.ReadLine(); } } }

Page 4 of 8

Generated by Foxit PDF Creator Foxit Software http://www.foxitsoftware.com For evaluation only.

Prof. Mukesh N Tekwani (9869 488 356) 5.

1 22 333 4444 55555

using 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

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 // 1 2 3 // 5 6 7 // 9 10 11 pattern: 4 8 12

using 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

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 array Org list 12 -20 1 -32 65 -44 11 Sorted list -44 -32 -20 1 9 11 12
using 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 Console.WriteLine("Org for (int i = 0; i < n; { Console.Write(a[i] } list list"); i++) + "\t");

370 19

9 65

19 370

// 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

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 is consisting of more than 15 characters Please enter a name Mumbai, Maharashtra Name entered is too long
using 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

Вам также может понравиться