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

c#.bhjr

Uploaded by

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

c#.bhjr

Uploaded by

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

1.

using System;

class PyramidPattern

static void Main()

int n = 5; // Number of rows

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

for (int j = i; j < n; j++)

Console.Write(" ");

for (int k = 1; k <= (2 * i - 1); k++)

Console.Write("*");

Console.WriteLine();

2.

using System;

class SeriesSum

static void Main()

Console.Write("Enter the value of x: ");

double x = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter the number of terms: ");

int terms = Convert.ToInt32(Console.ReadLine());

double sum = 0.0;

int sign = 1;

for (int i = 1; i <= 2 * terms - 1; i += 2)

sum += sign * Math.Pow(x, i);

sign *= -1;

Console.WriteLine("The sum of the series is: " + sum);

Enter the value of x: 5

Enter the number of terms: 6

The sum of the series is: -46950120

3.

using System;

class DivisibleByNine

static void Main()

int start = 100;

int end = 200;

int count = 0;

int sum = 0;
Console.WriteLine("Numbers between 100 and 200 that are divisible by 9:");

for (int i = start; i <= end; i++)

if (i % 9 == 0)

Console.Write(i + " ");

count++;

sum += i;

Console.WriteLine("\n\nTotal count of numbers divisible by 9: " + count);

Console.WriteLine("Sum of numbers divisible by 9: " + sum);

Numbers between 100 and 200 that are divisible by 9:

108 117 126 135 144 153 162 171 180 189 198

Total count of numbers divisible by 9: 11

Sum of numbers divisible by 9: 1683

4.

using System;

class ArrayDifference

static void Main()

{
int[] numbers = new int[5];

// Input 5 integers from the user

Console.WriteLine("Enter 5 integers:");

for (int i = 0; i < 5; i++)

Console.Write("Enter integer " + (i + 1) + ": ");

numbers[i] = Convert.ToInt32(Console.ReadLine());

int smallest = numbers[0];

int largest = numbers[0];

// Find the smallest and largest elements

foreach (int number in numbers)

if (number < smallest)

smallest = number;

if (number > largest)

largest = number;

int difference = largest - smallest;

Console.WriteLine("The smallest element is: " + smallest);

Console.WriteLine("The largest element is: " + largest);

Console.WriteLine("The difference between the smallest and largest elements is: " + difference);

Enter 5 integers:
Enter integer 1: 6

Enter integer 2: 5

Enter integer 3: 4

Enter integer 4: 3

Enter integer 5: 8

The smallest element is: 3

The largest element is: 8

The difference between the smallest and largest elements is: 5

5.

using System;

class SortArray

static void Main()

int[] numbers = { 5, 3, 8, 1, 2 };

// Display original array

Console.WriteLine("Original array:");

foreach (int number in numbers)

Console.Write(number + " ");

Console.WriteLine();

Array.Sort(numbers);

Console.WriteLine("Array in ascending order:");

foreach (int number in numbers)

{
Console.Write(number + " ");

Console.WriteLine();

// Sort array in descending order

Array.Reverse(numbers);

Console.WriteLine("Array in descending order:");

foreach (int number in numbers)

Console.Write(number + " ");

Console.WriteLine();

6.

using System;

class ArgumentPassing

static void Main()

int val1 = 10;

int val2 = 20;

Console.WriteLine("Before call-by-value method call, val1: " + val1);

CallByValue(val1);

Console.WriteLine("After call-by-value method call, val1: " + val1);

Console.WriteLine("Before call-by-reference method call, val2: " + val2);

CallByReference(ref val2);
Console.WriteLine("After call-by-reference method call, val2: " + val2);

static void CallByValue(int value)

value = value * 2;

Console.WriteLine("Inside call-by-value method, value: " + value);

static void CallByReference(ref int value)

value = value * 2;

Console.WriteLine("Inside call-by-reference method, value: " + value);

Before call-by-value method call, val1: 10

Inside call-by-value method, value: 20

After call-by-value method call, val1: 10

Before call-by-reference method call, val2: 20

Inside call-by-reference method, value: 40

After call-by-reference method call, val2: 40

7.

using System;

class BreakContinueExample

static void Main()

Console.WriteLine("Using 'continue' to skip even numbers:");


for (int i = 1; i <= 10; i++)

if (i % 2 == 0)

continue; // Skip the current iteration if the number is even

Console.Write(i + " ");

Console.WriteLine("\n\nUsing 'break' to exit loop when a number greater than 5 is found:");

for (int i = 1; i <= 10; i++)

if (i > 5)

break; // Exit the loop if the number is greater than 5

Console.Write(i + " ");

Using 'continue' to skip even numbers:

13579

Using 'break' to exit loop when a number greater than 5 is found:

12345

8.

using System;

class FactorialTable

static void Main()


{

Console.Write("Enter the value of m: ");

int m = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Factorial Table:");

for (int i = 1; i <= m; i++)

Console.WriteLine("{0}! = {1}", i, Factorial(i));

static long Factorial(int n)

long fact = 1;

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

fact *= i;

return fact;

Enter the value of m: 6

Factorial Table:

1! = 1

2! = 2

3! = 6

4! = 24

5! = 120

6! = 720
9.

using System;

class DiamondPattern

static void Main()

int n = 4; // Number of rows for the top half (excluding the middle row)

// Top half of the diamond

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

for (int j = 1; j <= i; j++)

Console.Write("*");

Console.WriteLine();

// Bottom half of the diamond

for (int i = n; i >= 1; i--)

for (int j = 1; j <= i; j++)

Console.Write("*");

Console.WriteLine();

}
10.

using System;

class StandardDeviationCalculator

static void Main()

double[] values = { 10, 12, 23, 23, 16, 23, 21, 16 };

double mean = Mean(values);

double standardDeviation = StandardDeviation(values, mean);

Console.WriteLine("The mean is: " + mean);

Console.WriteLine("The standard deviation is: " + standardDeviation);

static double Mean(double[] values)

double sum = 0;

foreach (double value in values)

sum += value;

return sum / values.Length;

static double StandardDeviation(double[] values, double mean)

double sumOfSquares = 0;

foreach (double value in values)

{
sumOfSquares += Math.Pow(value - mean, 2);

return Math.Sqrt(sumOfSquares / values.Length);

The mean is: 18

The standard deviation is: 4.89897948556636

You might also like