0% found this document useful (0 votes)
3 views

OOPs Lab Report

This document contains an assignment for the Structured Programming Language Lab at Uttara University, detailing various programming tasks. Each task includes a code snippet in C# for operations such as reversing a string, finding the largest number in an array, checking for prime numbers, and implementing a basic calculator, among others. The assignment is submitted by a student named Md Mahfujur Rahman to lecturer Mohsin Hossain.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

OOPs Lab Report

This document contains an assignment for the Structured Programming Language Lab at Uttara University, detailing various programming tasks. Each task includes a code snippet in C# for operations such as reversing a string, finding the largest number in an array, checking for prime numbers, and implementing a basic calculator, among others. The assignment is submitted by a student named Md Mahfujur Rahman to lecturer Mohsin Hossain.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Department of Computer Science and Engineering

Uttara University, Uttara, Dhaka


Assignment on Structured Programming Language
Course Code: CSE0611302
Course Title: Structured Programming Language Lab

Submitted to
Mohsin Hossain
Lecturer
Department of Computer Science and Engineering
Uttara University, Uttara, Dhaka

Submitted by
Md Mahfujur Rahman
Department of Computer Science and Engineering
Uttara University
Semester: Spring-2025
Student Id: 2241081221
Semester: 3rd

1. Reverse a string.
Code:
class Program
{
static void Main(string[] args)
{
System.Console.Write("\nEnter a Name: ");
string name = Console.ReadLine();

string newStr="";
for (int i = name.Length-1; i>=0; i--){
newStr += name[i];
}

System.Console.WriteLine($"\nOld String: {name}");


System.Console.WriteLine($"Reverse String: {newStr}\n\n");

}
}

Output:

2. Find the largest number in an array.


Code:
class Program
{
static void Main()
{
System.Console.Write("\nEnter size of the array: ");
int size = int.Parse(Console.ReadLine());
int [] arrays = new int[size];
// Take input in array
for(int i=0; i<size; i++){
System.Console.Write($"Element of [{i+1}]: ");
arrays[i] = int.Parse(Console.ReadLine());
}
int large = arrays[0];
for(int i=1; i<size-1; i++){
if(arrays[i]>large){
large= arrays[i];
}
}
System.Console.WriteLine($"Large number in arrays is:
{large}");
}
}

Output:

3. Check if a number is prime.

Code:
class Program
{
static void Main(string[] args)
{
System.Console.Write("Enter an number: ");
int n = int.Parse(Console.ReadLine());
int j =1;

if(n <= 1){


System.Console.WriteLine($"\n{n} is not an prime number.");
return;
}
if(n % 2 ==0){
System.Console.WriteLine($"\n{n} is not an prime number.");
return;
}
if(n == 2){
System.Console.WriteLine($"\n{n} is an prime number.");
return;
}

for(int i =3; i<Math.Sqrt(n); i+=2){ // we can use also (n-1)


as a loop condition.
if(n%i == 0){
System.Console.WriteLine($"\n{n} is not an prime
number.");
return;
}
}
System.Console.WriteLine($"\n{n} is an prime number.");
}
}

Output:

4. Calculate the factorial of a number.


Code:
class Program
{
static void Main(string[] args)
{
System.Console.Write("Enter an number: ");
int n = int.Parse(Console.ReadLine());

int fac=1;
for(int i=1; i<=n; i++){
fac = fac*i;
}
System.Console.WriteLine($"Factorial of number {n} is: {fac}");
}
}

Output:

5. Implement a basic calculator.


Code:
class Program
{
static void Main(string[] args)
{
System.Console.Write("Enter 1st number: ");
int n1 = int.Parse(Console.ReadLine());
System.Console.Write("Enter 2nd number: ");
int n2 = int.Parse(Console.ReadLine());
System.Console.Write("Enter an operator: ");
string operators = Console.ReadLine();

switch (operators)
{
case "+":
System.Console.WriteLine($"{n1} + {n2} is: {n1+n2}");
break;

case "-":
System.Console.WriteLine($"{n1} - {n2} is: {n1-n2}");
break;

case "*":
System.Console.WriteLine($"{n1} * {n2} is: {n1*n2}");
break;

case "/":
System.Console.WriteLine($"{n1} / {n2} is: {n1/n2}");
break;
default:
System.Console.WriteLine("Invalid operetor.");
break;
}
}
}

Output:

6. Check if a string is a palindrome.


Code:
class Program
{
static void Main(string[] args)
{
System.Console.WriteLine("Enter a string: ");
string str = Console.ReadLine();

string newStr = "";


for(int i=str.Length-1; i>=0; i--){

newStr += str[i];
}

if(newStr == str){
System.Console.WriteLine("String is palindrome.");
}
else{
System.Console.WriteLine("String isn't palindrome");
}
}
}

Output:
7. Generate the Fibonacci sequence.
Code:
class Program
{
static void Main(string[] args)
{
System.Console.Write("\n\nEnter the digit to you want feb
sequence: ");
int n = int.Parse(Console.ReadLine());

int a = 0;
int b = 1;
System.Console.Write($"\nFebonacci sequence is: {0}, {1}");
for(int i=1; i<n-1; i++ ){
int temp = a+b;
System.Console.Write(", "+temp);
a =b;
b=temp;
}
}
}

Output:

8. Find the sum of an array.


Code:
class Program
{
static void Main(string[] args)
{
int [] numArr = { 5, 10, 15, 20 };

System.Console.Write("\nSum of the array is: ");


int sum =0;
for(int i=0; i<numArr.Length; i++){
sum+= numArr[i];
}
System.Console.Write(sum+"\n\n");
}
}

Output:

9. Convert decimal to binary.


Code:
using System;

class Program
{
static void Main(string[] args)
{
Console.Write("Enter a decimal number: ");
int decimalNumber = int.Parse(Console.ReadLine());

string binaryNumber = Convert.ToString(decimalNumber, 2);

Console.WriteLine($"Binary representation: {binaryNumber}");


}
}

Output:
10. Find the second largest element in an array.

Code:
class Program
{
static void Main(string[] args)
{
int [] arr = {4,6,3,7,9};
int large = arr[0];
int secLarge = int.MinValue;
for(int i=0; i<arr.Length; i++){
if(arr[i]> large){
large = arr[i];
}
}

for(int i =0; i<arr.Length; i++){


if(arr[i]>secLarge && arr[i] != large){
secLarge = arr[i];
}
}
for(int i=0; i<arr.Length; i++){
System.Console.Write(arr[i]+" ");
}
System.Console.WriteLine($"\n2nd Large value is: {secLarge}");

}
}

Output:

11. Merge two sorted arrays.


Code:
Output:
12. Check if two strings are anagrams.

Code:
Output:

13. Remove duplicates from an array.

Code:
Output:

14. Find the first non-repeating character in a string.

Code:
Output:

15. Count the number of vowels in a string.

Code:
Output:

16. Implement a stack using an array.

Code:
Output:
17. Reverse an array.
Code:
class Program
{
static void Main(string[] args)
{
int [] arr = {1,2,3,4,5};
int n = arr.Length;
int [] newArr = new int[n];

for(int i=0; i<arr.Length; i++){


newArr[i]= arr[n-i-1];
}
for(int i =0; i<n; i++){
System.Console.Write(arr[i]+" ");
}
System.Console.WriteLine();
for(int i =0; i<n; i++){
System.Console.Write(newArr[i]+" ");
}
}
}

Output:

18. Sort an array (Bubble Sort).

Code:
class Program
{
static void Main(string[] args)
{
System.Console.WriteLine("\n");

int [] arr = {4,6,1,23,8,56,3};


System.Console.Write("Unsoted array is: ");
for(int i =0; i<arr.Length; i++){
System.Console.Write(arr[i]+" ");
}
System.Console.WriteLine("\n");
for(int i=0; i<arr.Length-1; i++){
for (int j=0; j<arr.Length-i-1; j++){
if(arr[j]>arr[j+1]){
int temp = arr[j];
arr[j]= arr[j+1];
arr[j+1]= temp;
}
}
}
System.Console.Write("\nSorted array is: ");
for(int i =0; i<arr.Length; i++){
System.Console.Write(arr[i]+" ");
}
System.Console.WriteLine("\n");

}
}

Output:

19. Find the greatest common divisor (GCD) of two numbers.

Code:
using System;

class Program
{
static void Main(string[] args)
{
Console.Write("Enter the first number: ");
int a = int.Parse(Console.ReadLine());
Console.Write("Enter the second number: ");
int b = int.Parse(Console.ReadLine());

int gcd = CalculateGCD(a, b);

Console.WriteLine($"The GCD of {a} and {b} is {gcd}");


}

static int CalculateGCD(int a, int b)


{
while (b != 0)
{
int remainder = a % b;
a = b;
b = remainder;
}

return a;
}
}

Output:

20. Calculate the power of a number.

Code:
class Program
{
static void Main(string[] args)
{
System.Console.WriteLine("Enter number: ");
int n = int.Parse(Console.ReadLine());
System.Console.WriteLine("Enter power: ");
int p = int.Parse(Console.ReadLine());
double result = Math.Pow(n,p);
System.Console.WriteLine("Ans is: "+result);
}
}

Output:

You might also like