0% found this document useful (0 votes)
22 views29 pages

C# Program Compilation

Uploaded by

Eman Seguido
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views29 pages

C# Program Compilation

Uploaded by

Eman Seguido
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 29

C# Program Compilation

A Compilation Project

Presented to the Faculty of the


College of Information Technology Education BSIT Program
Arellano University
Jose Abad Santos Campus, Pasay City

By:
John Rick Valle
Emmanuel Veloso

In partial fulfillment
of the Requirements for the Subject ITC111 Computer Programming 1

November 2024

0
TABLE OF CONTENTS

Program Number 1 Page 2-3

Program Number 2 Page 4-5

Program Number 3 Page 6-7

Program Number 4 Page 8-9

Program Number 5 Page 10-13

Program Number 6 Page 14-16

Program Number 7 Page 17-19

Program Number 8 Page 20-23

Program Number 9 Page 24

Program Number 10 Page 25-26

Program Number 11 Page 26-29

1
Program 1: Printing Char, String, Integer, Float, and Double.
Description of the program: Program will display “Char, String, Integer, Float, and Double.”

using System;

public class Program 1


{
public static void Main(string[] args)
{
Console.WriteLine("Activity 1: \n");
// Declaring an integer variable and assigning a value
int integerVariable = 10;

// Declaring a string variable and assigning a value


string nameAndCourse = "JOHN RICK VALLE & EMMANUEL VELOSO: BSIT - First
Year";

// Declaring a char variable and assigning a value


char grade = 'A';

// Declaring a double variable and assigning a value


double gradePointAverage = 10.0f;

// Declaring a long variable and assigning a value


long largeNumber = 100000000;

// Declaring a bool variable and assigning a value


bool isEnrolled = true;

// Outputting the values of the variables to the console


Console.WriteLine("This is an int: " + integerVariable);
Console.WriteLine("This is a string: " + nameAndCourse);
Console.WriteLine("This is a char: " + grade);
Console.WriteLine("This is a double: " + gradePointAverage);
Console.WriteLine("This is a long: " + largeNumber);
Console.WriteLine("This is a bool: " + isEnrolled);
}
}

2
Flowchart.

3
Program 2: Using comments and Naming Conventions.
Description of the program: Program will display “Char, String, Integer, Float, and Double.”

using System;

public class Program 2


{
public static void Main(string[] args)
{
Console.WriteLine("Activity 2: \n");
// Declaring an integer variable and assigning a value
int integerVariable = 10;

// Declaring a string variable and assigning a value


string nameAndCourse = "EMMANUEL VELOSO& JOHN RICK VALLE: BSIT - First
Year";

// Declaring a char variable and assigning a value


char grade = 'A';

// Declaring a double variable and assigning a value


double gradePointAverage = 10.0f;

// Declaring a long variable and assigning a value


long largeNumber = 100000000;

// Declaring a bool variable and assigning a value


bool isEnrolled = true;

// Outputting the values of the variables to the console


Console.WriteLine("This is an int: " + integerVariable);
Console.WriteLine("This is a string: " + nameAndCourse);
Console.WriteLine("This is a char: " + grade);
Console.WriteLine("This is a double: " + gradePointAverage);
Console.WriteLine("This is a long: " + largeNumber);
Console.WriteLine("This is a bool: " + isEnrolled);
}
}

4
Flowchart.

5
Program 3: Arithmetics in C#
Description of the program: Program displays basic arithmetic operations on a set of predefined
numbers.

using System;

public class Program 3


{
public static void Main(string[] args)
{
Console.WriteLine("Activity 3: ");

// Calculate the sum of 10 and 15


int sum = 20+ 15;

// Calculate the difference between 12 and 9


int difference = 15 - 9;

// Calculate the product of 11 and 7


int product = 11 * 7;

// Calculate the quotient of 32 divided by 5


float quotient = 32f / 5f;

// Calculate the remainder of 32 divided by 5


float remainder = 32f % 5f;

// Calculate 10 raised to the power of 5


double powerResult = Math.Pow(10, 5);

// Output the results to the console


Console.WriteLine("Sum of 20 & 15: " + sum);
Console.WriteLine("Difference of 15 & 9: " + difference);
Console.WriteLine("Product of 11 & 7: " + product);
Console.WriteLine("Quotient and Remainder of 32 & 5: " + quotient + " r " + remainder);
Console.WriteLine("Result of 10 raised to 5: " + powerResult);
}
}

6
Flowchart.

7
Program 4: Escape Sequence
Description: Program displays three different sentences using Console.WriteLine without using
variables

using System;

public class Program 4


{
public static void Main(string[] args)
{
Console.WriteLine("Activity 4: ");
// Uses Escape Sequence: "\" - Used to include double quotes within the string.
Console.WriteLine("Strings should be enclosed in quotation marks like so \"this is a string\"");
// Uses Escape Sequence: "\\" - Used to include a backslash in the path. Inside of its C:\
Windows\System32
Console.WriteLine("You should not delete C:\\Windows\\System32");
// Uses Escape Sequence: \t - Tab, \n - End
Console.WriteLine("Hello \t\n\t World!");
}
}

8
Flowchart.

9
Program 5: Comparing Numbers.
Description: Program displays the biggest and smallest using the if-else if- else, comparison,
and logical operators.

using System;

public class Program 5


{
public static void Main(string[] args)
{
Console.WriteLine("Activity 5: ");

// 1st set of numbers


int firstSetFirstNumber = 20;
int firstSetSecondNumber = 111;
int firstSetThirdNumber = 11;

// 2nd set of numbers


int secondSetFirstNumber = 25;
int secondSetSecondNumber = 2;
int secondSetThirdNumber = 12;

// 3rd set of numbers


int thirdSetFirstNumber = 8;
int thirdSetSecondNumber = 12;
int thirdSetThirdNumber = 26;

// Method to find the biggest and smallest number in a set


void FindBiggestAndSmallest(int num1, int num2, int num3, string setName)
{
int biggestNumber;
int smallestNumber;

// Finding the biggest number


if (num1 >= num2 && num1 >= num3)
{
biggestNumber = num1;
}
else if (num2 >= num1 && num2 >= num3)
{

10
biggestNumber = num2;
}
else
{
biggestNumber = num3;
}

// Finding the smallest number


if (num1 <= num2 && num1 <= num3)
{
smallestNumber = num1;
}
else if (num2 <= num1 && num2 <= num3)
{
smallestNumber = num2;
}
else
{
smallestNumber = num3;
}

// Output for the set


Console.WriteLine(setName + ": " + num1 + ", " + num2 + ", " + num3);
Console.WriteLine("The Biggest Number: " + biggestNumber + "\nThe Smallest Number: " +
smallestNumber);
Console.WriteLine();
}

// Call the method for each set


FindBiggestAndSmallest(firstSetFirstNumber, firstSetSecondNumber, firstSetThirdNumber,
"1st set");
FindBiggestAndSmallest(secondSetFirstNumber, secondSetSecondNumber,
secondSetThirdNumber, "2nd set");
FindBiggestAndSmallest(thirdSetFirstNumber, thirdSetSecondNumber, thirdSetThirdNumber,
"3rd set");
}
}

11
Flowchart.

12
13
Program 6: Age group
Description: Program displays the age group based on what age(number/integer) the user has
inputted.

using System;

public class Program 6


{
public static void Main(string[] args)
{

Console.WriteLine("Activity 6: "); // Output the activity title


bool isContinueProgram = true; // Initialize the loop control variable

do
{
try
{
Console.Write("Enter Age: "); // Prompt the user for input
string userInput = Console.ReadLine(); // Read the input from the user

// Check if input is null


if (userInput == null)
{
Console.WriteLine("Invalid input. Please enter a valid integer.");
continue;
}

// Try to parse the input to an integer


if (!int.TryParse(userInput, out int age))
{
Console.WriteLine("Invalid input. Please enter a valid integer.");
continue;
}

else
{
// Determine the age group based on the entered age
switch (age)
{
case int userAge when (userAge >= 0 && userAge <= 6):
Console.WriteLine("Age group: Toddler");
break;

14
case int userAge when (userAge >= 7 && userAge <= 12):
Console.WriteLine("Age group: Kid");
break;
case int userAge when (userAge >= 13 && userAge <= 19):
Console.WriteLine("Age group: Teen");
break;
case int userAge when (userAge >= 20 && userAge <= 30):
Console.WriteLine("Age group: Young Adult");
break;
case int userAge when (userAge >= 31 && userAge <= 59):
Console.WriteLine("Age group: Adult");
break;
case int userAge when (userAge >= 60):
Console.WriteLine("Age group: Senior");
break;
default:
Console.WriteLine("Invalid input.");
Break;
}
}

Flowchart.

15
Program 7: Printing an Array

16
Description: Program displays the student information inside a 2D array.

using System;

public class Program 7


{
public static void Main(string[] args)
{

Console.WriteLine("Activity 7: \n");

// Array of labels for the columns


string[] labels = { "Name", "Course", "Year" };

// 2D array of student details


string[,] students = {
{"John", "BSIT", "3rd"},
{"Mark", "BSCS", "1st"},
{"Jose", "BSIT", "2nd"},
{"Manuel", "BSCS", "4th"}
};

// Loop through each label in the labels array and print it


foreach (var label in labels)
{
Console.Write(label + "\t");
}
Console.WriteLine(); // New line after the labels
Console.WriteLine(); // Another new line for spacing

// Loop through the students array


for (int i = 0; i < students.GetLength(0); i++)
{
for (int j = 0; j < students.GetLength(1); j++)
{
// Print each element of the students array
Console.Write(students[i, j] + "\t");
}
Console.WriteLine(); // New line after each student
}
}

17
Flowchart.

18
Program 8: Math Methods.
Description: Program displays four Methods for Math (Addition, Subtraction, Multiplication, &
Division).

using System;

public class Program 8


{
public static void Main(string[] args)
{

Console.WriteLine("Activity 8:");

while (true)
{
int firstNumber = 0;
int secondNumber = 0;

Console.Write("\nInput 1st number: ");


string firstInput = Console.ReadLine();
Console.Write("Input 2nd number: ");
string secondInput = Console.ReadLine();

if (!int.TryParse(firstInput, out firstNumber))


{
Console.Clear(); // Clear the console for invalid input
Console.WriteLine("Invalid Number. Please Try Again.");
continue;
}

if (!int.TryParse(secondInput, out secondNumber))


{
Console.Clear(); // Clear the console for invalid input
Console.WriteLine("Invalid Number. Please Try Again.");
continue;
}

Console.Clear(); // Clear the console before displaying everything again


Console.WriteLine("Activity 8:");
Console.WriteLine("\nInput 1st number: " + firstNumber);
Console.WriteLine("Input 2nd number: " + secondNumber);

19
while (true)
{
Console.WriteLine("\nOptions:");
Console.WriteLine("Add - Addition");
Console.WriteLine("Sub - Subtraction");
Console.WriteLine("Mul - Multiplication");
Console.WriteLine("Div - Division");
Console.Write("\nPlease select an Option: ");
string operationChoice = Console.ReadLine().ToLower();

if (operationChoice != "add" && operationChoice != "sub" && operationChoice != "mul" &&


operationChoice != "div")
{
Console.Clear(); // Clear the console for invalid selection
Console.WriteLine("Invalid selection. Please try again.\n");
continue;
}

Console.Clear(); // Clear the console before displaying the result


Console.WriteLine("Activity 8:");
Console.WriteLine("\nInput 1st number: " + firstNumber);
Console.WriteLine("Input 2nd number: " + secondNumber);

Console.WriteLine("\nOptions:");
Console.WriteLine("Add - Addition");
Console.WriteLine("Sub - Subtraction");
Console.WriteLine("Mul - Multiplication");
Console.WriteLine("Div - Division");
}
}

20
Flowchart.

21
22
Prelim exam
public void PrelimExam()
{
Console.WriteLine("Prelim Exam:");
Console.WriteLine("JOHN RICK VALLE" + " " + 21 + " " + 'M');
Console.WriteLine("EMMANUEL VELOSO" + " " + 19 + " " + 'M');
}

23
Midterm exam

public void MidtermExam()


{
Console.WriteLine("Midterm Exam:");

// Define the valid range for military time (00:00 - 23:59)


// Declare two integer variables: militaryTime and hours
// militaryTime represents time in HHMM format
int militaryTime = 5959; // Example invalid time for demonstration
int hours = militaryTime / 100; // Extract hours from militaryTime
string period; // String to hold AM or PM period

// Display the hours and minutes


Console.WriteLine("Hours: " + hours);
Console.WriteLine("Minutes: " + (militaryTime % 100));

// Check if the time is valid


if ((militaryTime % 100 <= 59 && militaryTime <= 2359 && militaryTime % 100 > 0) && (hours
>= 0 && militaryTime % 100 < 60))
{
// Determine AM/PM period
if (hours == 0)
{
hours = 12; // Convert 00 hours to 12 AM
period = "AM";
}
else if (hours < 12)
{
period = "AM"; // Time is in the AM period
}
else if (hours == 12)
{
period = "PM"; // 12 hours is 12 PM
}
else // If hours is greater than 12, subtract 12 from hours and append “PM”
{
hours -= 12;
period = "PM";
}

24
// Display the standard time with appropriate format
if (militaryTime % 100 < 10)
{
// For minutes less than 10, add a leading zero
Console.WriteLine("It is " + hours + ":0" + (militaryTime % 100) + period + " standard
time");
}
else
{
// Display the standard time without a leading zero for minutes
Console.WriteLine("It is " + hours + ":" + (militaryTime % 100) + period + " standard
time");
}
}
else if (militaryTime == 0)
{
// Special case for midnight
Console.WriteLine("It is 12 AM standard time");
}
else if (militaryTime <= 59 && militaryTime > 0)
{
// Special case for times between 00:01 and 00:59
Console.WriteLine("It is 12:" + militaryTime + " AM standard time");
}
else
{
// If the input time is not valid, display an error message
Console.WriteLine("Invalid Time");
}
}

25
Semi-final exam
public void SemiFinalExam()
{
Console.WriteLine("Semi Final Exam:");
bool isContinueProgram = true; // Initialize the loop control variable

while (isContinueProgram)
{
// Display the menu options
Console.WriteLine("1. Rectangle");
Console.WriteLine("2. Triangle");
Console.WriteLine("3. Pyramid");
Console.WriteLine("4. Exit");
Console.Write("Choose your shape: ");

// Initialize selection variable and read user input


int userSelection = 0;
string userInput = Console.ReadLine();

// Validate if the input is an integer


if (!int.TryParse(userInput, out userSelection))
{
Console.WriteLine("Invalid Input. Please Try Again.");
continue; // Restart the loop if the input is invalid
}

// Execute the corresponding action based on user selection


switch (userSelection)
{
case 1:
// Handle rectangle drawing
Console.Write("Input your length: ");
int rectangleLength = int.Parse(Console.ReadLine());
Console.Write("Input your width: ");
int rectangleWidth = int.Parse(Console.ReadLine());

// Loop to print the rectangle shape


for (int i = 0; i < rectangleLength; i++)
{
for (int j = 0; j < rectangleWidth; j++)
{
Console.Write("* ");

26
}
Console.WriteLine(); // Move to the next line after each row
}
break;

case 2:
// Handle triangle drawing
Console.Write("Input your height: ");
int triangleHeight = int.Parse(Console.ReadLine());

// Loop to print the triangle shape


for (int i = 0; i <= triangleHeight; i++)
{
for (int j = i; j < triangleHeight; j++)
{
Console.Write("* ");
}
Console.WriteLine(); // Move to the next line after each row
}
break;

case 3:
// Handle pyramid drawing
Console.WriteLine("Enter the height of the pyramid:");
int pyramidHeight = int.Parse(Console.ReadLine());

// Loop to print the pyramid shape


for (int i = pyramidHeight; i >= 1; i--)
{
for (int j = pyramidHeight; j > i; j--)
{
Console.Write(" ");
}
for (int k = 1; k <= i; k++)
{
Console.Write("* ");
}
Console.WriteLine(); // Move to the next line after each row
}
break;

case 4:
// Exit the loop
isContinueProgram = false;

27
break;

default:
// Handle invalid selections
Console.WriteLine("Invalid Selection. Please Try Again.");
continue; // Restart the loop if the selection is invalid
}
}

28

You might also like