Part 1 - Basic C# programming Problem and Solutions
Part 2 - Basic C# Programming Problem and Solutions
Today I am writing this article for the beginners who have just begun programming in the C# language. I have solved all the basic problems of the C# programming. Today I will show you Part 3 of my previous article Basic C# Programming Problems.
- It contains a OOP section in the end.
- It clarifes the concept of enums in C#.
Problem 31
Write a program to produce the following output:
1
2 3
4 5 6
7 8 9 10
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Problem31 {
class Problem31 {
static void Main(string[] args) {
int space = 30;
int num = 1;
for (int i = 1; i < 5; i++) {
for (int j = 1; j <= space; j++) {
Console.Write(" ");
}
space = space - 2;
for (int k = 1; k <= i; k++) {
Console.Write(num);
Console.Write(" ");
num++;
}
Console.WriteLine("\n");
}
Console.ReadKey();
}
}
}

Problem 32
Write a program that takes 10 values from the user in an array and then shows the number of prime values in the array.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Problem32 {
class Problem32 {
static void Main(string[] args) {
int[] array = newint[10];
int count = 0;
int i;
int j;
int k = 2;
for (i = 0; i < 10; i++) {
Console.WriteLine("Enter " + i + " Value");
array[i] = Convert.ToInt32(Console.ReadLine());
}
for (j = 0; j < 10; j++) {
for (k = 2; j < array[count]; k++) {
if (array[count] % k == 0) {
goto to;
}
}
to: if (k == array[count]) {
Console.WriteLine("Entered Number Is " + array[count] + " A Prime Number.");
}
if (array[count] == 0 || array[count] == 1) {
Console.WriteLine("Entered Number Is " + array[count] + " Not A Composite Number Nor A Prime Number.");
}
count++;
}
Console.ReadKey();
}
}
}

Problem 33
How many printf statements will be executed by this program and rewrite the following program without using a goto statement.
void main() {
int i, j, k;
for (i = 1; i <= 3; i++) {
for (j = 1; j <= 3; j++) {
for (k = 1; k <= 3; k++) {
if (i == 3 && j == 3 && k == 3) goto out;
else printf("%d %d %d\n", i, j, k);
}
}
}
out: printf("Out of the loop at last!");
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Problem33 {
class Problem33 {
static void Main(string[] args) {
int i, j, k;
for (i = 1; i <= 3; i++) {
for (j = 1; j <= 3; j++) {
for (k = 1; k <= 3; k++) {
if (i == 3 && j == 3 && k == 3) {
break; // without goto.
} else {
Console.WriteLine("I " + i + "J " + j + "K " + k);
}
}
}
}
Console.WriteLine("In This Program WritLine Will Print 26 Times.");
Console.ReadKey();
}
}
}

Problem 34
Write a program that takes n values from user and then sorts them in ascending order.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Problem34 {
class Problem34 {
static void Main(string[] args) {
int num;
int temp;
Console.WriteLine("Enter The Numbers You Want To Enter:");
num = Convert.ToInt32(Console.ReadLine());
int[] array = newint[num];
for (int i = 0; i < num; i++) {
Console.WriteLine("Enter " + i + " Value:");
array[i] = Convert.ToInt32(Console.ReadLine());
}
for (int i = 0; i < num; i++) {
for (int j = 0; j < num - 1; j++) {
if (array[j] > array[j + 1]) {
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
Console.WriteLine("In Ascending Order:");
for (int i = 0; i < num; i++) {
Console.WriteLine(array[i]);
}
Console.ReadKey();
}
}
}
Output

Problem 35
Write a program that takes n values from the user and then sorts them using a Bubble sort.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Problem35 {
class Problem35 {
static void Main(string[] args) {
Console.WriteLine("\t\tName: Ehtesham Mehmood\n\t\tRoll No: 11014119-131\n\t\tSection: AE\n \t\tUOG\n");
int num;
int temp;
Console.WriteLine("Enter The Numbers You Want To Enter:");
num = Convert.ToInt32(Console.ReadLine());
int[] array = newint[num];
for (int i = 0; i < num; i++) {
Console.WriteLine("Enter " + i + " Value:");
array[i] = Convert.ToInt32(Console.ReadLine());
}
for (int i = 0; i < num; i++) {
for (int j = 0; j < num - 1; j++) {
if (array[j] > array[j + 1]) {
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
Console.WriteLine("\nIn Ascending Order Using Bubble Sort:");
for (int i = 0; i < num; i++) {
Console.WriteLine(array[i]);
}
Console.ReadKey();
}
}
}

Problem 36
Write a program that takes n values in an array and then search for a value in the array using a binary search algorithm. Hint: You need to sort that array first because a binary search can be applied only on a sorted array.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Problem36 {
class Problem36 {
static void Main(string[] args) {
Console.WriteLine("\t\tName: Ehtesham Mehmood\n\t\tRoll No: 11014119-131\n\t\tSection: AE\n \t\t UOG\n");
int num;
int temp;
int first = 0;
int last;
int mid;
int found;
int count = 1;
Console.WriteLine("Enter The Numbers You Want To Enter:");
num = Convert.ToInt32(Console.ReadLine());
int[] array = newint[num];
for (int i = 0; i < num; i++) {
Console.WriteLine("Enter " + count + " Value:");
array[i] = Convert.ToInt32(Console.ReadLine());
count++;
}
for (int i = 0; i < num; i++) {
for (int j = 0; j < num - 1; j++) {
if (array[j] > array[j + 1]) {
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
Console.WriteLine("Enter The Number You Want To Search:");
found = Convert.ToInt32(Console.ReadLine());
last = num - 1;
mid = (first + last) / 2;
while (first <= last) {
if (array[mid] < found) first = mid + 1;
elseif(array[mid] == found) {
Console.WriteLine(found + " Found At Location " + mid);
break;
}
else last = mid - 1;
mid = (first + last) / 2;
}
if (first > last) Console.WriteLine("Not Found " + found + " Is Not Present In The List.\n");
Console.ReadKey();
}
}
}
Output

Problem 37
Write a program that copies the values of one array to a second array in reverse order.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Problem37 {
class Problem37 {
static void Main(string[] args) {
Console.WriteLine("\t\tName: Ehtesham Mehmood\n\t\tRoll No: 11014119-131\n\t\tSection: AE\n \t\t UOG\n");
int[] array1 = newint[] {
1,
2,
3,
4,
5,
6,
7,
8,
9,
10
};
int[] array2 = newint[10];
Console.WriteLine("Array1 Values:");
for (int i = 0; i < 10; i++) {
Console.Write(array1[i]);
Console.Write(" ");
}
int j = 9;
Console.WriteLine("\n\nCopied Values To The Array2 In Reverse Order:");
for (int i = 0; i < 10; i++) {
array2[i] = array1[j];
j--;
}
for (int i = 0; i < 10; i++) {
Console.Write(array2[i]);
Console.Write(" ");
}
Console.ReadKey();
}
}
}

Problem 38
Create two arrays, student_rollno and student_marks, both of the same size. The first array will save the rollnos of students and the second array will save the marks of students against his rollno. For example if student_rollno[0] contains 197 then student_marks[0] will contain the marks of roll no 197.
You need to print the roll number of the student with maximum marks.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Problem38 {
class Problem38 {
static void Main(string[] args) {
Console.WriteLine("\t\tName: Ehtesham Mehmood\n\t\tRoll No: 11014119-131\n\t\tSection: AE\n \t\t UOG\n");
int[] student_rollno = newint[5];
int[] student_marks = newint[5];
int max;
int count = 0;
int temp = 1;
for (int i = 0; i < 5; i++) {
Console.WriteLine("Enter The " + temp + " Students Roll No:");
student_rollno[i] = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter The " + temp + " Students Marks:");
student_marks[i] = Convert.ToInt32(Console.ReadLine());
temp++;
}
//for (int i = 0; i < 5; i++)
//{
// Console.WriteLine(student_rollno[i]);
//}
max = student_marks[0];
for (int i = 0; i < 5; i++) {
if (max < student_marks[i]) {
count++;
max = student_marks[i];
}
}
Console.WriteLine("\n");
if (count == 0) {
Console.WriteLine("Student Roll No= " + student_rollno[0] + " Students Maximum Marks Is= " + max);
}
if (count == 1) {
Console.WriteLine("Student Roll No= " + student_rollno[1] + " Students Maximum Marks Is= " + max);
}
if (count == 2) {
Console.WriteLine("Student Roll No= " + student_rollno[2] + " Students Maximum Marks Is= " + max);
}
if (count == 3) {
Console.WriteLine("Student Roll No= " + student_rollno[3] + " Students Maximum Marks Is= " + max);
}
if (count == 4) {
Console.WriteLine("Student Roll No= " + student_rollno[4] + " Students Maximum Marks Is= " + max);
}
Console.ReadKey();
}
}
}

Problem 39
Write a function that takes one value as a parameter and display the sum of the digits in the value. For example, if the user has passed 125 then the function will print 1+2+5 = 7.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Problem39 {
class Problem39 {
static void Main(string[] args) {
Problem39 p = newProblem39();
string num;
int a;
int count;
Console.WriteLine("Enter A Number:");
num = Console.ReadLine();
count = num.Length;
a = int.Parse(num);
p.cal(a, count);
Console.ReadKey();
}
private void cal(int num, int count) {
int a;
int sum = 0;
for (int i = 1; i <= count; i++) {
a = num % 10;
num = num / 10;
sum = sum + a;
}
Console.WriteLine("The Sum Of The Individual Numbers Of The Entered Number Is:" + sum);
}
}
}
Output

Problem 40
Write a function that takes two values, n1 and n2, as arguments and multiply the n1 by itself n2 times. In other words, calculate n1n2 and then return the result to the main function that will display the result.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Problem40 {
class Problem40 {
static void Main(string[] args) {
Problem40 p = newProblem40();
double n1, n2;
Console.WriteLine("Enter n1 Value:");
n1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter n2 Value:");
n2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("The n1^n2 Is :" + p.Cal(n1, n2));
Console.ReadKey();
}
private double Cal(double n1, double n2) {
n1 = Math.Pow(n1, n2);
return n1;
}
}
}
Output

Problem 41
Write a program that will input a charater "a" value from the user. You need to use a switch statement to decide if the value of a is "t" then you need to call the table function. If the value of a is "f" then call the factorial function, if the value of a is "p" then call the prime function, if the value of a is "s" then call the search function.
You need to write four functions in your program as in the following:
Table(int n1,n2)
Factorial(int n3)
Prime(int n4)
Search(char n5[], char c, char choice)
- Table function will print the table of n1 from 1 to n2.
- Factorial function will print the factorial of n3 if n3 is a multiple of 2.
- Prime function will print the n4 if n4 is prime.
- Search function will take the first argument n5 as an array of characters and the second element a character to be searched for in the array and the third element a character to decide which searching algorithm is to be used, in other words if the user has passed the value of c as "s" then the Search function will perform the sequential search but if the value of c is something else then the Search function will perform a binary search.






Join the conversation! Your thoughts help the community grow.