Basic C# Programming Problem and Solutions: Part 1

Today I am writing this article for the beginners who have just begun programming in the C# language. I have solved all the basic problems of C# programming and also I have included some sections of OOP in this article that I will show you in the next part of this article series.

I have created made three article of this article series, each part contains 14 problems. Anyone that reads this article series with full concentration and that practices on their own, I am sure he/she will become very familiar with the basics of C# programming. I have provided every problem in the form of a question and a solution. Let's start.

Problem 1

Write a program that converts 1 lower case letter ("a" - "z") to its corresponding upper case letter ("A" - "Z"). For example if the user enters "c" then the program will show "C" on the screen.

Hint: You need to check the ASCII value in your program.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace VP_Assignment1

{

classProblem1

    {

staticvoid Main(string[] args)

        {

char a;

int b;

Console.WriteLine("\t\tName: Ehtesham Mehmood\n\t\tRoll No: 11014119-131\n\t\tSection: AE\n \t\t        UOG\n");

Console.WriteLine("Enter A Letter Between a-z:");

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

            b = (int)a;

if (b >= 97 && b <= 122)

            {

                b = b - 32;

 

                a = (char)b;

Console.WriteLine("In Upper Case Letter:" + a);

            }

else

            {

 

Console.WriteLine("You Enter Wrong Letter, Please Enter Letter Between a-z....!");

            }

Console.ReadKey();

 

 

        }

    }

}

Output:



Problem 2

Write a program that takes three points (x1, y1), (x2, y2) and (x3, y3) from the user and the program will check wheteher or not all the three points fall on one straight line.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace Programm2

{

classProblem2

    {

staticvoid Main(string[] args)

        {

int x1, x2, x3, y1, y2, y3;

int slope1, slope2, slope3;

Console.WriteLine("\t\tName: Ehtesham Mehmood\n\t\tRoll No: 11014119-131\n\t\tSection: AE\n \t\t        UOG\n");

Console.WriteLine("Enter the values of x1 and y1 coordinates of first point");

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

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

Console.WriteLine("Enter the values of x2 and y2 coordinates of second point");

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

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

Console.WriteLine("Enter the values of x3 and y3 coordinates of third point");

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

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

            slope1 = y2 - y1 / x2 - x1;

            slope2 = y3 - y1 / x3 - x1;

            slope3 = y3 - y2 / x3 - x2;

if (slope1 == slope2 && slope1 == slope3)

            {

 

Console.WriteLine("\nAll points are fall on one straight line ");

 

            }

else

            {

 

Console.WriteLine("\nAll points are not fall on one straight line");

            }

Console.ReadKey();

        }

    }

}

Output:



Problem 3

Write a program that takes coordinates (x, y) of a center of a circle and its radius from the user, the program will determine whether a point lies inside the circle, on the circle or outside the circle.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace Problem3

{

classProblem3

    {

staticvoid Main(string[] args)

        {

Console.WriteLine("\t\tName: Ehtesham Mehmood\n\t\tRoll No: 11014119-131\n\t\tSection: AE\n \t\t        UOG\n");

int x, y,radius;

int radius_square, coordinates_calculation;

Console.WriteLine("Enter X coordinates of circle:");

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

Console.WriteLine("Enter Y coordinates of circle:");

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

Console.WriteLine("Enter Radius of circle:");

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

            radius_square = radius * radius;                      // Because equation of a circle is (x-a)^2+(y-b)^2=r^2

//And here at the origin (0,0) so we do here 

            coordinates_calculation = (x * x) + (y * y);

if (coordinates_calculation == radius_square)

            {

 

Console.WriteLine("Points Lies On The Circle");

            }

if (coordinates_calculation> radius_square )

            {

 

Console.WriteLine("Points Lies OutSide The Circle");

            }

if(coordinates_calculation <radius_square )

            {

 

Console.WriteLine("Points Lies InSide The Circle");

            }

Console.ReadKey();

 

        }

    }

}

Output:



Problem 4

Write a program that takes a character from the user and determines whether the character entered is a capital letter, a small case letter, a digit or a special symbol. The following table shows the range of ASCII values for various characters.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace Problem4

{

classProblem4

    {

staticvoid Main(string[] args)

        {

Console.WriteLine("\t\tName: Ehtesham Mehmood\n\t\tRoll No: 11014119-131\n\t\tSection: AE\n \t\t        UOG\n");

char a;

int b;

Console.WriteLine("Enter A Charater:");

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

            b = (int)a;

if (b >= 65 && b <= 90)

            {

 

Console.WriteLine("Entered Character Is Capital Letter:");

            }

if (b >= 97 && b <= 122)

            {

 

Console.WriteLine("Entered Character Is Small Letter:");

            }

if (b >= 48 && b <= 57)

            {

 

Console.WriteLine("Entered Character Is  Digit:");

            }

if (b == 0 && b <= 47 || b >= 58 && b <= 64 || b >= 91 && b <= 96 || b >= 123 && b <= 127)

            {

 

Console.WriteLine("Entered Character Is Special Symbols:");

            }

Console.ReadKey();

        }

    }

}

Output:



Problem 5

Write a program using a switch statement that takes one value from the user and asks about the type of conversion and then performs a conversion depending on the type of conversion. If user enters:

I -> convert from inches to centimeters.
G -> convert from gallons to liters.
M -> convert from mile to kilometer.
P -> convert from pound to kilogram.

If the user enters any other character then show a proper message.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace Problem5

{

classProblem5

    {

staticvoid Main(string[] args)

        {

Console.WriteLine("\t\tName: Ehtesham Mehmood\n\t\tRoll No: 11014119-131\n\t\tSection: AE\n \t\t        UOG\n");

 

int value;

char choice;

double centimeter, liters, kilometer, kilogram;

Console.WriteLine("Enter A Digit Value:");

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

Console.WriteLine("\n\n Press Any Of The Given Choices \n I -> convert from inches to centimeters.\n G -> convert from gallons to liters.\n M -> convert from mile to
kilometer.\n P -> convert from pound to kilogram."
);

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

 

switch (choice)

            {

 

case'I':

                    centimeter = value / 0.3937;                       //1 cm is equal is 0.3037 inch

Console.WriteLine("\n\nIn Centimeters:" + centimeter);

break;

case'i':

                    centimeter = value / 0.3937;

Console.WriteLine("\n\nIn Centimeters:" + centimeter);

break;

case'G':

                    liters = value * 3.78;                             // 1 gallon=3.78 litters

Console.WriteLine("\n\nIn Litters:" + liters);

break;

case'g':

                    liters = value * 3.78;                             // 1 gallon=3.78 litters

Console.WriteLine("\n\nIn Litters:" + liters);

break;

case'M':

                    kilometer = value * 1.60;

Console.WriteLine("\n\nIn kilometers:" + kilometer);

break;

case'm':

                    kilometer = value * 1.60;

Console.WriteLine("\n\nIn kilometers:" + kilometer);

break;

case'P':

                    kilogram = value * 0.453;

Console.WriteLine("\n\nIn KiloGrams:" + kilogram);

break;

case'p':

                    kilogram = value * 0.453;

Console.WriteLine("\n\nIn KiloGrams:" + kilogram);

break;

 

default:

Console.WriteLine("You Enter A Invalid Choice, Please Enter A Valid Choice...!");

break;

 

            }

Console.ReadKey();

        }

    }

}

Output:



Problem 6

In a company, worker efficiency is determined on the basis of the time required for a worker to complete a specific job. If the time taken by the worker is between 2 - 3 hours, then the worker is said to be highly efficient. If the time required by the worker is 3 - 4 hours, then the worker is ordered to increase their speed. If the time taken is 4 - 5 hours then the worker is given training to improve his speed and if the time taken by the worker is more than 5 hours then the worker must leave the company. If the time taken by the worker is input through the keyboard then find the efficiency of the worker.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace Problem6

{

classProblem6

    {

staticvoid Main(string[] args)

        {

Console.WriteLine("\t\tName: Ehtesham Mehmood\n\t\tRoll No: 11014119-131\n\t\tSection: AE\n \t\t        UOG\n");

int time;

Console.WriteLine("Enter Time Required For A Worker To Complete A Particular Job In Hours");

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

if (time >= 2 && time >=3)

            {

 

Console.WriteLine("Worker Efficiency Is Highly Efficient.");

            }

if (time >= 3 && time >=4)

            {

 

Console.WriteLine("Worker Should Improve His Speed.");

            }

if (time >= 4 && time >= 5)

            {

 

Console.WriteLine("Worker Is Given Training To Improve His Speed.");

 

            }

if (time > 5)

            {

 

Console.WriteLine("Worker Should Leave The Company.");

            }

Console.ReadKey();

        }

    }

}

Output:



Problem 7

Write a program using conditional operators to determine whether a year entered through the keyboard is a leap year or not.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace Problem7

{

classProblem7

    {

staticvoid Main(string[] args)

        {

Console.WriteLine("\t\tName: Ehtesham Mehmood\n\t\tRoll No: 11014119-131\n\t\tSection: AE\n \t\t        UOG\n");

int year;

string a;

Console.WriteLine("Enter A Year:");

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

           a= year % 4 == 0 ? "Year Is Leap""It Is Not A Leap Year:";

Console.WriteLine(a);

Console.ReadKey();

        }

    }

}

Output:



Problem 8

Write a program using a switch statement that takes one character value from the user and checks whether the entered value is an arithmetic operator, logical operator, conditional operator, relational operator or something else.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace Problem8

{

classProblem8

    {

staticvoid Main(string[] args)

        {

Console.WriteLine("\t\tName: Ehtesham Mehmood\n\t\tRoll No: 11014119-131\n\t\tSection: AE\n \t\t        UOG\n");

string value;

 

Console.WriteLine("Enter A Operator:");

            value = Console.ReadLine();

 

switch (value)

            {

 

case"+":

Console.WriteLine("This Is Arithmatic Operator:");

break;

case"-":

Console.WriteLine("This Is Arithmatic Operator:");

break;

case"*":

Console.WriteLine("This Is Arithmatic Operator:");

break;

case"%":

Console.WriteLine("This Is Arithmatic Operator:");

break;

case"/":

Console.WriteLine("This Is Arithmatic Operator:");

break;

case"&":

Console.WriteLine("This Is Arithmatic Operator:");

break;

case"--":

Console.WriteLine("This Is Arithmatic Operator:");

break;

case"++":

Console.WriteLine("This Is Arithmatic Operator:");

break;

case"|":

Console.WriteLine("This Is Logical Operator:");

break;

case"!":

Console.WriteLine("This Is Logical Operator:");

break;

case"^":

Console.WriteLine("This Is Logical Operator:");

break;

case"&&":

Console.WriteLine("This Is Logical Operator:");

break;

case"||":

Console.WriteLine("This Is Logical Operator:");

break;

 

case"==":

Console.WriteLine("This Is Relational Operator:");

break;

case"!=":

Console.WriteLine("This Is Logical Operator:");

break;

case"<":

Console.WriteLine("This Is Logical Operator:");

break;

case">":

Console.WriteLine("This Is Logical Operator:");

break;

case"<=":

Console.WriteLine("This Is Logical Operator:");

break;

case">=":

Console.WriteLine("This Is Logical Operator:");

break;

case"?":

Console.WriteLine("This Is Conditional Operator:");

break;

default:

Console.WriteLine("This Is Something Else Operator:");

break;

            }

Console.ReadKey();

        }

    }

}

Output:



Problem 9

Write a program that prints an identity matrix using a for loop, in other words takes a value n from the user and shows the identity table of size n * n.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace Problem9

{

classProblem9

    {

staticvoid Main(string[] args)

        {

Console.WriteLine("\t\tName: Ehtesham Mehmood\n\t\tRoll No: 11014119-131\n\t\tSection: AE\n \t\t        UOG\n");

int size;

Console.WriteLine("Enter The Size Of The Identity Matrix:");

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

Console.WriteLine("\n\n Identity Matrix\n\n");

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

            {

 

for (int j = 0; j < size; j++)

                {

 

if (i == j)

                    {

 

Console.Write(1);

                    }

else

                    {

Console.Write(0);

                    }

 

                }

Console.WriteLine("\n");

            }

Console.ReadKey();

        }

    }

}

Output:



Problem 10

Write a program using a for loop that prints the following series.

1 2 4 8 16 21 64 128 …nth iteration.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace Problem10

{

classProblem10

    {

staticvoid Main(string[] args)

        {

Console.WriteLine("\t\tName: Ehtesham Mehmood\n\t\tRoll No: 11014119-131\n\t\tSection: AE\n \t\t        UOG\n");

int value;

double result;

Console.WriteLine("Enter Some Value:");

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

Console.WriteLine("\nSeries Is:\n");

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

            {

                result = Math.Pow(2, i);

Console.Write(result);

Console.Write(" ");

            }

Console.ReadKey();

        }

    }

}

Output:



Problem 11

Write a program using a for loop that prints the following output (you need to find a pattern to print letters in this order):

A B D H P

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace Problem11

{

classProblem11

    {

staticvoid Main(string[] args)

        {

char ch;

int i = 1;

int j = 0;

 

while(i<=16)

            {

 

                j = i + 64;

             ch = (char)j;

Console.Write(ch);

Console.Write(" ");

             i = i * 2;

            }

Console.ReadKey();

        }

    }

}

Output:



Problem 12

Write a program using a loop that prints the following output.

1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6 6 6 6 6 6 . . . nth iteration.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace Problem12

{

classProblem12

    {

staticvoid Main(string[] args)

        {

int value;

Console.WriteLine("Enter A Value:");

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

Console.WriteLine("\n");

 

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

            {

 

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

                {

 

Console.Write(i);

                }

Console.Write(" ");

            }

Console.ReadKey();

        }

    }

}

Output:



Problem 13

Write a program to print all the ASCII values and their equivalent characters using a while loop. The ASCII values vary from 0 to 255.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace Problem13

{

classProblem13

    {

staticvoid Main(string[] args)

        {

 

char ch;

 

int i = 0;

while(i<=255)

            {

 

Console.Write(i);

Console.Write(" ");

            ch = (char)i;

Console.WriteLine(ch);

            i++;

            }

Console.ReadKey();

        }

    }

}

Output:



Problem 14

Write a program to print all the ASCII values and their equivalent characters using a do-while loop. The ASCII values vary from 10 to 255.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace Problem15

{

classProblem15

    {

staticvoid Main(string[] args)

        {

Console.WriteLine("\t\tName: Ehtesham Mehmood\n\t\tRoll No: 11014119-131\n\t\tSection: AE\n \t\t        UOG\n");

char ch;

 

int i = 10;

do

            {

 

Console.Write(i);

Console.Write(" ");

                ch = (char)i;

Console.WriteLine(ch);

                i++;

            } while (i <= 255);

Console.ReadKey();

        }

    }

}

Output: