You are on page 1of 41

MONGE, CEDRICK D.

BSIT 3-1 ADET

//GANYAN PO FORMAT NYA SIR PAG KINOPY PO YUNG CODE


GALING VISUAL STUDIO CODE DI PO SYA SCREENSHOT YUNG
OUTPUT LANG PO YUNG SCREENSHOT.

(Easy)

1. if statements

C# program that checks if an integer variable number is less than 5. If number is indeed less
than 5, the program should print "{X} is less than 5" (where X represents the value of number).

Code:

namespace ADETProg

{
class If
{
static void Main(string[] args)
{
int number = 2;
if (number < 5)
{
Console.WriteLine("{0} is less than 5", number);
}

Console.WriteLine("This statement is always executed.");


}
}
}

Output:
MONGE, CEDRICK D. BSIT 3-1 ADET

2. Else if

C# program that takes an integer variable time and based on its value, prints a corresponding
greeting message.

Code:

namespace ADETProg
{
class Else
{
static void Main(string[] args)
{
int time = 22;
if (time < 10)
{
Console.WriteLine("Good morning.");
}
else if (time < 20)
{
Console.WriteLine("Good day.");
}
else
{
Console.WriteLine("Good evening.");
}
}
}
}

Output:

3. Nested if
MONGE, CEDRICK D. BSIT 3-1 ADET

C# program that compares three integers (first, second, and third) and determines which one is
the largest. The program should output the largest number along with the message "{X} is the
largest" (where X represents the value of the largest number).

Code:

namespace ADETProg
{
class Nested
{
static void Main(string[] args)
{
int first = 7, second = -23, third = 13;
if (first > second)
{
if (first > third)
{
Console.WriteLine("{0} is the largest", first);
}
else
{
Console.WriteLine("{0} is the largest", third);
}
}
else
{
if (second > third)
{
Console.WriteLine("{0} is the largest", second);
}
else
{
Console.WriteLine("{0} is the largest", third);
}
}
}
}
}

Output:
MONGE, CEDRICK D. BSIT 3-1 ADET

4. While loop statement

C# program using the while loop that initializes an integer variable i to 1. The program should
then repeatedly print the message "C# While Loop: Iteration X" (where X represents the current
value of i) and increment i by 1 until i is greater than 5.

Code:

namespace ADETProg
{
class While
{
static void Main(string[] args)
{
int i=1;
while (i<=5)
{
Console.WriteLine("C# While Loop: Iteration {0}", i);
i++;
}
}
}
}

Output:

5. Do While
MONGE, CEDRICK D. BSIT 3-1 ADET

C# program using the do-while loop that initializes an integer variable i to 0. The program
should then repeatedly print the value of i and increment it by 1 until i is less than 5.

Code:

namespace ADETProg
{
class DoWhile
{
static void Main(string[] args)
{
int i = 0;

do
{
Console.WriteLine("i = {0}", i);
i++;

} while (i < 5);


}
}
}

Output:

6. For Loop statement

C# program that prints the numbers from 1 to 10 using a for loop.

Code:

namespace ADETProg
{
class ForLoop
{
static void Main(string[] args)
{
MONGE, CEDRICK D. BSIT 3-1 ADET

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


{
Console.WriteLine(i);
}
}
}
}

Output:

7. Foreach statement

C# program that uses a foreach statement to iterate over an array of strings and prints each
string on a separate line.

Code:

namespace ADETProg
{
class ForEach
{
static void Main(string[] args)
{
string[] names = { "Alice", "Bob", "Charlie", "David" };

foreach (string name in names)


{
Console.WriteLine(name);
}
}
}
MONGE, CEDRICK D. BSIT 3-1 ADET

Output:

8. 2 level Nested loops

C# program that uses a 2-level nested loop to display a multiplication table from 1 to 5.

Code:

namespace ADETProg
{
class TwoLevelLoop
{
static void Main(string[] args)
{
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= 5; j++)
{
int result = i * j;
Console.WriteLine("{0} x {1} = {2}", i, j, result);
}
}
}
}
}

Output:
MONGE, CEDRICK D. BSIT 3-1 ADET

9. 3 level combination of nested loops

C# program that uses a 3-level nested loop to display a pattern of asterisks. The pattern should
have three levels, where each level contains a certain number of asterisks based on its level
number.

Code:

namespace ADETProg
{
class ThreeLevelLoop
{
static void Main(string[] args)
{
for (int i = 1; i <= 4; i++)
{
for (int j = 1; j <= i; j++)
{
for (int k = 1; k <= j; k++)
{
Console.Write("*");
}
Console.WriteLine();
}
}
}
}
}

Output:
MONGE, CEDRICK D. BSIT 3-1 ADET

10. 4 level combination of nested loop

C# program that uses a 4-level combination of nested loops to display a pattern of numbers.
The pattern should have four levels, where each level contains a certain number of numbers
based on its level number.

Code:

namespace ADETProg
{
class FourLevelLoop
{
static void Main(string[] args)
{
for (int i = 1; i <= 4; i++)
{
for (int j = 1; j <= i; j++)
{
for (int k = 1; k <= j; k++)
{
for (int l = 1; l <= k; l++)
{
Console.Write(i + " ");
}
Console.WriteLine();
}
}
}
}
}
}

Output:
MONGE, CEDRICK D. BSIT 3-1 ADET

(Intermediate)

1. If statement

C# program that determines the category of a given exam score. The program should take an
integer input representing the exam score and output the corresponding category based on the
following criteria:

If the score is greater than or equal to 90, output "Excellent".

If the score is greater than or equal to 80, output "Good".

If the score is greater than or equal to 70, output "Average".

If the score is less than 70, output "Below Average".

Code:

namespace ADETProg
{
class IMIF
{
static void Main(string[] args)
{
MONGE, CEDRICK D. BSIT 3-1 ADET

Console.Write("Enter a Score: ");


int score = Convert.ToInt32(Console.ReadLine());

if (score >= 90)


{
Console.WriteLine("Excellent");
}
else if (score >= 80)
{
Console.WriteLine("Good");
}
else if (score >= 70)
{
Console.WriteLine("Average");
}
else
{
Console.WriteLine("Below Average");
}
}
}
}

Output:

2. Else If

C# Program that tells the month depending on the user inputted numbers

Code:

namespace ADETProg
{
class IMElseIf
{
static void Main(string[] args)
MONGE, CEDRICK D. BSIT 3-1 ADET

Console.Write("Enter a Month (1 to 12): ");


int month = Convert.ToInt32(Console.ReadLine());
if (month == 1) {
Console.WriteLine("January is the 1st Month");
}
else if (month == 2) {
Console.WriteLine("February is the 2nd Month");
}
else if (month == 3) {
Console.WriteLine("March is the 3rd Month");
}
else if (month == 4) {
Console.WriteLine("April is the 4th Month");
}
else if (month == 5) {
Console.WriteLine("May is the 5th Month");
}
else if (month == 6) {
Console.WriteLine("June is the 6th Month");
}
else if (month == 7) {
Console.WriteLine("July is the 7th Month");
}
else if (month == 8) {
Console.WriteLine("August is the 8th Month");
}
else if (month == 9) {
Console.WriteLine("September is the 9th Month");

}
else if (month == 10) {
Console.WriteLine("October is the 10th Month");
}
else if (month == 11) {
Console.WriteLine("November is the 11th Month");
}
else if (month == 12) {
Console.WriteLine("December is the 12th Month");
}
}
}
}
MONGE, CEDRICK D. BSIT 3-1 ADET

Output:

3. Nested If

C# program that uses a nested IF statement to determine the eligibility of a student for a
scholarship. The program should take two integer inputs representing the student's GPA and
SAT score. Based on the following criteria, the program should output the eligibility status of
the student:

• If the GPA is greater than or equal to 3.5 and the SAT score is greater than or equal to
1200, output "Eligible for Full Scholarship".
• If the GPA is greater than or equal to 3.0 and the SAT score is greater than or equal to
1000, output "Eligible for Partial Scholarship".
• If the GPA is less than 3.0 and the SAT score is less than 1000, output "Not Eligible for
Scholarship".

Code:

namespace ADETProg
{
class IMNestedIf
{
static void Main(string[] args)
{

Console.Write("Enter your GPA (1.00 to 5.00): ");


double gpa = Convert.ToDouble(Console.ReadLine());

Console.Write("Enter your SatScore : ");


int satScore = Convert.ToInt32(Console.ReadLine());

if (gpa >= 3.5)


{
if (satScore >= 1200)
{
Console.WriteLine("Eligible for Full Scholarship");
}
MONGE, CEDRICK D. BSIT 3-1 ADET

else
{
Console.WriteLine("Not Eligible for Scholarship");
}
}
else if (gpa >= 3.0)
{
if (satScore >= 1000)
{
Console.WriteLine("Eligible for Partial Scholarship");
}
else
{
Console.WriteLine("Not Eligible for Scholarship");
}
}
else
{
Console.WriteLine("Not Eligible for Scholarship");
}
}
}
}

Output:

4. While Loop Statement

C# program that uses a while loop to print a pattern of numbers. The program should take an
integer input representing the number of rows in the pattern. Each row should contain a
sequence of numbers starting from 1 and increasing by 2 for each subsequent number.

Code:

namespace ADETProg
{
MONGE, CEDRICK D. BSIT 3-1 ADET

class IMWhileLoop
{
static void Main(string[] args)
{
Console.Write("Enter Rows: ");
int rows = Convert.ToInt32(Console.ReadLine());

int rowCount = 1;
int num = 1;

while (rowCount <= rows)


{
int colCount = 1;

while (colCount <= rowCount)


{
Console.Write(num + " ");
num += 2;

colCount++;
}

Console.WriteLine();
rowCount++;
}
}
}
}

Output:

5. Do While
MONGE, CEDRICK D. BSIT 3-1 ADET

C# program that uses a do-while loop to calculate the sum of even numbers from 1 to a given
positive integer. The program should take an integer input representing the upper limit of the
range. The sum should include both the lower and upper limits if they are even numbers. The
program should output the calculated sum.

Code:

namespace ADETProg
{
class IMDoWhile
{
static void Main(string[] args)
{
Console.Write("Enter Upper Limit: ");
int upperLimit = Convert.ToInt32(Console.ReadLine());
int number = 1;
int sum = 0;

do
{
if (number % 2 == 0)
{
sum += number;
}

number++;
} while (number <= upperLimit);

Console.WriteLine("Sum of even numbers: " + sum);


}
}
}

Output:

6. For Loop Statement


MONGE, CEDRICK D. BSIT 3-1 ADET

C# program that uses a for loop to calculate the factorial of a given positive integer. The
program should take an integer input representing the number for which the factorial needs to
be calculated. The factorial of a number is the product of all positive integers from 1 to that
number. The program should output the calculated factorial.

Code:

namespace ADETProg
{
class IMForLoop
{
static void Main(string[] args)
{
Console.Write("Enter a Number: ");
int number = Convert.ToInt32(Console.ReadLine());
int factorial = 1;

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


{
factorial *= i;
}

Console.WriteLine("Factorial of " + number + ": " + factorial);


}
}
}

Output:

7. Foreach Statement

C# program that uses a foreach loop to calculate the sum of all elements in an array of integers.
The program should declare and initialize an integer array with a few numbers. It should then
use a foreach loop to iterate through each element in the array and add it to a running sum.
Finally, the program should output the calculated sum.
MONGE, CEDRICK D. BSIT 3-1 ADET

Code:

namespace ADETProg
{
class IMForeach
{
static void Main(string[] args)
{
int[] numbers = { 1, 2, 3, 4, 5 };
int sum = 0;

foreach (int number in numbers)


{
sum += number;
}

Console.WriteLine("Sum of the numbers: " + sum);


}
}
}

Output:

8. 2 level nested loop

C# program that uses two nested loops to print a pattern of asterisks. The program should take
an integer input representing the number of rows in the pattern. Each row should contain a
sequence of asterisks, with the number of asterisks increasing by 1 for each subsequent row.

Code:

namespace ADETProg
{
class IM2LevelNested
{
MONGE, CEDRICK D. BSIT 3-1 ADET

static void Main(string[] args)


{
Console.Write("Enter number of rows: ");
int rows = Convert.ToInt32(Console.ReadLine());

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


{
for (int j = 1; j <= i; j++)
{
Console.Write("*");
}

Console.WriteLine();
}
}
}
}

Output:

9. 3 level combination of nested loops

Write a C# program that uses a 3-level combination of nested while loops to generate a pattern
of characters. The pattern should have three levels, where each level contains a certain
combination of characters based on its level number.

Code:

namespace ADETProg
{
class IMThreeLevelLoop
{
static void Main(string[] args)
MONGE, CEDRICK D. BSIT 3-1 ADET

{
int level = 1;
int currentChar = 65;

while (level <= 3)


{
int charCount = 1;

while (charCount <= level)


{
// Your code here
char currentCharacter = (char)currentChar;
Console.Write(currentCharacter);

currentChar++;
charCount++;
}

Console.WriteLine();
level++;
}
}
}
}

Output:

10. 4 level combination of nested loop

C# program that generates a pattern of numbers. The pattern should have four levels, where
each level contains a certain combination of numbers based on its level number. For each level,
the program should print the combination of numbers in a specific format

Code:

namespace ADETProg
{
class IMFourLevelLoop
{
MONGE, CEDRICK D. BSIT 3-1 ADET

static void Main(string[] args)


{
int i = 1;
while (i <= 4)
{
int j = 1;
while (j <= i)
{
int k = 1;
while (k <= j)
{
int l = 1;
while (l <= k)
{
Console.Write($"{i}{j}{k}{l} ");
l++;
}
k++;
Console.WriteLine();
}
j++;
Console.WriteLine();
}
i++;
Console.WriteLine();
}
}
}
}

Output:
MONGE, CEDRICK D. BSIT 3-1 ADET

(Hard)

1. If statement

C# Program that computes the sum of two user inputted values

Code:

namespace ADETProg
{
class HRDIfSttemnt
{
static void Main(string[] args)
MONGE, CEDRICK D. BSIT 3-1 ADET

{
int num1;
int num2;
int total;
string? choice;

while(true)
{
Console.Write(" \nEnter first number : ");
num1 = Convert.ToInt32( Console.ReadLine());
Console.Write(" \nEnter second number : ");
num2 = Convert.ToInt32(Console.ReadLine());
total = num2 + num1;
Console.WriteLine(" \nTotal is : "+ total);
Console.ReadKey();

Console.Write(" \n\nWould you like to solve another number? (yes


or no) : ");
choice = Console.ReadLine();

if (choice == "no")
{
Console.WriteLine(" \nThe program is now ending \n\n Press
any Key..... ");
Console.ReadKey();
Console.Clear();
System.Environment.Exit(0);
}
}

}
}
}

Output:
MONGE, CEDRICK D. BSIT 3-1 ADET

2. Else if

C# Program that generates a simple question

Code:

namespace ADETProg
{
class HRDElseIf
{
static void Main(string[] args)
{
string? answer;
bool loop = true;
string? menu;

while (loop)
{
Console.WriteLine("\n\n What is the command keyword to
exit a loop in C#? ");
Console.WriteLine(" a.Quit ");
Console.WriteLine(" b.Continue ");
Console.WriteLine(" c.Break ");
Console.WriteLine(" d.Exit ");

Console.Write("\n Press 'x' To exit the program \n\nEnter


your answer: ");
MONGE, CEDRICK D. BSIT 3-1 ADET

answer = Console.ReadLine();

if (answer == "c")
{
Console.WriteLine(" Correct! ");
Console.ReadKey();
Console.WriteLine("\n Would you like to answer
the question again? (Input 'yes' or 'no')");
Console.Write(" Enter your Answer: ");
menu = Console.ReadLine();

if (menu == "no")
{
Console.WriteLine("\n\n Ending program
");
Console.ReadKey();
Console.Clear();
System.Environment.Exit(0);
}
}

else if (answer == "x")


{
Console.WriteLine("\n\n Ending program ");
Console.ReadKey();
Console.Clear();
System.Environment.Exit(0);
}

else
{
Console.WriteLine(" Incorrect! ");
Console.ReadKey();
Console.WriteLine("\n Would you like to answer
the question again? (Input 'yes 'or 'no')");
Console.Write(" Enter your Answer: ");
menu = Console.ReadLine();

if (menu == "no")
{
Console.WriteLine("\n\n Ending program
");
Console.ReadKey();
Console.Clear();
System.Environment.Exit(0);
MONGE, CEDRICK D. BSIT 3-1 ADET

}
}

}
}
}

Output:

3. Nested If

C# Program to generate a multiplication table


MONGE, CEDRICK D. BSIT 3-1 ADET

Code:

namespace ADETProg
{
class HRDNestedIf
{
static void MultiplicationTable()
{
int colm;

Console.Write("\n Enter the number of column : ");


colm = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(" ");

for (int i = 1; i <= 10; i++) //row


{
for (int j = 1; j <= colm; j++) //column
{
if (i == 1)
{
if (i * j < 10)
{
Console.Write($" {i * j} ");
}
else
{
Console.Write($"{i * j} ");
}
}
else if (i > 1 && i < 10)
{
if (i * j < 10)
{
Console.Write($" {i * j} ");
}
else
{
Console.Write($"{i * j} ");
}
}
else
{
Console.Write($"{i * j} ");
}
}
MONGE, CEDRICK D. BSIT 3-1 ADET

Console.WriteLine();
}
}

static void Main(string[] args)


{
string? choice2;

Console.WriteLine("\n C# Program to generate a multiplication table


");

while(true)
{
MultiplicationTable();

Console.Write("\n Would you like to enter a new multiplication


table? (yes or no) : ");
choice2 = Console.ReadLine();

if (choice2 == "no")
{
Console.WriteLine("\n The program is now ending \n\n
Press any Key..... ");
Console.ReadKey();
Console.Clear();
System.Environment.Exit(0);

}
}
}

Output:
MONGE, CEDRICK D. BSIT 3-1 ADET

4. While loop statement

C# Program that generates a random number

Code:

namespace ADETProg
{
class HRDWhileLoop
{
static void Main(string[] args)
{
string? choice;

Console.WriteLine("\n C# Program that generate a random number


");

while(true)
{
Random random = new Random();
int current = 0;

current = random.Next(1, 10);


Console.WriteLine("\n Random Number : " + current);

Console.Write("\n Do you want to generate a new random


number? (yes or no) : ");
MONGE, CEDRICK D. BSIT 3-1 ADET

choice = Console.ReadLine();

if (choice == "no")
{
Console.WriteLine("\n The program is now ending \n\n
Press any Key..... ");
Console.ReadKey();
Console.Clear();
System.Environment.Exit(0);
}
}

}
}
}

Output:

5. Do While statement.

C# program that uses a do-while loop to find the first Fibonacci number that exceeds a given
positive integer. The Fibonacci sequence is a series of numbers where each number is the sum
of the two preceding ones, starting with 0 and 1. The program should take an integer input
representing the threshold value. It should calculate and output the first Fibonacci number that
exceeds the threshold.
MONGE, CEDRICK D. BSIT 3-1 ADET

Code:

namespace ADETProg
{
class HRDIfSttemnt
{
static void Main(string[] args)
{
Console.Write("Enter a Threshold: ");
int threshold= Convert.ToInt32(Console.ReadLine());
int fibPrev = 0;
int fibCurr = 1;
int fibNext;

do
{
fibNext = fibPrev + fibCurr;
fibPrev = fibCurr;
fibCurr = fibNext;

} while (fibNext <= threshold);

Console.WriteLine("First Fibonacci number exceeding " + threshold +


": " + fibNext);
}
}
}

Output:

6. For loop statement

C# Program that displays my surname using asterisk


MONGE, CEDRICK D. BSIT 3-1 ADET

Code:

namespace ADETProg
{
class HRDForLoop
{
static void Main(string[] args)
{
int row,column;

Console.Write("\n\n");
Console.Write("Display my surname using asterisk:\n");
Console.Write("---------------------------------------------");
Console.Write("\n\n");

for(row=0;row<=6;row++) //Letter M
{
for (column=0; column<=6; column++)
{
if (column == 1 || column == 5 || (row == 2 && (column == 2
|| column == 4)) || (row == 3 && column == 3))
Console.Write("*");
else
Console.Write(" ");
}
Console.Write("\n");
}
Console.Write("\n");

for(row=0;row<=6;row++) //Letter O
{
for (column=0; column<=6; column++)
{
if (((column == 1 || column == 5) && row != 0 && row != 6) ||
((row == 0 || row == 6) && column > 1 && column < 5))
Console.Write("*");
else
Console.Write(" ");
}
Console.Write("\n");
}
Console.Write("\n");

for(row=0;row<=6;row++) //Letter N
{
MONGE, CEDRICK D. BSIT 3-1 ADET

for (column=0; column<=6; column++)


{
if (column == 1 || column == 5 || (row == column
&& column != 0 && column != 6))
Console.Write("*");
else
Console.Write(" ");
}
Console.Write("\n");
}
Console.Write("\n");

for(row=0;row<=6;row++) //Letter G
{
for (column=0; column<=6; column++)
{
if ((column == 1 && row != 0 && row != 6) ||
((row == 0 || row == 6) && column > 1 && column < 5) || (row == 3 && column > 2
&& column < 6) || (column == 5 && row != 0 && row != 2 && row != 6))
Console.Write("*");
else
Console.Write(" ");
}
Console.Write("\n");
}
Console.Write("\n");

for(row=0;row<=6;row++)//Letter E
{
for (column=0; column<=6; column++)
{
if (column == 1 || ((row == 0 || row == 6) &&
(column > 1 && column < 6)) || (row == 3 && column > 1 && column < 5))
Console.Write("*");
else
Console.Write(" ");
}
Console.Write("\n");
}
Console.Write("\n");

}
}
}
MONGE, CEDRICK D. BSIT 3-1 ADET

Output:

7. Foreach statement

C# program that uses a nested foreach loop to find and display all pairs of numbers from two
separate integer arrays whose sum is a given target value. The program should take three
inputs: two integer arrays (array1 and array2) containing distinct numbers, and a target sum
MONGE, CEDRICK D. BSIT 3-1 ADET

(target). It should then find and output all pairs of numbers (num1, num2) where num1 is from
array1 and num2 is from array2, such that num1 + num2 equals the target sum.

Code:

namespace ADETProg
{
class HRDForeach
{
static void Main(string[] args)
{
int[] array1 = { 1, 2, 3, 4, 5 };
int[] array2 = { 6, 7, 8, 9, 10 };
int target = 12;

foreach (int num1 in array1)


{
foreach (int num2 in array2)
{
if (num1 + num2 == target)
{
Console.WriteLine("Pair: ({0}, {1})", num1, num2);
}
}
}
}
}
}

Output:

8. 2 level nested loop statement


MONGE, CEDRICK D. BSIT 3-1 ADET

C# program that uses two nested loops to find and display all pairs of prime numbers within a
given range. The program should take two integer inputs: a starting number (start) and an
ending number (end) defining the range. It should then find and output all pairs of prime
numbers within the specified range. A prime number is a number greater than 1 that is divisible
only by 1 and itself.

Code:

namespace ADETProg
{
class HRD2lvlnested
{
static bool IsPrime(int number)
{
if (number < 2)
return false;

for (int i = 2; i <= Math.Sqrt(number); i++)


{
if (number % i == 0)
return false;
}

return true;
}

static void Main()


{
int start = 2;
int end = 20;

for (int i = start; i <= end; i++)


{
for (int j = start; j <= end; j++)
{
if (IsPrime(i) && IsPrime(j))
{
Console.WriteLine("Pair: ({0}, {1})", i, j);
}
}
}
}
}
}
MONGE, CEDRICK D. BSIT 3-1 ADET

Output:

9. 3 level combination of nested loop

C# program that uses three nested loops to generate and display all possible combinations of
three letters from a given set of letters. The program should take an array of characters (letters)
representing the available letters. It should then generate and output all possible combinations
of three letters from the given set.

Code:

namespace ADETProg
{
class HRD3lvlcmbnested
{
static void Main(string[] args)
{
char[] letters = { 'A', 'B', 'C' };

foreach (char letter1 in letters)


MONGE, CEDRICK D. BSIT 3-1 ADET

{
foreach (char letter2 in letters)
{
foreach (char letter3 in letters)
{
Console.WriteLine("{0}{1}{2}", letter1, letter2,
letter3);
}
}
}
}
}
}

Output:
MONGE, CEDRICK D. BSIT 3-1 ADET

10. 4 level combination of nested loop

C# Program that returns a string of even or odd numbers greater than 0 and less than 20

Code:

namespace ADETProg
{
class HRD4levelnested
{
static string ReturnEvenNumbers()
{
string str = string.Empty;
string? choice;

while (true)
{

Console.WriteLine("\n Program that returns a string of


even or odd numbers greater than 0 and less than 20 ");
Console.Write("\n Enter your choice (odd or even) : ");
choice = Console.ReadLine();
Console.WriteLine("\n");

if (choice == "even")
{
Console.Write(" Even Numbers : ");
for (int i = 1; i < 20; i++)
{
if (i % 2 == 0)
{
str += i + " ";
}
}
return str;
}

else if (choice == "odd")


{
Console.Write(" Odd Numbers : ");
for (int i = 1; i < 20; i++)
{
if (i % 2 == 1)
MONGE, CEDRICK D. BSIT 3-1 ADET

{
str += i + " ";
}
}
return str;
}

else
{
Console.WriteLine(" Wrong Choice!! Please Try
Again ");
return str;
}

}
}

static void Main(string[] args)


{

string? choice2;

while (true)
{
Console.WriteLine(ReturnEvenNumbers());

Console.Write("\n Would you like to select other choices? (yes or


no) : ");
choice2 = Console.ReadLine();

if (choice2 == "no")
{
Console.WriteLine("\n The program is now ending \n\n
Press any Key..... ");
Console.ReadKey();
Console.Clear();
System.Environment.Exit(0);

}
}

}
}
}
MONGE, CEDRICK D. BSIT 3-1 ADET

Output:

You might also like