Create a program to ask the user for a real number and display its square root. Errors must be trapped using "try..catch".
Solution:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Exercise7
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter an integer");
float result, num;
try
{
num = Convert.ToSingle(Console.ReadLine());
result = (float)Math.Sqrt(num);
Console.WriteLine("Result {0}",result);
Console.ReadLine();
}
catch(Exception)
{
Console.WriteLine("Cannot Calculate sqrt");
}
}
}
}
Problem 2:
Create a program that uses the conditional operator to give a boolean variable named "bothEven" the value "true" if two numbers entered by the user are the even, or "false" if any of them is odd.
Solution
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Exercise8
{
class Program
{
static void Main(string[] args)
{
int num1, num2;
bool botheven;
Console.WriteLine("Enter first number");
num1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter sec number");
num2 = Convert.ToInt32(Console.ReadLine());
botheven = (( num1 % 2 == 0) && (num2 %2 == 0)) ? true : false;
Console.WriteLine(botheven ? "both numbers are even" : "one of them is odd");
Console.ReadLine();
}
}
}
Problem 3:
Create a program to ask the user for 5 numbers, store them in an array and show them in reverse order.
Solution:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Exercise9
{
class Program
{
static void Main(string[] args)
{
int[] numbers = new int[5];
for(int i = 0;i<5;i++)
{
Console.WriteLine("numbers {0}", i + 1);
numbers[i] = Convert.ToInt32(Console.ReadLine());
}
for ( int i=4;i>=0;i--)
{
Console.Write(" {0}\t",numbers[i]);
}
Console.ReadLine();
}
}
}
Problem 4:
Write a C# program to ask the user for 10 integer numbers and display the even ones.
Solution:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Exercise9
{
class Program
{
static void Main(string[] args)
{
int[] numbers = new int[10];
for(int i = 0;i<10;i++)
{
Console.WriteLine("numbers {0}", i + 1);
numbers[i] = Convert.ToInt32(Console.ReadLine());
}
for (int i = 0; i < 10; i++)
if(numbers[i] %2 == 0)
Console.Write(" {0}\t", numbers[i]);
Console.ReadLine();
}
}
}
Problem 5:
Create a C# program to ask the user for 10 real numbers and display the average of the positive ones and the average of the negative ones.
Solution:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Exercise9
{
class Program
{
static void Main(string[] args)
{
float[] numbers = new float[10];
float totalpos = 0.0f;
float totalneg = 0.0f;
int countneg = 0;
int countpos = 0;
for(int i = 0;i<10;i++)
{
Console.WriteLine("numbers {0}", i + 1);
numbers[i] = Convert.ToSingle(Console.ReadLine());
}
for (int i = 0; i < 10; i++)
{
if (numbers[i] < 0)
{
totalneg += numbers[i];
countneg++;
}
if (numbers[i] > 0)
{
totalpos += numbers[i];
countpos++;
}
}
Console.WriteLine("Average of negative numbers : {0}",totalneg/countneg);
Console.WriteLine("Average of positive numbers : {0}",totalpos/countpos);
Console.ReadLine();
}
}
}
Problem 6:
Create a "struct" to store data of 2D points. The fields for each point will be:
x coordinate (short)
y coordinate (short)
r (red colour, byte)
g (green colour, byte)
b (blue colour, byte)
Write a program which creates two "points", asks the user for their data, and then displays their content.
Solution:
:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Exercise11
{
public class Program
{
struct point
{
public short x;
public short y;
public byte r;
public byte g;
public byte b;
}
static void Main(string[] args)
{
point p1, p2;
Console.Write("Enter X for first point");
p1.x = Convert.ToInt16(Console.ReadLine());
Console.Write("Enter Y for first point");
p1.y = Convert.ToInt16(Console.ReadLine());
Console.Write("Enter Red for first point");
p1.r = Convert.ToByte(Console.ReadLine());
Console.Write("Enter Green for first point");
p1.g = Convert.ToByte(Console.ReadLine());
Console.Write("Enter Blue for first point");
p1.b = Convert.ToByte(Console.ReadLine());
p2.x = Convert.ToInt16(Console.ReadLine());
Console.Write("Enter Y for sec point");
p2.y = Convert.ToInt16(Console.ReadLine());
Console.Write("Enter Red for sec point");
p2.r = Convert.ToByte(Console.ReadLine());
Console.Write("Enter Green for sec point");
p2.g = Convert.ToByte(Console.ReadLine());
Console.Write("Enter Blue for sec point");
p2.b = Convert.ToByte(Console.ReadLine());
Console.WriteLine("p1 is located in {0},{1} Colour {2}, {3}, {4}", p1.x, p1.y, p1.r, p1.g, p1.b);
Console.WriteLine("p2 is located in {0},{1} Colour {2}, {3}, {4}", p2.x, p2.y, p2.r, p2.g, p2.b);
Console.ReadLine();
}
}
}
Problem 7:
Expand the previous exercise (struct point), so that up to 1.000 points can be stored, using an "array of struct". Ask the user for data for the first two points and then display them.
Solution:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Exercise11
{
public class Program
{
struct point
{
public short x;
public short y;
public byte r;
public byte g;
public byte b;
}
static void Main(string[] args)
{
point[] p = new point[1000];
Console.Write("Enter X for first point");
p[0].x = Convert.ToInt16(Console.ReadLine());
Console.Write("Enter Y for first point");
p[0].y = Convert.ToInt16(Console.ReadLine());
Console.Write("Enter Red for first point");
p[0].r = Convert.ToByte(Console.ReadLine());
Console.Write("Enter Green for first point");
p[0].g = Convert.ToByte(Console.ReadLine());
Console.Write("Enter Blue for first point");
p[0].b = Convert.ToByte(Console.ReadLine());
p[1].x = Convert.ToInt16(Console.ReadLine());
Console.Write("Enter Y for sec point");
p[1].y = Convert.ToInt16(Console.ReadLine());
Console.Write("Enter Red for sec point");
p[1].r = Convert.ToByte(Console.ReadLine());
Console.Write("Enter Green for sec point");
p[1].g = Convert.ToByte(Console.ReadLine());
Console.Write("Enter Blue for sec point");
p[1].b = Convert.ToByte(Console.ReadLine());
Console.WriteLine("p1 is located in {0},{1} Colour {2}, {3}, {4}", p[0].x, p[0].y, p[0].r, p[0].g, p[0].b);
Console.WriteLine("p2 is located in {0},{1} Colour {2}, {3}, {4}", p[1].x, p[1].y, p[1].r, p[1].g, p[1].b);
Console.ReadLine();
}
}
}
Problem 8:
Expand the previous exercise (array of points), so that it displays a menu, in which the user can choose to:
- Add data for one point
- Display all the entered points
- Calculate (and display) the average values for x and y
- Exit the program
Solution:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Exercise12
{
class Program
{
struct points
{
public byte x;
public byte y;
}
static void Main(string[] args)
{
points[] p = new points[1000];
bool finish = false;
int countArray = 0;
int TotalX = 0;
int TotalY = 0;
do{
Console.WriteLine("-------------------------------------");
Console.WriteLine("1.Add data for one point");
Console.WriteLine("2.Display all the entered points");
Console.WriteLine("3.Calculate the avrage value for x and Y");
Console.WriteLine("4.Exit the program");
Console.WriteLine("-------------------------------------");
Console.WriteLine("Enter a number");
Console.WriteLine("-------------------------------------");
byte request = Convert.ToByte(Console.ReadLine());
switch(request)
{
case 1:
Console.WriteLine("Enter a number for point x");
p[countArray].x = Convert.ToByte(Console.ReadLine());
TotalX += p[countArray].x;
Console.WriteLine("Enter a number for point y");
p[countArray].y = Convert.ToByte(Console.ReadLine());
TotalX += p[countArray].y;
countArray++;
break;
case 2:
if(countArray > 0)
{
for(int i = 0;i<countArray;i++)
{
Console.WriteLine("Value : x{0} : {1}\n ",i + 1,p[i].x);
Console.WriteLine("Value : y{0} : {1}\n ",i + 1,p[i].y);
Console.WriteLine("-------------------------------------");
}
}
else
Console.WriteLine("No datas");
break;
case 3:
if(countArray > 0)
{
Console.WriteLine("-------------------------------------");
Console.WriteLine("Average x:{0}\n",TotalX/countArray);
Console.WriteLine("Average y:{0}\n",TotalY/countArray);
Console.WriteLine("-------------------------------------");
}
else
Console.WriteLine("No Data");
break;
case 4:
finish = true;
break;
default:
break;
}
}
while(!finish);
}
}
}
Problem 9:
Create a small database, which will be used to store data about books. For a certain book, we want to keep the following information:
Title
Author
The program must be able to store 1000 books, and the user will be allowed to:
Add data for one book
Display all the entered books (just title and author, in the same line)
Search for the book(s) with a certain title
Delete a book at a known position (for example, book number 6)
Exit the program
Hint: to delete an item in an array, you must move backwards every item which was placed after it, and the decrease the counter.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Exrcise13
{
class Program
{
struct books
{
public string title;
public string author;
}
static void Main(string[] args)
{
int capacity = 1000;
books [] books = new books[1000];
bool repeat = true;
string option ;
int count = 0;
string search;
bool found;
do{
Console.WriteLine("-------------------------------------");
Console.WriteLine("1.Add data for one book");
Console.WriteLine("2.Display all the entered books");
Console.WriteLine("3.Exact Search for the books");
Console.WriteLine("4.Partial Search for the books");
Console.WriteLine("5.Delete the books");
Console.WriteLine("0.Exit the program");
Console.WriteLine("-------------------------------------");
Console.WriteLine("Enter an option");
Console.WriteLine("-------------------------------------");
option = Console.ReadLine();
switch(option)
{
case "1":
if(count < capacity)
{
Console.WriteLine("Enter data for book {0}",count + 1);
Console.WriteLine("Enter the name of the book");
books[count].title = Console.ReadLine();
Console.WriteLine("Enter the name of the author");
books[count].author = Console.ReadLine();
count++;
Console.WriteLine("-------------------------------------");
}
else
Console.WriteLine("--database full--");
break;
case "2":
if(count == 0)
Console.WriteLine("No data to search");
else
for(int i = 0;i<count;i++)
{
Console.WriteLine("{0} :Title = {1}\n Author = {2} ",i + 1,books[i].title,books[i].author);
Console.WriteLine("-------------------------------------");
}
break;
case "3":
Console.WriteLine("Enter the name of the book");
search = Console.ReadLine();
found = false;
for(int i=0;i<count;i++)
if(books[i].title==search)
{
Console.WriteLine("book found:{0}",books[i].title);
found = true;
}
if(!found)
Console.WriteLine("Not found");
Console.WriteLine("-------------------------------------");
break;
case "4":
Console.WriteLine("Enter the search string");
search = Console.ReadLine();
found= false;
for(int i =0;i<count;i++)
if(books[i].title.ToUpper().Contains(search.ToUpper()) || books[i].author.ToUpper().Contains(search.ToUpper()))
{
Console.WriteLine("{0} found in {1}",search,books[i].title);
found = true;
}
if(!found)
Console.WriteLine("Not found");
Console.WriteLine("-------------------------------------");
break;
case "5":
if(count == 0)
Console.WriteLine("No data to delete");
else
{
Console.WriteLine("Enter the number of book to be deleted from 1 to {0}",count);
int postoDelete = Convert.ToInt32(Console.ReadLine()) - 1;
for(int i = postoDelete; i < count - 1;i++)
books[i] = books[i + 1];
count--;
}
break;
case "0":
repeat = false;
break;
default:
Console.WriteLine("Wrong option:pls re-enter");
break;
}
}
while(repeat);
}
}
}
Problem 10
Create a program that asks the user for a string and:
- Replace all lowercase A by uppercase A, except if they are preceded with a space
- Display the initials (first letter and those after a space)
- Display odd letters uppercase and even letter lowercase
The program must display all generated strings.
Solution
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Exercise15
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Tell a string:");
string Entry = Console.ReadLine();
string result;
result = Entry.Replace("a","A");
Console.WriteLine(result);
Console.WriteLine(Upfs(Entry));
Console.ReadLine();
}
public static string Upfs(string s)
{
return char.ToUpper(s[0]) + s.Substring(1);
}
}
}
Problem 11:
Create a struct to store two data for a person: name and date of birth. The date of birth must be another struct consisting on day, month and year.
Solution:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Exercise16
{
class Program
{
struct person
{
public string name;
public dateBirth Date;
}
struct dateBirth
{
public int day;
public int month;
public int year;
}
static void Main(string[] args)
{
int d = 0;
int m = 0; int y = 0;
int total = 1;
person[] p = new person[total];
for (int i = 0; i < total; i++)
{
Console.WriteLine("Enter name of the person");
string name = Console.ReadLine();
p[i].name = name;
Console.WriteLine("-------------------------");
Console.WriteLine("Enter day");
d = Convert.ToInt32(Console.ReadLine());
p[i].Date.day = d;
Console.WriteLine("-------------------------");
Console.WriteLine("Enter month");
m = Convert.ToInt32(Console.ReadLine());
p[i].Date.month = m;
Console.WriteLine("-------------------------");
Console.WriteLine("Enter year");
y = Convert.ToInt32(Console.ReadLine());
p[i].Date.year = y;
Console.WriteLine("-------------------------");
Console.ReadLine();
}
}
}
}
Problem 12:
Create a program to ask the user for 10 integer numbers , sort them and display them sorted.
Solution:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Exercise17
{
class Program
{
static void Main(string[] args)
{
int count = 9;
int[] data = new int[count];
int i, j, temp;
for (i = 0;i<count;i++)
{
Console.Write("Enter the numbers");
data[i] = Convert.ToInt32(Console.ReadLine());
}
for(i=0;i<count-1;i++)
{
for(j=i+1;j<count;j++)
{
if(data[i]>data[j])
{
temp = data[i];
data[i] = data[j];
data[j] = temp;
}
}
}
Console.WriteLine("Data is sorted");
foreach (int value in data)
Console.WriteLine("Values {0}", value);
Console.ReadLine();
}
}
}