C# Variables & Arithmetic Practice Programs

This comprehensive guide presents seven essential C# programming tasks designed for beginners to master fundamental concepts including variables, arithmetic operations, user input handling, and formatted output. Each task comes with complete source code that demonstrates practical implementation of core programming principles. From creating personalized information displays to calculating mathematical expressions and physics equations, these exercises provide hands-on experience with C#'s data types, operators, and console I/O operations. The programs gradually progress from simple display operations to more complex calculations involving user input, making them ideal for students and aspiring developers building their C# foundation.
Task 1: Personal Information & Marksheet Display
This program demonstrates the use of escape sequences to create a formatted output displaying personal information and a mark sheet.
Source Code:
// Display Personal Information with Formatted Output Console.WriteLine("______PERSONAL INSFORMATION______\n|First Name: Shoaib \t\t|\n|Last Name: Akhter \t\t|\n|Father Name: Ali \t\t|\n|Age: 19\t\t\t|\n|Email Id: [email protected]\t|\n|Phone Number: +9254635181\t|\n|Education: Intermediate\t|\n---------------------------------\n\n\n"); // Display Marksheet with Formatted Output Console.WriteLine("__________My Marksheet___________\n|\t\t\t\t|\n|Subject\tTotal\tObtained|\n|English\t100\t72\t|\n|Urdu\t\t100\t80\t|\n|Pst\t\t100\t75\t|\n|Islamyat\t100\t82\t|\n|Maths\t\t100\t86\t|\n|Computer\t100\t90\t|\n|Physics\t100\t88\t|\n|Chemistry\t100\t79\t|\n|\t\t\t\t|\n|%Percentage Obtained\t81.5%\t|");
Task 2: C# Arithmetic Expressions
This program calculates and displays the results of six different arithmetic expressions using floating-point numbers.
Source Code:
float eq1, eq2, eq3, eq4, eq5, eq6; // Equation 1: 3.0 * 5.0 Console.WriteLine("Equation 1 is : 3.0*5.0 "); eq1 = Convert.ToSingle(3.0F * 5.0F); Console.WriteLine("{0:0.00}", eq1); // Equation 2: 7.1 * 8.3 - 2.2 Console.WriteLine("Equation 2 is : 7.1*8.3-2.2 "); eq2 = Convert.ToSingle(7.1F * 8.3F - 2.2F); Console.WriteLine("{0:0.00}", eq2); // Equation 3: 3.2 / (6.1 * 5) Console.WriteLine("Equation 3 is : 3.2/(6.1*5) "); eq3 = Convert.ToSingle(3.2F / (6.1F * 5.0F)); Console.WriteLine("{0:0.00}", eq3); // Equation 4: 15 / 4 Console.WriteLine("Equation 4 is : 15/4 "); eq4 = Convert.ToSingle(15.0F / 4.0F); Console.WriteLine("{0:0.00}", eq4); // Equation 5: 15 % 4 Console.WriteLine("Equation 5 is : 15%4 "); eq5 = Convert.ToSingle(15.0F % 4.0F); Console.WriteLine("{0:0.00}", eq5); // Equation 6: 5 * 3 - (6 * 4) Console.WriteLine("Equation 6 is : 5*3-(6*4) "); eq6 = Convert.ToSingle(5.0F * 3.0F - (6.0F * 4.0F)); Console.WriteLine("{0:0.00}", eq6);
Task 3: Temperature Conversion (Fahrenheit to Celsius)
This program takes a temperature in Fahrenheit from the user and converts it to Celsius using integer arithmetic.
Source Code:
double fahrenheit, celsius; Console.WriteLine("Enter Temperature in Fahrenheit : "); fahrenheit = Convert.ToSingle(Console.ReadLine()); celsius = Convert.ToSingle((fahrenheit - 32) * 5 / 9); Console.WriteLine("Temperature in Celsius = {0:0.00}", celsius);
Task 4: Area of Circle Calculation
This program calculates the area of a circle using the formula 2πR² after taking the radius as user input.
Source Code:
double area, radius; Console.WriteLine("Enter Radius of a Circle : "); radius = Convert.ToSingle(Console.ReadLine()); Console.WriteLine("Formula To Calculate Area of Circle = 2πR² "); area = 2 * Math.PI * Math.Pow(radius, 2); Console.WriteLine("The Area of Circle : {0:0.00}", area);
Task 5: Complex Arithmetic Expression Evaluation
This program evaluates the expression (((a+b)(cd*e))-e)/f by taking input values from the user.
Source Code:
double a, b, c, d, e, f, equation; Console.WriteLine("EQUATION IS = (((a + b) * (c * e * d)) - e) / f"); Console.WriteLine("Enter the Value of a : "); a = Convert.ToSingle(Console.ReadLine()); Console.WriteLine("Enter the Value of b : "); b = Convert.ToSingle(Console.ReadLine()); Console.WriteLine("Enter the Value of c : "); c = Convert.ToSingle(Console.ReadLine()); Console.WriteLine("Enter the Value of d : "); d = Convert.ToSingle(Console.ReadLine()); Console.WriteLine("Enter the Value of e : "); e = Convert.ToSingle(Console.ReadLine()); Console.WriteLine("Enter the Value of f : "); f = Convert.ToSingle(Console.ReadLine()); equation = Convert.ToSingle((((a + b) * (c * e * d)) - e) / f); Console.WriteLine("Solution of Equation : {0:0.00}", equation);
Task 6: First Equation of Motion
This program implements the first equation of motion (Vf = Vi + at) by taking initial velocity, acceleration, and time as input from the user.
Source Code:
double vf, vi, a, t; Console.WriteLine("FIRST EQUATION OF MOTION IS : vf = vi + at"); Console.Write("Enter Initial Velocity : "); vi = Convert.ToSingle(Console.ReadLine()); Console.Write("Enter Acceleration : "); a = Convert.ToSingle(Console.ReadLine()); Console.Write("Enter Time(in Seconds) : "); t = Convert.ToSingle(Console.ReadLine()); vf = Convert.ToSingle(vi + (a * t)); Console.WriteLine("The Final Velocity is = {0:0.00}", vf);
Task 7: User Profile Information System
This program collects personal information from the user and displays it in a formatted profile layout.
Source Code:
string name, fathername, age, martialstatus, city, email, phone, profession, degree, university; Console.Write("Enter Your Name : "); name = Console.ReadLine(); Console.Write("Enter Your Father's Name : "); fathername = Console.ReadLine(); Console.Write("Enter Your Age : "); age = Console.ReadLine(); Console.Write("Enter Your Martial Status : "); martialstatus = Console.ReadLine(); Console.Write("Where Do You Live : "); city = Console.ReadLine(); Console.Write("Enter Your Email Address : "); email = Console.ReadLine(); Console.Write("Enter Your Phone Number : "); phone = Console.ReadLine(); Console.Write("Enter Your Profession : "); profession = Console.ReadLine(); Console.Write("Enter Your 4 Year Degree Program : "); degree = Console.ReadLine(); Console.Write("Enter Your University : "); university = Console.ReadLine(); Console.WriteLine("\nTHANKS FOR PROVIDING INFORMATION\n"); Console.WriteLine("\t\t********************* Your Profile *********************\n"); Console.Write("\t\tName : {0}" + "\n\t\tFather's Name : {1}" + "\n\t\tAge : {2}" + "\n\t\tMartial Status : {3}" + "\n\t\tCity : {4}" + "\n\t\tEmail : {5}" + "\n\t\tPhone Number : {6}" + "\n\t\tProfession : {7}" + "\n\t\tDegree : {8}" + "\n\t\tUniversity : {9}\n", name, fathername, age, martialstatus, city, email, phone, profession, degree, university); Console.WriteLine("\t\t_____________________________________________________________");
CodingsHub2
Contributor at Jorvea — Free Guest Blogging & Content Publishing Platform
Frequently Asked Questions
What are escape sequences in C# and why are they used?
Escape sequences in C# are special character combinations starting with a backslash () that represent non-printable characters or characters that have special meaning in strings. Common escape sequences include \n for newline, \t for tab, and \ for a literal backslash. They are used to format text output, create tables, and control how data is displayed in the console, making the output more readable and organized.
What is the difference between Convert.ToSingle() and explicit casting in C#?
Convert.ToSingle() is a method that safely converts various data types to a float (single-precision floating-point number) and handles conversion errors gracefully, while explicit casting (float)value is a direct type conversion that may cause overflow or precision issues. Convert.ToSingle() is generally preferred when converting user input from Console.ReadLine() as it provides better error handling and type safety.
Why do we use {0:0.00} formatting in Console.WriteLine() statements?
The {0:0.00} formatting syntax in C# specifies that the first argument should be displayed with exactly two decimal places. The "0" inside the format specifier acts as a digit placeholder, ensuring that even if the number has fewer decimal places, zeros are added to maintain consistent formatting. This is particularly useful for displaying monetary values, measurements, and calculation results with precise decimal representation.
What are the key differences between integer and floating-point arithmetic in C#?
Integer arithmetic in C# performs operations on whole numbers and truncates any decimal portion when dividing, while floating-point arithmetic (float, double) preserves decimal precision in calculations. For example, 15/4 yields 3 in integer arithmetic but 3.75 in floating-point arithmetic. Additionally, floating-point types can represent very large or small numbers but may have precision limitations, while integers are exact but limited in range. Understanding this difference is crucial when performing mathematical calculations in C# programs.


