Sunteți pe pagina 1din 17

1.

Write a C# Sharp program to print Hello and your name in a


separate line.
public class Exercise1
{
public static void Main( )
{
System.Console.WriteLine("Hello");
System.Console.WriteLine("Alexandra Abramov!");
}
}
2. Write a C# Sharp program to print the sum of two numbers.
public class Exercise2
{
public static void Main()
{
System.Console.WriteLine(15+17);
}
}

3. Write a C# Sharp program to swap two numbers.


using System;

public class Exercise5

public static void Main(string[] args)

int number1, number2, temp;

Console.Write("\nInput the First Number : ");

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

Console.Write("\nInput the Second Number : ");

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

temp = number1;
number1 = number2;

number2 = temp;

Console.Write("\nAfter Swapping : ");

Console.Write("\nFirst Number : "+number1);

Console.Write("\nSecond Number : "+number2);

Console.Read();

4. Write a C# Sharp program that takes four numbers as input to


calculate and print the average
using System;

public class Exercise9

public static void Main()

int number1,number2,number3,number4;

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

number1 = Convert.ToInt32(Console.ReadLine());

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

number2 = Convert.ToInt32(Console.ReadLine());

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

number3 = Convert.ToInt32(Console.ReadLine());

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

number4 = Convert.ToInt32(Console.ReadLine());
int result = (number1 + number2 + number3 + number4) / 4;

Console.WriteLine("The average of {0} , {1} , {2} , {3} is: {4} ",

number1, number2, number3, number4, result);

5. Write a C# program to convert from celsius degrees to Kelvin and


Fahrenheit
using System;

public class Exercise14

public static void Main( )

Console.Write("Enter the amount of celsius: ");

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

Console.WriteLine("Kelvin = {0}", celsius + 273);

Console.WriteLine("Fahrenheit = {0}", celsius * 18 / 10 + 32);

6. Write a C# Sharp program that takes three letters as input and


display them in reverse order.
using System;

public class Exercise1

public static void Main()

char letter,letter1,letter2;
Console.Write("Input letter: ");

letter = Convert.ToChar(Console.ReadLine());

Console.Write("Input letter: ");

letter1 = Convert.ToChar(Console.ReadLine());

Console.Write("Input letter: ");

letter2 = Convert.ToChar(Console.ReadLine());

Console.WriteLine("{0} {1} {2}",letter2,letter1,letter);

7. Write a C# Sharp program to check whether a given number is


even or odd.
using System;

public class Exercise2

public static void Main()

int num1, rem1;

Console.Write("\n\n");

Console.Write("Check whether a number is even or odd :\n");

Console.Write("---------------------------------------");

Console.Write("\n\n");

Console.Write("Input an integer : ");

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

rem1 = num1 % 2;
if (rem1 == 0)

Console.WriteLine("{0} is an even integer.\n",num1);

else

Console.WriteLine("{0} is an odd integer.\n",num1);

8. Write a C# Sharp program to find the sum of first 10 natural


numbers.
using System;

public class Exercise2

public static void Main()

int j, sum = 0;

Console.Write("\n\n");

Console.Write("Find the sum of first 10 natural numbers:\n");

Console.Write("----------------------------------------------");

Console.Write("\n\n");

Console.Write("The first 10 natural number is :\n");

for (j = 1; j <= 10; j++)

sum = sum + j;

Console.Write("{0} ",j);

Console.Write("\nThe Sum is : {0}\n", sum);

}
9. Write a program in C# Sharp to store elements in an array and print
it.
using System;

public class Exercise1

public static void Main()

int[] arr = new int[10];

int i;

Console.Write("\n\nRead and Print elements of an array:\n");

Console.Write("-----------------------------------------\n");

Console.Write("Input 10 elements in the array :\n");

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

Console.Write("element - {0} : ",i);

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

Console.Write("\nElements in array are: ");

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

Console.Write("{0} ", arr[i]);

Console.Write("\n");

}
10. Write a program in C# Sharp to find the sum of all elements
of the array.
using System;

public class Exercise3

public static void Main()

int[] a= new int[100];

int i, n, sum=0;

Console.Write("\n\nFind sum of all elements of array:\n");

Console.Write("--------------------------------------\n");

Console.Write("Input the number of elements to be stored in the


array :");

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

Console.Write("Input {0} elements in the array :\n",n);

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

Console.Write("element - {0} : ",i);

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

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

sum += a[i];

}
Console.Write("Sum of all elements stored in the array is : {0}\n\n",
sum);

11. Write a program in C# Sharp to copy the elements one array


into another array.
using System;

public class Exercise4

public static void Main()

int[] arr1 = new int[100];

int[] arr2 = new int[100];

int i, n;

Console.Write("\n\nCopy the elements one array into another array


:\n");

Console.Write("----------------------------------------------------\n");

Console.Write("Input the number of elements to be stored in the


array :");

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

Console.Write("Input {0} elements in the array :\n",n);

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

Console.Write("element - {0} : ",i);


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

/* Copy elements of first array into second array.*/

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

arr2[i] = arr1[i];

/* Prints the elements of first array */

Console.Write("\nThe elements stored in the first array are :\n");

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

Console.Write("{0} ", arr1[i]);

/* Prints the elements copied into the second array. */

Console.Write("\n\nThe elements copied into the second array are :\n");

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

Console.Write("{0} ", arr2[i]);

Console.Write("\n\n");

12. Write a program in C# Sharp to input a string and print it


using System;

public class Exercise1

{
public static void Main()

string str;

Console.Write("\n\nAccept a string from keyboard :\n");

Console.Write("-----------------------------------\n");

Console.Write("Input the string : ");

str= Console.ReadLine();

Console.Write("The string you entered is : {0}\n", str);

13. Write a program in C# Sharp to find the length of a string


without using library function.
using System;

public class Exercise2

public static void Main()

string str; /* Declares a string of size 100 */

int l= 0;

Console.Write("\n\nFind the length of a string :\n");

Console.Write("---------------------------------\n");

Console.Write("Input the string : ");

str = Console.ReadLine();

foreach(char chr in str)

{
l += 1;

Console.Write("Length of the string is : {0}\n\n", l);

14. Write a program in C# Sharp to create a user define function


using System;

class funcexer1

public static void welcome()

Console.WriteLine("Welcome Friends!");

public static void HaveAnice()

Console.WriteLine("Have a nice day!");

public static void Main()

Console.Write("\n\nSee, how to create an user define function :\n");

Console.Write("------------------------------------------------\n");

welcome();

HaveAnice();

Console.Write("\n");
}

15. Write a program in C# Sharp to find the factorial of a given


number using recursion
using System;

class RecExercise9

static void Main(string[] args)

Console.WriteLine("\n\n Recursion : Find the factorial


of a given number :");

Console.WriteLine("-------------------------------------------------------");

Console.Write(" Input any positive number : ");

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

long fact = FactorialCalcu(n1);

Console.WriteLine(" The factorial of {0} is : {1} ", n1, fact);

Console.ReadKey();

private static long FactorialCalcu(int n1)

if (n1 == 0)

return 1;

return n1 * FactorialCalcu(n1-1);
}

16. Write a program in C# Sharp to find the Fibonacci numbers


for a n numbers of series using recursion.
using System;

class RecExercise10

public static int FindFibonacci(int n)

int p = 0;

int q = 1;

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

int temp = p;

p = q;

q = temp + q;

return p;

static void Main()

Console.WriteLine("\n\n Recursion : Find the Fibonacci numbers for a n


numbers of series :");

Console.WriteLine("-----------------------------------------------------------
------------");
Console.Write(" Input number of terms for the Fibonacci series
: ");

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

Console.Write("\n The Fibonacci series of {0} terms is : ",n1);

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

Console.Write("{0} ",FindFibonacci(i));

Console.ReadKey();

17. Write a C# Sharp program to extract the Date property and


display the DateTime value in the formatted output.
using System;

public class Example1

public static void Main()

DateTime dt1 = new DateTime(2016, 6, 8, 11, 49, 0);

Console.WriteLine("Complete date: "+dt1.ToString());

// Get date-only portion of date, without its time.

DateTime dateOnly = dt1.Date;

Console.WriteLine("Short Date: "+dateOnly.ToString("d"));

Console.WriteLine("Display date using 24-hour clock format:");

Console.WriteLine(dateOnly.ToString("g"));
Console.WriteLine(dateOnly.ToString("MM/dd/yyyy HH:mm"));

18. Write a program in C# Sharp to remove a file from the disk.


using System;

using System.IO;

using System.Text;

class filexercise1

public static void Main()

string fileName = @"mytest.txt";

Console.Write("\n\n Remove a file from the disk (at


first create the file ) :\n");

Console.Write("--------------------------------------------------------------\
n");

// Delete the file if it exists.

if (File.Exists(fileName))

File.Delete(fileName);

Console.WriteLine(" The file {0} deleted


successfully.\n\n",fileName);

else

Console.WriteLine(" File does not exist");


Console.ReadKey();

19. Write a program in C# Sharp to create a file with text and


read the file.
using System;

using System.IO;

using System.Text;

class filexercise3

public static void Main()

string fileName = @"mytest.txt";

try

// Delete the file if it exists.

if (File.Exists(fileName))

File.Delete(fileName);

Console.Write("\n\n Create a file with text and read


the file :\n");

Console.Write("-------------------------------------------------\n");

// Create the file.

using (StreamWriter fileStr = File.CreateText(fileName))


{

fileStr.WriteLine(" Hello and Welcome");

fileStr.WriteLine(" It is the first content");

fileStr.WriteLine(" of the text file mytest.txt");

using (StreamReader sr = File.OpenText(fileName))

string s = "";

Console.WriteLine(" Here is the content of the


file mytest.txt : ");

while ((s = sr.ReadLine()) != null)

Console.WriteLine(s);

Console.WriteLine("");

catch (Exception MyExcep)

Console.WriteLine(MyExcep.ToString());

20.
21.
22.

S-ar putea să vă placă și