0% found this document useful (0 votes)
10 views11 pages

General Programsc#

Uploaded by

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

General Programsc#

Uploaded by

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

using System;

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

namespace CsharpPractice1
{
public class PracticeLevel1
{
public static void TestFib()
{
int a, b, c, n;
Console.WriteLine("Enter the number of terms:");
n = int.Parse(Console.ReadLine());
a = 0;
b = 1;
Console.Write($"{a} ");
Console.Write($"{b} ");
for (int i = 1; i <= n; i++)
{
c = a + b;
a = b;
b = c;
Console.Write($"{c} ");
}
}

public static int DigitReverse()


{
string result = "";
Console.WriteLine("Enter a 4-digit number: ");
int num = Convert.ToInt32(Console.ReadLine());
if (num <= 0)
{
return -1;
}
int x = num;
for (int i = 1; i <= 4; i++)
{
int digit = num % 10;
num = num / 10;
result += digit.ToString();
}
Console.WriteLine($"Reverse of {x} is : {result}");
Console.WriteLine("Enter a number to multiply with reverse number:");
int num1 = Convert.ToInt32(Console.ReadLine());
num = int.Parse(result) * num1;
Console.Write($"Multiplying {result} with {num1}: {num}");
return 1;
}

public static void CalculateAreaOfShape()


{
go:
Console.WriteLine("Choose a shape below to caluate its area: ");
Console.WriteLine("===========================================");
Console.WriteLine("Circle");
Console.WriteLine("Cylinder");
string shapeName = Console.ReadLine();
switch (shapeName.ToLower())
{
case "circle":
Console.WriteLine("Enter radius of a Circle:");
float radiusOfCircle = Convert.ToSingle(Console.ReadLine());

Console.WriteLine("What do you want to calculate: ");

Console.WriteLine("===========================================");
Console.WriteLine("Area");
Console.WriteLine("Diameter");

string calculationType = Console.ReadLine();


if (calculationType == "area")
{
double areaOfCircle = 3.14 * radiusOfCircle *
radiusOfCircle;
Console.WriteLine($"Area of circle with radius
{radiusOfCircle} is {areaOfCircle}");
}
else if (calculationType == "diameter")
{
double diameterOfCircle = 2 * radiusOfCircle;
Console.WriteLine($"Diameter of circle with radius
{radiusOfCircle} is {diameterOfCircle}");
}
else
{
Console.WriteLine("Input must be Area or Diameter");
}
break;
case "cylinder":
Console.WriteLine("What do you want to calculate: ");

Console.WriteLine("===========================================");
Console.WriteLine("BaseArea");
Console.WriteLine("Volume");
string calculationType1 = "";
calculationType1 = Console.ReadLine();
if (calculationType1 == "basearea")
{
Console.WriteLine("Enter radius of a Cylinder:");
float radiusOfCylinder =
Convert.ToSingle(Console.ReadLine());

double areaOfCylinder = 3.14 * radiusOfCylinder *


radiusOfCylinder;
Console.WriteLine($"Base Area of Cylinder with radius
{radiusOfCylinder} is {areaOfCylinder}");
}
else if (calculationType1 == "volume")
{
Console.WriteLine("Enter radius of a Cylinder:");
float radiusOfCylinder =
Convert.ToSingle(Console.ReadLine());
Console.WriteLine("Enter height of a Cylinder:");
float heightOfCylinder =
Convert.ToSingle(Console.ReadLine());

double areaOfCylinder = 2 * 3.14 * radiusOfCylinder *


radiusOfCylinder + 2 * 3.14 * radiusOfCylinder * heightOfCylinder;
Console.WriteLine($"Volume of Cylinder with radius
{radiusOfCylinder} and height {heightOfCylinder} is {areaOfCylinder}");
}
else
{
Console.WriteLine("Input must be BaseArea or Volume");
}
break;
}
Console.WriteLine("Do you want to check any more SHAPE enter yes . ");
if (Console.ReadLine() != "yes")
{
Console.WriteLine("Thank you");
Console.ReadKey();
}
else
{
goto go;
}
}

public static void ArmstrongNumberTest()


{
int n, r, sum = 0, temp;
n = Convert.ToInt32(Console.ReadLine());
temp = n;
while (n > 0)
{
r = n % 10;
sum = sum + r * r * r;
n = n / 10;
}
if (temp == sum)
{
Console.WriteLine($"{temp} is an ArmStrong number");
}
else
{
Console.WriteLine($"{temp} is not an ArmStrong number");
}
}

public static void Array_Number_Position()


{
int[] arr = new int[6] { 45, 2, 65, 87, 24, 65 };
Console.WriteLine("enter a number: ");
int n = Convert.ToInt32(Console.ReadLine());
int flag = 0; int k = 0;

foreach (int num in arr)


{
if (num == n)
{
flag++;
if (flag == 1)
Console.WriteLine($"Number {n} is in the position");
Console.WriteLine(k);
}
k++;
}
if (flag == 0)
{
Console.WriteLine("Number not found");
}
}

public static void PrimeNumberTest()


{
int i; int number;

Console.WriteLine("Enter number:");
number = Convert.ToInt32(Console.ReadLine());
int flag = 0;
for (i = 1; i <= number; i++)
{
if (number % i == 0)
{
flag++;
}
}
if (flag == 2)
{
Console.WriteLine($"{number} is prime");
}
else
{
Console.WriteLine($"{number} is not prime");
}

public static void Array_Max_1()


{
int[] arr = new int[6] { 45, 2, 65, 87, 24, 65 };
int n = 0;
foreach (int num in arr)
{
if (num > n)
{
n = num;
}
}
Console.WriteLine($"Max num in the array is: {n}");
}

public static void Array_Max_2()


{
int[] arr = new int[6] { 45, 2, 65, 87, 24, 65 };
int temp = 0; int i;
for (i = 0; i < 5; i++)
{
if (arr[i] > arr[i + 1])
{
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
Console.WriteLine($"Max number in the array : {arr[i]} ");
}

public static void Array_Ascending()


{
int[] arr = new int[6] { 145, 2, 265, 87, 24, 15 };
int temp = 0; int i;

for (int j = 5; j >= 1; j--)


{
for (i = 0; i < 5; i++)
{
if (arr[i] > arr[i + 1])
{
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
}
Console.WriteLine($"Array in Ascending order:");
foreach (int k in arr)
{
Console.WriteLine($"{k} ");
}
}

public static void Array_Descending()


{
int[] arr = new int[6] { 145, 2, 265, 87, 24, 15 };
int temp = 0; int i;

for (int j = 5; j >= 1; j--)


{
for (i = 0; i < 5; i++)
{
if (arr[i] < arr[i + 1])
{
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
}
Console.WriteLine($"Array in Descending order:");
foreach (int k in arr)
{
Console.WriteLine($"{k} ");
}
}

public static void Array_3()


{
System.Diagnostics.Stopwatch myStopWatch = new
System.Diagnostics.Stopwatch();
myStopWatch.Start();
int[] arr = new int[200];
int i;
for (i = 0; i <= 199; i++)
{
arr[i] = i;
}

myStopWatch.Stop();
Console.WriteLine($"Time taken :
{myStopWatch.Elapsed.Milliseconds.ToString()}");
}

public static void Array_FindPrime_In_Array_Final()


{
System.Diagnostics.Stopwatch myStopWatch = new
System.Diagnostics.Stopwatch();
myStopWatch.Start();
int[] arr = new int[1000];
int i; int flag = 0;
for (i = 0; i <= 999; i++)
{
arr[i] = i + 1;
}
for (i = 0; i <= 999; i++)
{
flag = 0;
for (int j = 1; j <= arr[i]; j++)
{
if (arr[i] % j == 0)
{
if (arr[i] == 1)
{
Console.WriteLine($"{arr[i]} is neutral");
break;
}
if (flag >= 3 && j == arr[i])
{
Console.WriteLine($"{arr[i]} is not a prime");
}

flag++;
if (flag == 2 && j == arr[i])
Console.WriteLine($"{arr[i]} is prime");
}
}
}
myStopWatch.Stop();
Console.WriteLine($"Time taken :
{myStopWatch.Elapsed.Milliseconds.ToString()}");
}

public static void Array_FindPrime_In_Array_1()


{
System.Diagnostics.Stopwatch myStopWatch = new
System.Diagnostics.Stopwatch();
myStopWatch.Start();
int[] arr = new int[10] { 251, 1, 241, 90, 277, 1, 132, 38, 199, 37 };
int flag = 0; int i;
for (i = 0; i <= 9; i++)
{
flag = 0;
int n = arr[i];
for (int j = 1; j <= n / 2; j++)
{
if (n % j == 0)
{
if (n == 1)
{
Console.WriteLine($"{n}\t is neutral");
break;
}
if (flag >= 3 && j == n)
{
Console.WriteLine($"{n}\t is not a prime");
}
flag++;
if (flag == 2 && j == n)
Console.WriteLine($"{n}\t is prime");
}
}
}
myStopWatch.Stop();
Console.WriteLine($"Time taken :
{myStopWatch.Elapsed.Milliseconds.ToString()}");
}

private static bool CheckPrime(int num)


{
bool b = false;
for (int i = 2; i <= num / 2; i++)
{
if (num % i == 0)
{
b = true;
break;
}
}
return b;
}

public static void FindPrimeNumbersInArray()


{
System.Diagnostics.Stopwatch myStopWatch = new
System.Diagnostics.Stopwatch();
myStopWatch.Start();

bool isPrime = false;

int[] arr = new int[10] { 251, 1, 241, 90, 277, 1, 132, 38, 199, 37 };
//int[] arr = new int[1000];
//int j;

//for (j = 0; j <= 999; j++)


//{
// arr[j] = j + 1;
//}
for (int i = 0; i <= 9; i++)
{
if (arr[i] == 1)
{
Console.WriteLine($"{arr[i]} is neutral");
}
else
{
isPrime = CheckPrime(arr[i]);
if (isPrime)
{
Console.WriteLine($"{arr[i]}\t is not a prime");
}
else
{
Console.WriteLine($"{arr[i]}\t is a prime");
}
}
}
myStopWatch.Stop();
Console.WriteLine($"Time taken :
{myStopWatch.Elapsed.Milliseconds.ToString()}");
}

public static void CalculatePowerOfANumber_Recursive()


{
int num, power, result;
Console.WriteLine("Enter number =");
num = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter exponent of number=");
power = Convert.ToInt32(Console.ReadLine());
result = CalculatePower_Recursive(num, power);
Console.WriteLine($"Recursive {num} to the power of {power} :
{result}");
}

private static int CalculatePower_Recursive(int number, int exponent)


{
if (exponent == 0)
return 1;
else
return number * CalculatePower_Recursive(number, exponent - 1);
}

public static void CalculatePowerOfANumber()


{
int num, power; long result = 1, i;
Console.WriteLine("Enter number =");
num = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter exponent of number=");
power = Convert.ToInt32(Console.ReadLine());
for (i = 0; i < power; i++)
{
result = result * num;
}
Console.WriteLine($" {num} to the power of {power} : {result}");
}

public static void CountDigitsInANUmber()


{
int n, i = 0, temp = 0;

Console.WriteLine("Enter number =");


n = Convert.ToInt32(Console.ReadLine());
temp = n;
if (n == 0)
{
Console.WriteLine("There is only 1 digit in your number.");
}
else
{
while (n != 0)
{
n = n / 10;
i++;
}
Console.WriteLine($"There are {i} digits in number {temp}");
}
}

public static void SumOfNaturalNumber()


{
int n, i, sum = 0;
Console.WriteLine("Enter of number");
n = Convert.ToInt32(Console.ReadLine());

for (i = 1; i <= n; i++)


{
sum += i;
}
Console.WriteLine($"Sume of {n} natural numbers is : {sum}");
}

public static void Array_Max_MIn()


{
int[] arr = new int[6] { 45, 2, 65, 87, 24, 65 };
int max = arr[0], i, min = arr[0]; ;
for (i = 0; i < arr.Length; i++)
{
if (arr[i] > max)
max = arr[i];
}
for (i = 0; i < arr.Length; i++)
{
if (arr[i] < min)
min = arr[i];
}

Console.WriteLine($"Max num and Min num in the array is: {max},{min}");


}

public static void Array_Third_Max()


{
int[] arr = new int[6] { 45, 2, 65, 87, 24, 65 };
int max1 = 0, max2 = 0, max3 = 0; int i; int n = 0;
for (i = 0; i < arr.Length; i++)
{
if (arr[i] > max1)
{
max3 = max2;
max2 = max1;
max1 = arr[i];
}
else if (arr[i] > max1)
{
max3 = max2;
max2 = arr[i];
}
else if (arr[i] > max1)

{
max3 = arr[i];
}
}
Console.WriteLine($"Third Max,Second Max,Max num : {max3},{max2},
{max1}");
}

public static void Median_Array()


{
int i, j, n = 0; float median = 0;
int[] arr = new int[6] { 45, 2, 65, 87, 24, 65 };

n = arr.Length;

Console.Write($"Array in ascending order: ");

int temp = 0;

for (j = 5; j >= 1; j--)


{
for (i = 0; i < 5; i++)
{
if (arr[i] < arr[i + 1])
{
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
}
foreach (var item in arr)
{
Console.Write($"{item} \n");
}

if (n % 2 == 0)
{
median = (arr[n / 2] + arr[n / 2 + 1]) / 2.0f;
Console.WriteLine($"The median is: {median} ");
}
else
{
median = arr[n / 2 + 1];
Console.WriteLine($"Array in ascending order: {median} ");
}
}

}
}

You might also like