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

Csharp For Final

The document contains examples of C# code to perform various tasks like reading and printing array elements, calculating tax, creating arrays, converting temperature units, and calculating factorials. Multiple code snippets are provided as examples to demonstrate how to accomplish these tasks using C#.

Uploaded by

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

Csharp For Final

The document contains examples of C# code to perform various tasks like reading and printing array elements, calculating tax, creating arrays, converting temperature units, and calculating factorials. Multiple code snippets are provided as examples to demonstrate how to accomplish these tasks using C#.

Uploaded by

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

C# for final

1. write a C# code fragment that read element in an array of n elements


and print them.

using System;

class Program
{
static void Main()
{
Console.Write("Enter the number of elements in the array: ");
int n = int.Parse(Console.ReadLine());

int[] arr = new int[n];

Console.WriteLine($"Enter {n} elements:");

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


{
Console.Write($"Element {i + 1}: ");
arr[i] = int.Parse(Console.ReadLine());
}

Console.WriteLine("Elements in the array:");

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


{
Console.WriteLine(arr[i]);
}
}
}

2. Sure, here's a complete C# program that calculates the pension, income


tax, and net salary for an employee according to the Ethiopian tax
proclamation:

```csharp
using System;

class Program
{
static void Main()
{
Console.WriteLine("Welcome to the Ethiopian Tax Calculator");

// Get employee salary from the user


Console.Write("Enter the employee's monthly salary: ");
double salary = double.Parse(Console.ReadLine());

// Calculate pension (10% of the salary)


double pension = 0.1 * salary;

// Calculate income tax


double tax = CalculateIncomeTax(salary);

// Calculate net salary (salary - pension - tax)


double netSalary = salary - pension - tax;

// Display the results


Console.WriteLine("\nTax Calculation Results");
Console.WriteLine("----------------------------");
Console.WriteLine($"Employee's Monthly Salary: {salary}");
Console.WriteLine($"Pension: {pension}");
Console.WriteLine($"Income Tax: {tax}");
Console.WriteLine($"Net Salary: {netSalary}");
}

// Function to calculate income tax based on Ethiopian tax rates


static double CalculateIncomeTax(double salary)
{
double[] taxRates = { 0, 0.1, 0.15, 0.2, 0.25, 0.3 }; // Tax rates
based on salary levels
double[] salaryLevels = { 3000, 4000, 5000, 6000, 7000 }; //
Salary levels

double tax = 0;
double remainingSalary = salary;
int i = 0;

while (remainingSalary > 0 && i < taxRates.Length)


{
double currentLevelSalary = Math.Min(remainingSalary,
salaryLevels[i]);
tax += currentLevelSalary * taxRates[i];
remainingSalary -= currentLevelSalary;
i++;
}

return tax;
}
}
```

In this program, we prompt the user to enter the employee's monthly


salary. The program then calculates
the pension (10% of the salary), income tax based on Ethiopian tax rates,
and the net salary (salary - pension - tax).
Finally, it displays the results on the screen. The income tax calculation
is based on a progressive tax system
with different tax rates for different salary levels, as defined in the
`CalculateIncomeTax` function.

using System;

class Program
{
static void Main()
{
Console.WriteLine("Welcome to the Ethiopian Tax Calculator");

// Get employee salary from the user


Console.Write("Enter the employee's monthly salary: ");
double salary = double.Parse(Console.ReadLine());

// Calculate pension (10% of the salary)


double pension = 0.1 * salary;

// Calculate income tax


double tax = CalculateIncomeTax(salary);

// Calculate net salary (salary - pension - tax)


double netSalary = salary - pension - tax;

// Display the results


Console.WriteLine("\nTax Calculation Results");
Console.WriteLine("----------------------------");
Console.WriteLine($"Employee's Monthly Salary: {salary}");
Console.WriteLine($"Pension: {pension}");
Console.WriteLine($"Income Tax: {tax}");
Console.WriteLine($"Net Salary: {netSalary}");
}

// Function to calculate income tax based on Ethiopian tax rates


static double CalculateIncomeTax(double salary)
{
double[] taxRates = { 0, 0.1, 0.15, 0.2, 0.25, 0.3 }; // Tax rates
based on salary levels
double[] salaryLevels = { 3000, 4000, 5000, 6000, 7000 }; //
Salary levels

double tax = 0;
double remainingSalary = salary;
int i = 0;

while (remainingSalary > 0 && i < taxRates.Length)


{
double currentLevelSalary = Math.Min(remainingSalary,
salaryLevels[i]);
tax += currentLevelSalary * taxRates[i];
remainingSalary -= currentLevelSalary;
i++;
}

return tax;
}
}

3. write a C# program which create an array of integers of size 5,


initializ them and display the elemnts in a console?

using System;
class Program
{
static void Main()
{
// Create an array of integers of size 5
int[] array = new int[5];

// Initialize the elements of the array


array[0] = 10;
array[1] = 20;
array[2] = 30;
array[3] = 40;
array[4] = 50;

// Display the elements in the console


Console.WriteLine("Elements of the array:");
for (int i = 0; i < array.Length; i++)
{
Console.WriteLine($"Element {i + 1}: {array[i]}");
}
}
}

4. every window service has different start types what are those service
types

A. Automatic (Delayed Start): The service starts automatically when the


system boots up. However, there might be a delay in starting this
service to allow the system to start faster.

B. Automatic: The service starts automatically when the system boots up.
It starts immediately upon system boot without any delay.

C. Manual: The service needs to be started manually by the user or other


services. It does not start automatically upon system boot.

D. Disabled: The service is disabled, and it won't start automatically,


even if the system is booted. It can only be started manually if
needed.

5.using C#, Create an object of the car class, with the name myObj. then
we print the value of the fields color and maxSpeed?

using System;

class Car
{
public string color;
public int maxSpeed;
}
class Program
{
static void Main()
{
// Create an object of the Car class with the name myObj
Car myObj = new Car();

// Set the values of the color and maxSpeed fields


myObj.color = "Red";
myObj.maxSpeed = 200;

// Print the values of the color and maxSpeed fields


Console.WriteLine($"Car color: {myObj.color}");
Console.WriteLine($"Car max speed: {myObj.maxSpeed} km/h");
}
}

6.write C# program that accept temperature in Celsius from keyboard and


convert in to Fahrenheit

using System;

class Program
{
static void Main()
{
Console.Write("Enter the temperature in Celsius: ");
double celsius = double.Parse(Console.ReadLine());

double fahrenheit = CelsiusToFahrenheit(celsius);

Console.WriteLine($"Temperature in Fahrenheit: {fahrenheit} °F");


}

static double CelsiusToFahrenheit(double celsius)


{
// Formula to convert Celsius to Fahrenheit: F = (C * 9/5) + 32
return (celsius * 9 / 5) + 32;
}
}

7.the output of: int x = 5, z=1; int y = 2; z+=y++;


console.writeLine(x%z); is

Ans= 1

8.write C# code to create integer of two dimensional n and m arrays , to


read from keyboard and print data to the screen?

using System;

class Program
{
static void Main()
{
Console.Write("Enter the number of rows (n): ");
int n = int.Parse(Console.ReadLine());

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


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

// Create a 2D array with dimensions n x m


int[,] array = new int[n, m];

// Read input for the array elements from the keyboard


for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
Console.Write($"Enter the element at index [{i}, {j}]: ");
array[i, j] = int.Parse(Console.ReadLine());
}
}

// Print the data to the screen


Console.WriteLine("Array Data:");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
Console.Write($"{array[i, j]} ");
}
Console.WriteLine();
}
}
}

9.write a C# program that find the factorial of a given number?

using System;

class Program
{
static long Factorial(int n)
{
if (n == 0 || n == 1)
{
return 1;
}
else
{
return n * Factorial(n - 1);
}
}

static void Main()


{
Console.Write("Enter a number to find its factorial: ");
int number = int.Parse(Console.ReadLine());

if (number < 0)
{
Console.WriteLine("Factorial is not defined for negative
numbers.");
}
else
{
long result = Factorial(number);
Console.WriteLine($"Factorial of {number} is: {result}");
}
}
}

You might also like