Part 2 - Basic C# Programming Problem and Solutions
Part 3 - Basic C# Programming Problem and Solutions
Today I am writing this article for 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 three articles for 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
{
class Problem1
{
static void Main(string[] args)
{
char a;
int b;
Console.WriteLine("\t\tName: Ehtesham Mehmood\n\t\tRoll No: 11014119131\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
{
class Problem2
{
static void 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
{
class Problem3
{
static void 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
{
class Problem4
{
static void 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 {
class Problem5 {
static void 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();
}
}
}










Irfan ShaikhPosted Jun 29, 2017, 6:11 AM
The Conditions return in program 6 are not proper. OR "||" should be used at the place of &&. Please check the conditions for that program once again. Thanks
Manav PandyaPosted Sep 17, 2016, 10:20 AM
Nice ...
neha KumariPosted Jun 21, 2016, 5:45 AM
Hi Ehtesham,
Lalit RaghavPosted May 17, 2016, 3:52 AM
Nice Article
Ehtesham MehmoodPosted Mar 16, 2016, 3:29 AM
What error you are facing ?
Vipin TyagiPosted Mar 9, 2016, 6:35 AM
error in problem 9
Vipin TyagiPosted Mar 9, 2016, 6:08 AM
error in problem 4