0% found this document useful (0 votes)
5 views30 pages

C# Programming Basics and Examples

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views30 pages

C# Programming Basics and Examples

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

●Approved by AICTE, Ministry of HRD, Govt.

of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018


Affiliated: ●VMSB Uttarakhand Technical University ● Sri Dev Suman Uttarakhand University ●Uttarakhand Board of Technical Education

Experiment No.: 1
Problem Statement: Write a C# program to perform basic arithmetic operations (addition,
subtraction, multiplication, and division) based on user input.
Objective:
• Understand user input handling in C#.
• Implement basic arithmetic operations using C#.
Required device – Computer

Solution:

using System;

class ArithmeticOperations
{
static void Main()
{
[Link]("Enter first number: ");
double num1 = [Link]([Link]());

[Link]("Enter second number: ");


double num2 = [Link]([Link]());

[Link]($"Addition: {num1 + num2}");


[Link]($"Subtraction: {num1 - num2}");
[Link]($"Multiplication: {num1 * num2}");
[Link]($"Division: {num1 / num2}");
}
}

Vision
• To become centre of excellence in software development and research

Mission:
• To induce ethical values and spirit of social commitment.
• To provide a learning ambience to enhance innovations and problem-solving skills.
4040
• To provide a comprehensive education, benchmarked against the highest global standards.
• To Collaborate with software industry and adopting technology for achieving quality of
technical education.
• To promote research based projects / activities in the emerging areas of technology
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ●VMSB Uttarakhand Technical University ● Sri Dev Suman Uttarakhand University ●Uttarakhand Board of Technical Education

Output: -

Enter first number: 10


Enter second number: 5
Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2

Vision
• To become centre of excellence in software development and research

Mission:
• To induce ethical values and spirit of social commitment.
• To provide a learning ambience to enhance innovations and problem-solving skills.
4040
• To provide a comprehensive education, benchmarked against the highest global standards.
• To Collaborate with software industry and adopting technology for achieving quality of
technical education.
• To promote research based projects / activities in the emerging areas of technology
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ●VMSB Uttarakhand Technical University ● Sri Dev Suman Uttarakhand University ●Uttarakhand Board of Technical Education

Experiment No.: 2
Problem Statement: Write a C# program to check whether a number is even or odd.
Objective: Understand conditional statements in C#.
SOLUTION:

using System;

class EvenOddCheck
{
static void Main()
{
[Link]("Enter a number: ");
int num = Convert.ToInt32([Link]());

if (num % 2 == 0)
[Link]($"{num} is Even");
else
[Link]($"{num} is Odd");
}
}

Vision
• To become centre of excellence in software development and research

Mission:
• To induce ethical values and spirit of social commitment.
• To provide a learning ambience to enhance innovations and problem-solving skills.
4040
• To provide a comprehensive education, benchmarked against the highest global standards.
• To Collaborate with software industry and adopting technology for achieving quality of
technical education.
• To promote research based projects / activities in the emerging areas of technology
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ●VMSB Uttarakhand Technical University ● Sri Dev Suman Uttarakhand University ●Uttarakhand Board of Technical Education

Output: -

Enter a number: 7
7 is Odd

Vision
• To become centre of excellence in software development and research

Mission:
• To induce ethical values and spirit of social commitment.
• To provide a learning ambience to enhance innovations and problem-solving skills.
4040
• To provide a comprehensive education, benchmarked against the highest global standards.
• To Collaborate with software industry and adopting technology for achieving quality of
technical education.
• To promote research based projects / activities in the emerging areas of technology
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ●VMSB Uttarakhand Technical University ● Sri Dev Suman Uttarakhand University ●Uttarakhand Board of Technical Education

Experiment No.: 3
Problem Statement: Write a C# program to find the largest among three numbers.
Objective: Understand conditional statements and comparison operations.
SOLUTION:
using System;

class LargestNumber
{
static void Main()
{
[Link]("Enter first number: ");
int a = Convert.ToInt32([Link]());

[Link]("Enter second number: ");


int b = Convert.ToInt32([Link]());

[Link]("Enter third number: ");


int c = Convert.ToInt32([Link]());

int largest = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);


[Link]($"Largest number is: {largest}");
}
}

Vision
• To become centre of excellence in software development and research

Mission:
• To induce ethical values and spirit of social commitment.
• To provide a learning ambience to enhance innovations and problem-solving skills.
4040
• To provide a comprehensive education, benchmarked against the highest global standards.
• To Collaborate with software industry and adopting technology for achieving quality of
technical education.
• To promote research based projects / activities in the emerging areas of technology
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ●VMSB Uttarakhand Technical University ● Sri Dev Suman Uttarakhand University ●Uttarakhand Board of Technical Education

Output:-
Enter first number: 10
Enter second number: 20
Enter third number: 15
Largest number is: 20

Vision
• To become centre of excellence in software development and research

Mission:
• To induce ethical values and spirit of social commitment.
• To provide a learning ambience to enhance innovations and problem-solving skills.
4040
• To provide a comprehensive education, benchmarked against the highest global standards.
• To Collaborate with software industry and adopting technology for achieving quality of
technical education.
• To promote research based projects / activities in the emerging areas of technology
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ●VMSB Uttarakhand Technical University ● Sri Dev Suman Uttarakhand University ●Uttarakhand Board of Technical Education

Experiment No.: 4
Problem Statement: Write a C# program to reverse a given string.
Objective: Use loops or built-in methods for string reversal.
SOLUTION:
using System;

class ReverseString
{
static void Main()
{
[Link]("Enter a string: ");
string str = [Link]();

char[] charArray = [Link]();


[Link](charArray);

[Link]($"Reversed string: {new string(charArray)}");


}
}

Vision
• To become centre of excellence in software development and research

Mission:
• To induce ethical values and spirit of social commitment.
• To provide a learning ambience to enhance innovations and problem-solving skills.
4040
• To provide a comprehensive education, benchmarked against the highest global standards.
• To Collaborate with software industry and adopting technology for achieving quality of
technical education.
• To promote research based projects / activities in the emerging areas of technology
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ●VMSB Uttarakhand Technical University ● Sri Dev Suman Uttarakhand University ●Uttarakhand Board of Technical Education

Output: -
Enter a string: Hello
Reversed string: olleH

Vision
• To become centre of excellence in software development and research

Mission:
• To induce ethical values and spirit of social commitment.
• To provide a learning ambience to enhance innovations and problem-solving skills.
4040
• To provide a comprehensive education, benchmarked against the highest global standards.
• To Collaborate with software industry and adopting technology for achieving quality of
technical education.
• To promote research based projects / activities in the emerging areas of technology
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ●VMSB Uttarakhand Technical University ● Sri Dev Suman Uttarakhand University ●Uttarakhand Board of Technical Education

Experiment No.: 5
Problem Statement: Write a C# program to calculate the factorial of a number using
recursion.
Objective: Implement factorial calculation using a recursive function.
Solution:

using System;

class FactorialRecursion
{
static int Factorial(int n)
{
return (n == 0 || n == 1) ? 1 : n * Factorial(n - 1);
}

static void Main()


{
[Link]("Enter a number: ");
int num = Convert.ToInt32([Link]());

[Link]($"Factorial of {num} is {Factorial(num)}");


}
}

Vision
• To become centre of excellence in software development and research

Mission:
• To induce ethical values and spirit of social commitment.
• To provide a learning ambience to enhance innovations and problem-solving skills.
4040
• To provide a comprehensive education, benchmarked against the highest global standards.
• To Collaborate with software industry and adopting technology for achieving quality of
technical education.
• To promote research based projects / activities in the emerging areas of technology
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ●VMSB Uttarakhand Technical University ● Sri Dev Suman Uttarakhand University ●Uttarakhand Board of Technical Education

Output:

Enter a number: 5
Factorial of 5 is 120

Vision
• To become centre of excellence in software development and research

Mission:
• To induce ethical values and spirit of social commitment.
• To provide a learning ambience to enhance innovations and problem-solving skills.
4040
• To provide a comprehensive education, benchmarked against the highest global standards.
• To Collaborate with software industry and adopting technology for achieving quality of
technical education.
• To promote research based projects / activities in the emerging areas of technology
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ●VMSB Uttarakhand Technical University ● Sri Dev Suman Uttarakhand University ●Uttarakhand Board of Technical Education

Experiment No.: 6
Problem Statement: Write a C# program to generate the Fibonacci series up to a given number
of terms.
Objective: Generate Fibonacci sequence using iterative or recursive approach.
SOLUTION:
using System;

class FibonacciSeries
{
static void Main()
{
[Link]("Enter number of terms: ");
int terms = Convert.ToInt32([Link]());

int a = 0, b = 1, c;
[Link]("Fibonacci Series: " + a + " " + b + " ");

for (int i = 2; i < terms; i++)


{
c = a + b;
[Link](c + " ");
a = b;
b = c;
}
}
}

Vision
• To become centre of excellence in software development and research

Mission:
• To induce ethical values and spirit of social commitment.
• To provide a learning ambience to enhance innovations and problem-solving skills.
4040
• To provide a comprehensive education, benchmarked against the highest global standards.
• To Collaborate with software industry and adopting technology for achieving quality of
technical education.
• To promote research based projects / activities in the emerging areas of technology
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ●VMSB Uttarakhand Technical University ● Sri Dev Suman Uttarakhand University ●Uttarakhand Board of Technical Education

Output:-
Enter number of terms: 6
Fibonacci Series: 0 1 1 2 3 5

Vision
• To become centre of excellence in software development and research

Mission:
• To induce ethical values and spirit of social commitment.
• To provide a learning ambience to enhance innovations and problem-solving skills.
4040
• To provide a comprehensive education, benchmarked against the highest global standards.
• To Collaborate with software industry and adopting technology for achieving quality of
technical education.
• To promote research based projects / activities in the emerging areas of technology
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ●VMSB Uttarakhand Technical University ● Sri Dev Suman Uttarakhand University ●Uttarakhand Board of Technical Education

Experiment No.: 7
Problem Statement: Write a C# program to check whether a given string is a palindrome.
Objective: Implement logic to check palindrome.
SOLUTION:
using System;

class PalindromeCheck
{
static void Main()
{
[Link]("Enter a string: ");
string str = [Link]();
string rev = new string([Link]().ToArray());

if ([Link](rev, [Link]))
[Link]("Palindrome");
else
[Link]("Not a Palindrome");
}
}

Vision
• To become centre of excellence in software development and research

Mission:
• To induce ethical values and spirit of social commitment.
• To provide a learning ambience to enhance innovations and problem-solving skills.
4040
• To provide a comprehensive education, benchmarked against the highest global standards.
• To Collaborate with software industry and adopting technology for achieving quality of
technical education.
• To promote research based projects / activities in the emerging areas of technology
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ●VMSB Uttarakhand Technical University ● Sri Dev Suman Uttarakhand University ●Uttarakhand Board of Technical Education

Output:-
Enter a string: madam
Palindrome

Vision
• To become centre of excellence in software development and research

Mission:
• To induce ethical values and spirit of social commitment.
• To provide a learning ambience to enhance innovations and problem-solving skills.
4040
• To provide a comprehensive education, benchmarked against the highest global standards.
• To Collaborate with software industry and adopting technology for achieving quality of
technical education.
• To promote research based projects / activities in the emerging areas of technology
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ●VMSB Uttarakhand Technical University ● Sri Dev Suman Uttarakhand University ●Uttarakhand Board of Technical Education

Experiment No.: 8
Problem Statement: Write a C# program to check whether a number is prime.
Objective: Implement prime number logic efficiently.
SOLUTION:

using System;

class PrimeCheck
{
static void Main()
{
[Link]("Enter a number: ");
int num = Convert.ToInt32([Link]());

bool isPrime = num > 1;


for (int i = 2; i <= [Link](num); i++)
{
if (num % i == 0)
{
isPrime = false;
break;
}
}

[Link](isPrime ? "Prime" : "Not Prime");


}
}

Vision
• To become centre of excellence in software development and research

Mission:
• To induce ethical values and spirit of social commitment.
• To provide a learning ambience to enhance innovations and problem-solving skills.
4040
• To provide a comprehensive education, benchmarked against the highest global standards.
• To Collaborate with software industry and adopting technology for achieving quality of
technical education.
• To promote research based projects / activities in the emerging areas of technology
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ●VMSB Uttarakhand Technical University ● Sri Dev Suman Uttarakhand University ●Uttarakhand Board of Technical Education

Output:-
Enter a number: 7
Prime

Vision
• To become centre of excellence in software development and research

Mission:
• To induce ethical values and spirit of social commitment.
• To provide a learning ambience to enhance innovations and problem-solving skills.
4040
• To provide a comprehensive education, benchmarked against the highest global standards.
• To Collaborate with software industry and adopting technology for achieving quality of
technical education.
• To promote research based projects / activities in the emerging areas of technology
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ●VMSB Uttarakhand Technical University ● Sri Dev Suman Uttarakhand University ●Uttarakhand Board of Technical Education

Experiment No.: 9
Problem Statement: Write a C# program to swap two numbers without using a third variable.
Objective: Learn how to swap numbers using arithmetic operations.
Solution:
using System;

class SwapNumbers
{
static void Main()
{
[Link]("Enter first number: ");
int a = Convert.ToInt32([Link]());

[Link]("Enter second number: ");


int b = Convert.ToInt32([Link]());

a = a + b;
b = a - b;
a = a - b;

[Link]($"After swapping: First Number = {a}, Second Number = {b}");


}
}

Vision
• To become centre of excellence in software development and research

Mission:
• To induce ethical values and spirit of social commitment.
• To provide a learning ambience to enhance innovations and problem-solving skills.
4040
• To provide a comprehensive education, benchmarked against the highest global standards.
• To Collaborate with software industry and adopting technology for achieving quality of
technical education.
• To promote research based projects / activities in the emerging areas of technology
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ●VMSB Uttarakhand Technical University ● Sri Dev Suman Uttarakhand University ●Uttarakhand Board of Technical Education

Output:-
Enter first number: 5
Enter second number: 10
After swapping: First Number = 10, Second Number = 5

Vision
• To become centre of excellence in software development and research

Mission:
• To induce ethical values and spirit of social commitment.
• To provide a learning ambience to enhance innovations and problem-solving skills.
4040
• To provide a comprehensive education, benchmarked against the highest global standards.
• To Collaborate with software industry and adopting technology for achieving quality of
technical education.
• To promote research based projects / activities in the emerging areas of technology
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ●VMSB Uttarakhand Technical University ● Sri Dev Suman Uttarakhand University ●Uttarakhand Board of Technical Education

Experiment No.: 10
Problem Statement: Write a C# program to count the number of vowels in a given string.
Objective: Learn string iteration and character comparison.
Solution:
using System;

class VowelCount
{
static void Main()
{
[Link]("Enter a string: ");
string str = [Link]().ToLower();

int count = 0;
foreach (char c in str)
{
if ("aeiou".Contains(c))
count++;
}

[Link]($"Number of vowels: {count}");


}
}

Vision
• To become centre of excellence in software development and research

Mission:
• To induce ethical values and spirit of social commitment.
• To provide a learning ambience to enhance innovations and problem-solving skills.
4040
• To provide a comprehensive education, benchmarked against the highest global standards.
• To Collaborate with software industry and adopting technology for achieving quality of
technical education.
• To promote research based projects / activities in the emerging areas of technology
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ●VMSB Uttarakhand Technical University ● Sri Dev Suman Uttarakhand University ●Uttarakhand Board of Technical Education

Output: -

Enter a string: Hello World


Number of vowels: 3

Vision
• To become centre of excellence in software development and research

Mission:
• To induce ethical values and spirit of social commitment.
• To provide a learning ambience to enhance innovations and problem-solving skills.
4040
• To provide a comprehensive education, benchmarked against the highest global standards.
• To Collaborate with software industry and adopting technology for achieving quality of
technical education.
• To promote research based projects / activities in the emerging areas of technology
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ●VMSB Uttarakhand Technical University ● Sri Dev Suman Uttarakhand University ●Uttarakhand Board of Technical Education

Experiment No.: 11
Problem Statement: Write a C# program to reverse a given number.
Objective: Learn how to manipulate numbers as strings.
Solution:
using System;

class ReverseNumber
{
static void Main()
{
[Link]("Enter a number: ");
int num = Convert.ToInt32([Link]());

int rev = 0;
while (num > 0)
{
rev = rev * 10 + num % 10;
num /= 10;
}

[Link]($"Reversed number: {rev}");


}
}

Vision
• To become centre of excellence in software development and research

Mission:
• To induce ethical values and spirit of social commitment.
• To provide a learning ambience to enhance innovations and problem-solving skills.
4040
• To provide a comprehensive education, benchmarked against the highest global standards.
• To Collaborate with software industry and adopting technology for achieving quality of
technical education.
• To promote research based projects / activities in the emerging areas of technology
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ●VMSB Uttarakhand Technical University ● Sri Dev Suman Uttarakhand University ●Uttarakhand Board of Technical Education

Output: -
Enter a number: 1234
Reversed number: 4321

Vision
• To become centre of excellence in software development and research

Mission:
• To induce ethical values and spirit of social commitment.
• To provide a learning ambience to enhance innovations and problem-solving skills.
4040
• To provide a comprehensive education, benchmarked against the highest global standards.
• To Collaborate with software industry and adopting technology for achieving quality of
technical education.
• To promote research based projects / activities in the emerging areas of technology
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ●VMSB Uttarakhand Technical University ● Sri Dev Suman Uttarakhand University ●Uttarakhand Board of Technical Education

Experiment No.: 12
Problem Statement: Write a C# program to check if a number is an Armstrong number.
Objective: Learn the concept of Armstrong numbers.
Solution:
using System;

class ArmstrongCheck
{
static void Main()
{
[Link]("Enter a number: ");
int num = Convert.ToInt32([Link]());

int sum = 0, temp = num, digits = [Link]().Length;

while (temp > 0)


{
int digit = temp % 10;
sum += (int)[Link](digit, digits);
temp /= 10;
}

[Link](num == sum ? "Armstrong Number" : "Not an Armstrong


Number");
}
}

Vision
• To become centre of excellence in software development and research

Mission:
• To induce ethical values and spirit of social commitment.
• To provide a learning ambience to enhance innovations and problem-solving skills.
4040
• To provide a comprehensive education, benchmarked against the highest global standards.
• To Collaborate with software industry and adopting technology for achieving quality of
technical education.
• To promote research based projects / activities in the emerging areas of technology
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ●VMSB Uttarakhand Technical University ● Sri Dev Suman Uttarakhand University ●Uttarakhand Board of Technical Education

Output: -
Enter a number: 153
Armstrong Number

Vision
• To become centre of excellence in software development and research

Mission:
• To induce ethical values and spirit of social commitment.
• To provide a learning ambience to enhance innovations and problem-solving skills.
4040
• To provide a comprehensive education, benchmarked against the highest global standards.
• To Collaborate with software industry and adopting technology for achieving quality of
technical education.
• To promote research based projects / activities in the emerging areas of technology
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ●VMSB Uttarakhand Technical University ● Sri Dev Suman Uttarakhand University ●Uttarakhand Board of Technical Education

Experiment No.: 13
Problem Statement: Write a C# program to calculate the sum of digits of a number.
Objective: Learn number decomposition using loops.
Solution:
using System;

class SumOfDigits
{
static void Main()
{
[Link]("Enter a number: ");
int num = Convert.ToInt32([Link]());

int sum = 0;
while (num > 0)
{
sum += num % 10;
num /= 10;
}

[Link]($"Sum of digits: {sum}");


}
}

Vision
• To become centre of excellence in software development and research

Mission:
• To induce ethical values and spirit of social commitment.
• To provide a learning ambience to enhance innovations and problem-solving skills.
4040
• To provide a comprehensive education, benchmarked against the highest global standards.
• To Collaborate with software industry and adopting technology for achieving quality of
technical education.
• To promote research based projects / activities in the emerging areas of technology
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ●VMSB Uttarakhand Technical University ● Sri Dev Suman Uttarakhand University ●Uttarakhand Board of Technical Education

Output: -
Enter a number: 1234
Sum of digits: 10

Vision
• To become centre of excellence in software development and research

Mission:
• To induce ethical values and spirit of social commitment.
• To provide a learning ambience to enhance innovations and problem-solving skills.
4040
• To provide a comprehensive education, benchmarked against the highest global standards.
• To Collaborate with software industry and adopting technology for achieving quality of
technical education.
• To promote research based projects / activities in the emerging areas of technology
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ●VMSB Uttarakhand Technical University ● Sri Dev Suman Uttarakhand University ●Uttarakhand Board of Technical Education

Experiment No.: 14
Problem Statement: Write a C# program to find the second largest element in an array.
Objective: Learn array traversal and conditional checks.
Solution:
using System;

class SecondLargest
{
static void Main()
{
int[] arr = { 10, 20, 4, 45, 99, 32 };

int largest = [Link], secondLargest = [Link];


foreach (int num in arr)
{
if (num > largest)
{
secondLargest = largest;
largest = num;
}
else if (num > secondLargest && num != largest)
{
secondLargest = num;
}
}

[Link]($"Second Largest Element: {secondLargest}");


}
}

Vision
• To become centre of excellence in software development and research

Mission:
• To induce ethical values and spirit of social commitment.
• To provide a learning ambience to enhance innovations and problem-solving skills.
4040
• To provide a comprehensive education, benchmarked against the highest global standards.
• To Collaborate with software industry and adopting technology for achieving quality of
technical education.
• To promote research based projects / activities in the emerging areas of technology
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ●VMSB Uttarakhand Technical University ● Sri Dev Suman Uttarakhand University ●Uttarakhand Board of Technical Education

Output:-
Second Largest Element: 45

Vision
• To become centre of excellence in software development and research

Mission:
• To induce ethical values and spirit of social commitment.
• To provide a learning ambience to enhance innovations and problem-solving skills.
4040
• To provide a comprehensive education, benchmarked against the highest global standards.
• To Collaborate with software industry and adopting technology for achieving quality of
technical education.
• To promote research based projects / activities in the emerging areas of technology
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ●VMSB Uttarakhand Technical University ● Sri Dev Suman Uttarakhand University ●Uttarakhand Board of Technical Education

Experiment No.: 15
Problem Statement: Write a C# program to find the Greatest Common Divisor (GCD) of two
numbers.
Objective: Learn Euclidean algorithm for GCD calculation.
Solution:
using System;

class GCD
{
static int FindGCD(int a, int b)
{
return b == 0 ? a : FindGCD(b, a % b);
}

static void Main()


{
[Link]("Enter first number: ");
int a = Convert.ToInt32([Link]());

[Link]("Enter second number: ");


int b = Convert.ToInt32([Link]());

[Link]($"GCD of {a} and {b} is {FindGCD(a, b)}");


}
}

Vision
• To become centre of excellence in software development and research

Mission:
• To induce ethical values and spirit of social commitment.
• To provide a learning ambience to enhance innovations and problem-solving skills.
4040
• To provide a comprehensive education, benchmarked against the highest global standards.
• To Collaborate with software industry and adopting technology for achieving quality of
technical education.
• To promote research based projects / activities in the emerging areas of technology
●Approved by AICTE, Ministry of HRD, Govt. of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018
Affiliated: ●VMSB Uttarakhand Technical University ● Sri Dev Suman Uttarakhand University ●Uttarakhand Board of Technical Education

Output:-

Enter first number: 48


Enter second number: 18
GCD of 48 and 18 is 6

Vision
• To become centre of excellence in software development and research

Mission:
• To induce ethical values and spirit of social commitment.
• To provide a learning ambience to enhance innovations and problem-solving skills.
4040
• To provide a comprehensive education, benchmarked against the highest global standards.
• To Collaborate with software industry and adopting technology for achieving quality of
technical education.
• To promote research based projects / activities in the emerging areas of technology

You might also like