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.
The structure of your program will be like this. You need to make it exactly working.
Switch(a) {
case‘ f’: factorial();
break;
case‘ p’: prime();
break;
case‘ t’: table();
break;
case‘ s’: search();
break;
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Problem41 {
class Problem41 {
static void Main(string[] args) {
Problem41 p1 = newProblem41();
Console.WriteLine("\t\tName: Ehtesham Mehmood\n\t\tRoll No: 11014119-131\n\t\tSection: AE\n \t\tUOG\n");
System.Console.Write(" Press t For Table \n Press f For Factorial \n Press p For Prime\n Press s For Search\n Press Character(except t,f,p) For Sequential Search");
System.Console.Write("\n\nInput a Character Value: ");
string a = System.Console.ReadLine();
switch (a) {
case "t":
System.Console.Write("Enter Number: ");
int n1 = Convert.ToInt32(System.Console.ReadLine());
System.Console.Write("Enter Number: ");
int n2 = Convert.ToInt32(System.Console.ReadLine());
p1.table(n1, n2);
break;
case "f":
System.Console.Write("Enter number: ");
int n3 = Convert.ToInt32(System.Console.ReadLine());
p1.factorial(n3);
break;
case "p":
System.Console.Write("Enter Nmber To Check It is Prime Or Not: ");
int n4 = Convert.ToInt32(System.Console.ReadLine());
p1.prime(n4);
break;
case "s":
int num;
char temp;
int count = 1;
char found;
Console.WriteLine("Enter The Numbers You Want To Enter:");
num = Convert.ToInt32(Console.ReadLine());
char[] array = newchar[num];
for (int i = 0; i < num; i++) {
Console.WriteLine("Enter " + count + " Character:");
array[i] = Convert.ToChar(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 Character You Want To Search:");
found = Convert.ToChar(Console.ReadLine());
p1.Binary_search(array, found, a, num); // Binary Seacrh Function call
break;
default:
Console.WriteLine("Sequential Search");
int num1;
char temp1;
int count1 = 1;
char found1;
Console.WriteLine("Enter The Numbers You Want To Enter:");
num1 = Convert.ToInt32(Console.ReadLine());
char[] array1 = newchar[num1];
for (int i = 0; i < num1; i++) {
Console.WriteLine("Enter " + count1 + " Character:");
array1[i] = Convert.ToChar(Console.ReadLine());
count1++;
}
for (int i = 0; i < num1; i++) {
for (int j = 0; j < num1 - 1; j++) {
if (array1[j] > array1[j + 1]) {
temp1 = array1[j];
array1[j] = array1[j + 1];
array1[j + 1] = temp1;
}
}
}
Console.WriteLine("Enter The Character You Want To Search:");
found1 = Convert.ToChar(Console.ReadLine());
p1.Binary_search(array1, found1, a, num1); // Binary Seacrh Function call
// System.Console.WriteLine("Invalid choice");
break;
}
Console.ReadKey();
}
//starts table funtion
private void table(int n1, int n2) {
int i;
int result;
System.Console.WriteLine("Table Of " + n1 + " Is");
for (i = 1; i <= n2; i++) {
result = n1 * i;
System.Console.WriteLine(n1 + "X" + i + "=" + result);
}
}
//starts factorial function
private void factorial(int n3) {
int fact = 1, i;
if (n3 % 2 == 0) {
for (i = 1; i <= n3; i++) {
fact = fact * i;
}
System.Console.Write("Factorial Is: " + fact);
} else {
System.Console.Write("Enter number Is Not A Multple Of 2, So Cannot See Its Factorial");
}
}
//starts prime function
private void prime(int n4) {
int c;
for (c = 2; c <= n4 - 1; c++) {
if (n4 % c == 0) {
System.Console.Write("Not A Prime Number");
break;
}
}
if (c == n4) System.Console.Write("Enter Number is Prime number");
}
//starts search function
private void Binary_search(char[] array, char found, string s, int num) {
if (s == "s") {
int first = 0;
int last;
int mid;
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();
} else {
int b = 1;
int i;
bool flag = true;
for (i = 0; i < num; i++) {
if (array[i] == found) {
Console.WriteLine(found + " Found At Location " + b);
flag = false;
}
b++;
}
if (flag) {
Console.WriteLine(found + "Is Not In The List ");
}
}
}
}
Output
Problem 42
Write two functions, max(int,int) and prime(int). The max function will take two arguments and will return the maximum of the two numbers to main. Then main function will pass this calculated factorial to the prime function that will show whether that passed value is prime or not.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Problem42 {
class Problem42 {
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");
Problem42 p = newProblem42();
int num1, num2;
int picker;
Console.WriteLine("Enter The num1:");
num1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter The num2:");
num2 = Convert.ToInt32(Console.ReadLine());
picker = p.max(num1, num2);
Console.WriteLine("The Maximum Is " + picker);
p.prime(picker);
Console.ReadKey();
}
private int max(int num1, int num2) {
if (num1 > num2) {
return num1;
} else {
return num2;
}
}
private void prime(int num) {
int i = 2;
while (i < num) {
if (num % i == 0) {
Console.WriteLine("\nThis Number Is A Composite Number.");
break;
}
i++;
}
if (i == num) {
Console.WriteLine("\nThis Number Is A Prime Number.");
}
if (num == 0 || num == 1) {
Console.WriteLine("\nThis Number Is Not A Composite Number Nor A Prime Number.");
}
}
}
}
Problem 43
Write a function that takes four arrays of the same size as arguments; array1, array2, array3 and array4. The function will multiply the corresponding values of array1 and array2 and will save the result at the same index as array3, in other words array3[0] = array1[0] * array2[0] and so on. Then create another function and pass all four arrays to that function as an argument where you need to compare array1, array2 and array3 and save the max value in array4. In other words if array1[0] = 10, array2[0] = 5, array3[0]= 11 then you need to save the max value, in other words array4[0] = 11. In the main function, show all the values of array4.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Problem43 {
class Problem43 {
public 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");
Problem43 p = newProblem43();
int[] array1 = newint[] {
1,
3,
5,
7,
9
};
int[] array2 = newint[] {
2,
4,
6,
8,
10
};
int[] array3 = newint[5];
int[] array4 = newint[5];
int[] temp = newint[5];
int[] temp2 = newint[5];
temp = p.mullarray(array1, array2, array3, array4);
Console.WriteLine("Values Of Array3 Which Is The Result Of Multiplication Of The Corresponding Indexes Of The Array1 And Array2:");
for (int i = 0; i < 5; i++) {
Console.WriteLine(temp[i]);
}
temp2 = p.mullarray(array1, array2, temp, array4);
Console.WriteLine("\nMaximum Values Of The All Of Them Which Stored In The Array4:");
for (int i = 0; i < 5; i++) {
Console.WriteLine(temp2[i]);
}
Console.ReadKey();
}
public int[] mullarray(int[] a1, int[] a2, int[] a3, int[] a4) {
for (int i = 0; i < 5; i++) {
a3[i] = a1[i] * a2[i];
}
return a3;
}
public int[] maxarray(int[] a1, int[] a2, int[] a3, int[] a4) {
for (int i = 0; i < 5; i++) {
if (a1[i] > a2[i] && a1[i] > a3[i]) {
a4[i] = a1[i];
}
if (a2[i] > a1[i] && a2[i] > a3[i]) {
a4[i] = a2[i];
}
if (a3[i] > a1[i] && a3[i] > a2[i]) {
a4[i] = a3[i];
}
}
return a4;
}
}
}
Output
Problem 44
If the lengths of the sides of a triangle are denoted by a, b, and c, then the area of the triangle is given by:
Area = SS(S-a)(S-b)(S-c)
where, S = ( a + b + c ) / 2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Problem44 {
class Problem44 {
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");
double a, b, c, S;
double Area;
Console.WriteLine("Enter Side A");
a = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Enter Side B");
b = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Enter Side C");
c = Convert.ToDouble(Console.ReadLine());
S = (a + b + c) / 2;
Area = S * Math.Sqrt(S * (S - a) * (S - b) * (S - c));
Console.WriteLine("Area Of The Triangle Is: " + Area);
Console.ReadKey();
}
}
}
Problem 45
Create a class of student that stores characteristcs of a student, like studentID, studentName, studentDOB, studentRollNo, studentEmail, studentGPA of the last 5 semesters and other related information of the student. (You may set some properties Boolean and some readonly.)
- Calculate the CGPA of each student using the function calculateCGPA(…).
- The student with the highest CGPA will be considerd CR of the class. There should be a function that will compare the CGPA of students and will declare a student having a greater CGPA as CR.
- The program should be able to take input of 5 students from the user; definitely there will be a function that will take input from the user.
- You need to use a Setter for setting the values of the data members of the class and a Getter function for getting the values of the data members of the class. (You can use a property as an alternative.)
- The default value for each student's GPA should be 3.0. (You need to use an array for storing GPAs of the last 5 semesters. You may simply initialize the array with 3.0 in the constructor).
- All data members should be private.
- Member functions can be public.
- The program must be able to add two objects, in other words if we add two students then all their corresponding data members will be added one by one. (Operator Overloading).
- The program must contain an explicitly defined copy constructor.
- StudentID and StudentGPA of the last 5 semesters must be constant or readonly.
- The value of studentGPA of the last 5 semesters must be selected from a enum that may contain values {1.0, 2.0, 3.0, and 4.0}.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Problem45 {
class Student {
private int studentID;
private string studentName;
private string studentDOB;
private string studentEmail;
private int studentRollNo;
private double studentGPA;
enum gpa_Range {
one = 1, two = 2, three = 3, four = 4
};
gpa_Range g;
public 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");
Student s1 = new Student();
Student s2 = new Student();
Student s3 = new Student();
Student s4 = new Student();
Student s5 = new Student();
Student s6 = new Student();
Student comp = new Student();
double[] picker = new double[5];
double[] st = new double[5];
Console.WriteLine("..............Data Of Student s1..............");
picker = s1.inputUser();
st[0] = s1.calculateCGPA(picker);
Console.WriteLine("..............Data Of Student s2..............");
picker = s2.inputUser();
st[1] = s2.calculateCGPA(picker);
s6 = s1 + s2;
s6.Display();
Console.WriteLine("..............Data Of Student s3..............");
picker = s3.inputUser();
st[2] = s3.calculateCGPA(picker);
Console.WriteLine("..............Data Of Student s4..............");
picker = s4.inputUser();
st[3] = s4.calculateCGPA(picker);
Console.WriteLine("..............Data Of Student s5..............");
picker = s5.inputUser();
st[4] = s5.calculateCGPA(picker);
Console.WriteLine("..............Here CR Selection Is Perform..............");
comp.compareCGPA(st[0], st[1], st[2], st[3], st[4]);
Console.ReadKey();
}
public Student() {
studentGPA = 3.0;
}
public Student(Student s) // copy constructor
{
studentID = s.studentID;
studentName = s.studentName;
studentDOB = s.studentDOB;
studentEmail = s.studentEmail;
studentRollNo = s.studentRollNo;
studentGPA = s.studentGPA;
}
public double[] arr = newdouble[5];
public double[] inputUser() {
Console.WriteLine("Enter Student ID:");
studentID = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter Student Name:");
studentName = Console.ReadLine();
Console.WriteLine("Enter Student DOB:");
studentDOB = Console.ReadLine();
Console.WriteLine("Enter Student Email:");
studentEmail = Console.ReadLine();
Console.WriteLine("Enter Student Roll No:");
studentRollNo = Convert.ToInt32(Console.ReadLine());
int choice;
int count = 1;
for (int i = 0; i < 5; i++) {
Console.WriteLine("Enter Student GPA Semester: " + count);
Console.WriteLine("Press 1 For 1gpa\nPress 2 For 2gpa\nPress 3 For 3gpa\nPress 4 For 3gpa");
choice = Convert.ToInt32(Console.ReadLine());
g = (gpa_Range) choice;
switch (g) {
casegpa_Range.one: arr[i] = (double) gpa_Range.one;
break;
casegpa_Range.two: arr[i] = (double) gpa_Range.two;
break;
casegpa_Range.three: arr[i] = (double) gpa_Range.three;
break;
casegpa_Range.four: arr[i] = (int) gpa_Range.four;
break;
}
count++;
}
return arr;
}
public double calculateCGPA(double[] pick) {
double cgpa = 0;
for (int i = 0; i < 5; i++) {
cgpa = cgpa + pick[i];
}
cgpa = cgpa / 5;
studentGPA = cgpa;
return cgpa;
}
public void compareCGPA(double s1, double s2, double s3, double s4, double s5) {
if ((s1 > s2) && (s1 > s3) && (s1 > s4) && (s1 > s5)) {
Console.WriteLine("Student s1 Is Become The CR With CGPA Of " + s1);
}
if ((s2 > s1) && (s2 > s3) && (s2 > s4) && (s2 > s5)) {
Console.WriteLine("Student s1 Is Become The CR With CGPA Of " + s2);
}
if ((s3 > s1) && (s3 > s2) && (s3 > s4) && (s3 > s5)) {
Console.WriteLine("Student s2 Is Become The CR With CGPA Of " + s3);
}
if ((s4 > s1) && (s4 > s2) && (s4 > s3) && (s4 > s5)) {
Console.WriteLine("Student s1 Is Become The CR With CGPA Of " + s4);
}
if ((s5 > s1) && (s4 > s2) && (s5 > s3) && (s5 > s4)) {
Console.WriteLine("Student s1 Is Become The CR With CGPA Of " + s5);
}
}
public static Studentoperator + (Student ss1, Student ss2) {
Student result = newStudent();
result.studentName = string.Copy(ss1.studentName);
string.Concat(result.studentName, ss2.studentName);
result.studentDOB = string.Copy(ss1.studentDOB);
string.Concat(result.studentDOB, ss2.studentDOB);
result.studentEmail = string.Concat(ss1.studentEmail, ss2.studentEmail);
result.studentID = ss1.studentID + ss2.studentID;
result.studentRollNo = ss1.studentRollNo + ss2.studentRollNo;
result.studentGPA = ss1.studentGPA + ss2.studentGPA;
return result;
}
public void Display() {
Console.WriteLine("Student ID IS " + studentID);
Console.WriteLine("Student Name IS " + studentName);
Console.WriteLine("Student DOB IS " + studentDOB);
Console.WriteLine("Student Email IS " + studentEmail);
Console.WriteLine("Student Roll No IS " + studentRollNo);
Console.WriteLine("Student CGPS IS " + studentGPA);
}
}
}
Output