Professional Documents
Culture Documents
(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);
}
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
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++;
Output:
Code:
namespace ADETProg
{
class ForLoop
{
static void Main(string[] args)
{
MONGE, CEDRICK D. BSIT 3-1 ADET
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" };
Output:
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
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
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:
Code:
namespace ADETProg
{
class IMIF
{
static void Main(string[] args)
{
MONGE, CEDRICK D. BSIT 3-1 ADET
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
}
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)
{
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:
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;
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);
Output:
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;
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;
Output:
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
Console.WriteLine();
}
}
}
}
Output:
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;
currentChar++;
charCount++;
}
Console.WriteLine();
level++;
}
}
}
}
Output:
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
Output:
MONGE, CEDRICK D. BSIT 3-1 ADET
(Hard)
1. If statement
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();
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
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 ");
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
{
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
Code:
namespace ADETProg
{
class HRDNestedIf
{
static void MultiplicationTable()
{
int colm;
Console.WriteLine();
}
}
while(true)
{
MultiplicationTable();
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
Code:
namespace ADETProg
{
class HRDWhileLoop
{
static void Main(string[] args)
{
string? choice;
while(true)
{
Random random = new Random();
int current = 0;
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;
Output:
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(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;
Output:
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;
return true;
}
Output:
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 letter2 in letters)
{
foreach (char letter3 in letters)
{
Console.WriteLine("{0}{1}{2}", letter1, letter2,
letter3);
}
}
}
}
}
}
Output:
MONGE, CEDRICK D. BSIT 3-1 ADET
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)
{
if (choice == "even")
{
Console.Write(" Even Numbers : ");
for (int i = 1; i < 20; i++)
{
if (i % 2 == 0)
{
str += i + " ";
}
}
return str;
}
{
str += i + " ";
}
}
return str;
}
else
{
Console.WriteLine(" Wrong Choice!! Please Try
Again ");
return str;
}
}
}
string? choice2;
while (true)
{
Console.WriteLine(ReturnEvenNumbers());
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: