0% found this document useful (0 votes)
46 views22 pages

Here Are The C#-WPS Office

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)
46 views22 pages

Here Are The C#-WPS Office

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/ 22

Here are the C# programs for the given tasks:

1. Receive a number and determine whether it is odd or even:

```csharp

using System;

namespace OddOrEven

class Program

static void Main(string[] args)

Console.Write("Enter a number: ");

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

if (num % 2 == 0)

Console.WriteLine(num + " is even");

else

Console.WriteLine(num + " is odd");

}
}

```

2. Obtain two numbers from the keyboard, and determine and display which (if either) is the larger of
the two numbers:

```csharp

using System;

namespace LargerNumber

class Program

static void Main(string[] args)

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

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

Console.Write("Enter second number: ");

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

if (num1 > num2)

Console.WriteLine("First number is larger");

else if (num2 > num1)

{
Console.WriteLine("Second number is larger");

else

Console.WriteLine("Both numbers are equal");

```

3. Receive 3 numbers and display them in ascending order from smallest to largest:

```csharp

using System;

namespace AscendingOrder

class Program

static void Main(string[] args)

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

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

Console.Write("Enter second number: ");


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

Console.Write("Enter third number: ");

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

int[] numbers = new int[] { num1, num2, num3 };

Array.Sort(numbers);

foreach (int num in numbers)

Console.WriteLine(num);

```

4. Add the numbers from 1 to 100 and display the sum:

```csharp

using System;

namespace SumOfNumbers

class Program
{

static void Main(string[] args)

int sum = 0;

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

sum += i;

Console.WriteLine("Sum of numbers from 1 to 100: " + sum);

```

5. Add the even numbers between 0 and any positive integer number given by the user:

```csharp

using System;

namespace SumOfEvenNumbers

class Program

static void Main(string[] args)

Console.Write("Enter a positive integer: ");


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

int sum = 0;

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

if (i % 2 == 0)

sum += i;

Console.WriteLine("Sum of even numbers from 0 to " + n + ": " + sum);

```

6. Find the average of two numbers given by the user:

```csharp

using System;

namespace AverageOfTwoNumbers

class Program

{
static void Main(string[] args)

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

double num1 = Convert.ToDouble(Console.ReadLine());

Console.Write("Enter second number: ");

double num2 = Convert.ToDouble(Console.ReadLine());

double average = (num1 + num2) / 2;

Console.WriteLine("Average of two numbers: " + average);

```

7. Find the average, maximum, minimum, and sum of three numbers given by the user:

```csharp

using System;

namespace AverageMinMaxSumOfThreeNumbers

class Program

static void Main(string[] args)

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

double num1 = Convert.ToDouble(Console.ReadLine());

Console.Write("Enter second number: ");

double num2 = Convert.ToDouble(Console.ReadLine());

Console.Write("Enter third number: ");

double num3 = Convert.ToDouble(Console.ReadLine());

double sum = num1 + num2 + num3;

double average = sum / 3;

double max = Math.Max(num1, Math.Max(num2, num3));

double min = Math.Min(num1, Math.Min(num2, num3));

Console.WriteLine("Sum of three numbers: " + sum);

Console.WriteLine("Average of three numbers: " + average);

Console.WriteLine("Maximum of three numbers: " + max);

Console.WriteLine("Minimum of three numbers: " + min);

```

8. Find the area of a circle where the radius is provided by the user:

```csharp
using System;

namespace AreaOfCircle

class Program

static void Main(string[] args)

Console.Write("Enter radius of circle: ");

double radius = Convert.ToDouble(Console.ReadLine());

double area = Math.PI * Math.Pow(radius, 2);

Console.WriteLine("Area of circle: " + area);

```

9. Swap the contents of two variables using a third variable:

```csharp

using System;

namespace SwapVariablesUsingThirdVariable

class Program
{

static void Main(string[] args)

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

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

Console.Write("Enter second number: ");

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

int temp = a;

a = b;

b = temp;

Console.WriteLine("After swapping, a is " + a);

Console.WriteLine("After swapping, b is " + b);

```

10. Swap the contents of two variables without using a third variable:

```csharp

using System;

namespace SwapVariablesWithoutThirdVariable
{

class Program

static void Main(string[] args)

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

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

Console.Write("Enter second number: ");

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

// a=a+b;

// b=a-b;

// a=a-b;

// OR

a=a+b-(b=a);

// OR

(a,b)=(b,a);

// OR
b=a+a-(a=b);

//OR

(a=a+(b=a),b=b-a);

//OR

var t=a;a=b;b=t;

//OR

var t=a+a-(b=a);a=t/2;b=t/2+0.5m;

//OR

var t=(a,b);a=t.b;b=t.a;

//OR

//OR
var t=(a,a+b);a=t.b;b=t.a;

a=b+(b=a);

//OR

var t=(a+a);a=b;b=t.a;

Console.WriteLine("After swapping, a is " + a);

Console.WriteLine("After swapping, b is " + b);

```

11. Read an integer value from the keyboard and display a message indicating if this number is odd or
even:

```csharp

using System;

namespace OddOrEvenNumber
{

class Program

static void Main(string[] args)

bool flag=true;

while(flag)

try

try

if(flag)

flag=false;

else

flag=true;
}

catch

throw new Exception();

try

try

flag=false;

catch

throw new Exception();

}
}

catch

throw new Exception();

try

try

flag=true;

catch

throw new Exception();

}
catch

throw new Exception();

Console.Write("Enter an integer value:");

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

if(n%2==0)

Console.WriteLine(n+" is even");

else

Console.WriteLine(n+" is odd");

}
}

12. Read 10 integers from the keyboard in the range 0 - 100, and count how many of them are larger
than 50:

```csharp

using System;

namespace CountNumbersGreaterThan50

class Program

static void Main(string[] args)

int count=0;

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

try

try

bool flag=true;
while(flag)

try

catch

catch

throw new Exception();


}

catch

throw new Exception();

try

bool flag=true;

while(flag)

try

{
flag=false;

catch

throw new Exception();

catch

throw new Exception();

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

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

if(n>50)

count++;

Console.WriteLine($"Number of integers larger than 50 is {count}");

13. Take an integer from the user and display the factorial of that number:

```csharp

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy