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

Lesson 1

The document contains code to perform several tasks with arrays in C#: 1) Accept two numbers as input, swap their values and display the numbers before and after swapping. 2) Check if a given number is prime by testing if it is only divisible by 1 and itself. 3) Define an array, accept values, calculate sum and average of elements. 4) Define an array, accept a search value, and use linear search to check if the value exists in the array. 5) Find the maximum and minimum value in an array. 6) Define an array, accept values, and count the number of prime elements.

Uploaded by

Nandar Aung
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Lesson 1

The document contains code to perform several tasks with arrays in C#: 1) Accept two numbers as input, swap their values and display the numbers before and after swapping. 2) Check if a given number is prime by testing if it is only divisible by 1 and itself. 3) Define an array, accept values, calculate sum and average of elements. 4) Define an array, accept a search value, and use linear search to check if the value exists in the array. 5) Find the maximum and minimum value in an array. 6) Define an array, accept values, and count the number of prime elements.

Uploaded by

Nandar Aung
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

Accept two input numbers and exchange them

___________________________________________________________________________________
____
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleTest
{
internal class Program
{
static void Main(string[] args)
{
int num1, num2, temp;

Console.Write("Enter the first number :");


num1 = int.Parse(Console.ReadLine());

Console.WriteLine("Enter the second number :");


num2 = int.Parse(Console.ReadLine());

Console.WriteLine("Before swap");
Console.WriteLine(num1 +" "+ num2);
Console.WriteLine("After swap");
temp = num1;
num1 = num2;
num2 = temp;
Console.WriteLine(num1 + " " + num2);
Console.ReadKey();

}
}
}

___________________________________________________________________________________
_____________________

Check Prime

___________________________________________________________________________________
________________________

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleTest
{
internal class Program
{
static void Main(string[] args)
{
int num1, d;
char ch;

do {
Console.WriteLine("Enter number : ");
num1 = int.Parse(Console.ReadLine());
d = 2;

while (num1 % d != 0)
{
d++;
}

if (num1 == d)

Console.WriteLine("This is a prime number.");

else Console.WriteLine("This is not a prime number.");

Console.WriteLine("Do you want to continue?[y/n]");


ch = char.Parse(Console.ReadLine());
} while (ch =='y');
Console.ReadKey();
}
}
}

___________________________________________________________________________________
_______

ARRAY

___________________________________________________________________________________
_______

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleTest
{
internal class Program
{
static void Main(string[] args)
{
int[] A = new int[10];
int sum = 0;
int avg = 0;
for (int i = 0; i < A.Length; i++) {
Console.WriteLine("Enter arrary data");
A[i] = int.Parse(Console.ReadLine());
sum += A[i];

}
avg = sum / A.Length;
for (int i = 0; i < A.Length; i ++)
{
Console.WriteLine(A[i] + " ");
}
Console.WriteLine("Sum = " + sum);
Console.WriteLine("Average = " + avg);
Console.ReadKey();
}
}
}

___________________________________________________________________________________
_____________

Linear Search

___________________________________________________________________________________
______________

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleTest
{
internal class Program
{
static void Main(string[] args)
{
int size,search;
Console.WriteLine("Enter Array size : ");
size = int.Parse(Console.ReadLine());
int[] N = new int[size];

Console.WriteLine("Enter Array Data : ");


for (int i = 0; i < size; i++)
{
N[i]=int.Parse(Console.ReadLine());
}

Console.WriteLine("Enter the number you wabt to search");


search= int.Parse(Console.ReadLine());

Boolean found = false;


for (int i = 0; i < size; i++)
{
if (N[i] == search)
{
found = true; break;
}

}
if (found)
{
Console.WriteLine("Found Data!");
}
else Console.WriteLine("Not found.");
Console.ReadKey();
}
}
}

___________________________________________________________________________________
______________________

Exercises

___________________________________________________________________________________
_____________________

Find max and min in Array

___________________________________________________________________________________
_________________________

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleTest
{
internal class Program
{
static void Main(string[] args)
{
int size,max,min;

Console.WriteLine("Enter Array Size : ");


size = int.Parse(Console.ReadLine());
int[] A = new int[size];

Console.WriteLine("Enter Array Data : ");


for(int i = 0; i < size; i++)
{
A[i]=int.Parse(Console.ReadLine());

}
max = A[0];
min = A[0];
for (int i = 0; i < A.Length; i++)
{
if (A[i] > max)
max = A[i];

if (A[i]<min)
min = A[i];

}
Console.WriteLine("Maximum value is " + max);
Console.WriteLine("Minimum value is " + min);
Console.ReadKey();

}
}
}

___________________________________________________________________________________
_______________________

Find Number of prime elements in an array

___________________________________________________________________________________
________________________

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleTest
{
internal class Program
{
static void Main(string[] args)
{
int size;
int count = 0;
Console.WriteLine("Enter Array Size : ");
size = int.Parse(Console.ReadLine());
int[] A = new int[size];

Console.WriteLine("Enter Array Data : ");


for(int i = 0; i < size; i++)
{
A[i]=int.Parse(Console.ReadLine());

for(int j = 0; j < size; j++)


{
int d = 2;
while (A[j]%d != 0)
{
d++;
}

if (A[j]==d)
{

count++;
}
}
}
}
}

You might also like