Phone no: +234 705 673 3798 or email us: info@kdesglobal.com
for (int i = 1; i <= 10; i++)
{
Console.WriteLine(i);
}
int sum = 0;
for (int i = 2; i <= 20; i += 2)
{
sum += i;
}
Console.WriteLine("Sum: " + sum);
Console.Write("Enter a number: ");
int number = Convert.ToInt32(Console.ReadLine());
for (int i = 1; i <= 10; i++)
{
Console.WriteLine($"{number} x {i} = {number * i}");
}
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: " + factorial);
Console.Write("Enter a number: ");
int number = Convert.ToInt32(Console.ReadLine());
int sum = 0;
while (number > 0)
{
sum += number % 10;
number /= 10;
}
Console.WriteLine("Sum of digits: " + sum);
Console.Write("Enter a number: ");
int number = Convert.ToInt32(Console.ReadLine());
bool isPrime = true;
for (int i = 2; i <= Math.Sqrt(number); i++)
{
if (number % i == 0)
{
isPrime = false;
break;
}
}
Console.WriteLine("Is Prime: " + isPrime);
Console.Write("Enter the number of terms: ");
int n = Convert.ToInt32(Console.ReadLine());
int first = 0, second = 1;
Console.Write("Fibonacci Series: " + first + ", " + second);
for (int i = 2; i < n; i++)
{
int next = first + second;
Console.Write(", " + next);
first = second;
second = next;
}
Console.WriteLine();
int[] array = { 10, 5, 20, 15, 30 };
int max = array[0];
for (int i = 1; i < array.Length; i++)
{
if (array[i] > max)
{
max = array[i];
}
}
Console.WriteLine("Largest element: " + max);
Console.Write("Enter a string: ");
string input = Console.ReadLine();
int count = 0;
string vowels = "aeiou";
for (int i = 0; i < input.Length; i++)
{
if (vowels.Contains(input[i]))
{
count++;
}
}
Console.WriteLine("Number of vowels: " + count);
Console.Write("Enter a string: ");
string input = Console.ReadLine();
string reversed = "";
for (int i = input.Length - 1; i >= 0; i--)
{
reversed += input[i];
}
Console.WriteLine("Reversed string: " + reversed);
Console.Write("Enter a number: ");
int number = Convert.ToInt32(Console.ReadLine());
while (number > 9)
{
int sum = 0;
while (number > 0)
{
sum += number % 10;
number /= 10;
}
number = sum;
}
Console.WriteLine("Single digit sum: " + number);
for (2 c = 'A'; c <= 'Z'; c++)
{
Console.WriteLine($"{c}: {(int)c}");
}
int[] array = { 10, 5, 20, 15, 30 };
int max = array[0];
int secondMax = int.MinValue;
for (int i = 1; i < array.Length; i++)
{
if (array[i] > max)
{
secondMax = max;
max = array[i];
}
else if (array[i] > secondMax && array[i] != max)
{
secondMax = array[i];
}
}
Console.WriteLine("Second largest element: " + secondMax);
Console.Write("Enter a string: ");
string input = Console.ReadLine();
bool isPalindrome = true;
for (int i = 0; i < input.Length / 2; i++)
{
if (input[i] != input[input.Length - i - 1])
{
isPalindrome = false;
break;
}
}
Console.WriteLine("Is Palindrome: " + isPalindrome);
Console.Write("Enter a string: ");
string input = Console.ReadLine();
Console.Write("Enter a character: ");
char character = Console.ReadLine()[0];
int count = 0;
for (int i = 0; i < input.Length; i++)
{
if (input[i] == character)
{
count++;
}
}
Console.WriteLine("Occurrences: " + count);
Console.Write("Enter the first number: ");
int number1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the second number: ");
int number2 = Convert.ToInt32(Console.ReadLine());
int gcd = 1;
for (int i = 1; i <= number1 && i <= number2; i++)
{
if (number1 % i == 0 && number2 % i == 0)
{
gcd = i;
}
}
Console.WriteLine("GCD: " + gcd);
Console.Write("Enter a number: ");
int number = Convert.ToInt32(Console.ReadLine());
int originalNumber = number;
int sum = 0;
int digitCount = (int)Math.Log10(number) + 1;
while (number > 0)
{
int digit = number % 10;
sum += (int)Math.Pow(digit, digitCount);
number /= 10;
}
bool isArmstrong = sum == originalNumber;
Console.WriteLine("Is Armstrong: " + isArmstrong);
Console.Write("Enter the first number: ");
int number1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the second number: ");
int number2 = Convert.ToInt32(Console.ReadLine());
int lcm = Math.Max(number1, number2);
while (true)
{
if (lcm % number1 == 0 && lcm % number2 == 0)
{
break;
}
lcm++;
}
Console.WriteLine("LCM: " + lcm);
Console.Write("Enter the lower bound: ");
int lowerBound = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the upper bound: ");
int upperBound = Convert.ToInt32(Console.ReadLine());
Console.Write("Prime numbers: ");
for (int number = lowerBound; number <= upperBound; number++)
{
bool isPrime = true;
for (int i = 2; i <= Math.Sqrt(number); i++)
{
if (number % i == 0)
{
isPrime = false;
break;
}
}
if (isPrime && number > 1)
{
Console.Write(number + " ");
}
}
Console.WriteLine();
Console.Write("Enter the lower bound: ");
int lowerBound = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the upper bound: ");
int upperBound = Convert.ToInt32(Console.ReadLine());
int sum = 0;
for (int number = lowerBound; number <= upperBound; number++)
{
if (number % 2 != 0)
{
sum += number;
}
}
Console.WriteLine("Sum of odd numbers: " + sum);
Console.Write("Enter the value of n: ");
int n = Convert.ToInt32(Console.ReadLine());
Console.Write("Natural numbers: ");
for (int i = 1; i <= n; i++)
{
Console.Write(i + " ");
}
Console.WriteLine();
Console.Write("Enter the value of n: ");
int n = Convert.ToInt32(Console.ReadLine());
int sum = 0;
for (int i = 1; i <= n; i++)
{
sum += i;
}
Console.WriteLine("Sum: " + sum);
Console.Write("Enter the value of n: ");
int n = Convert.ToInt32(Console.ReadLine());
Console.Write("Even numbers: ");
for (int i = 1; i <= n; i++)
{
Console.Write(2 * i + " ");
}
Console.WriteLine();
Console.Write("Enter the value of n: ");
int n = Convert.ToInt32(Console.ReadLine());
int sum = 0;
for (int i = 1; i <= n; i++)
{
sum += i * i;
}
Console.WriteLine("Sum of squares: " + sum);
*
**
***
****
*****
using a for loop.
Console.Write("Enter the 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();
}
Write a C# program to print the pattern:
*****
****
***
**
*
using a for loop.
Console.Write("Enter the number of rows: ");
int rows = Convert.ToInt32(Console.ReadLine());
for (int i = rows; i >= 1; i--)
{
for (int j = 1; j <= i; j++)
{
Console.Write("*");
}
Console.WriteLine();
}
*
**
***
****
*****
using a for loop.
Console.Write("Enter the number of rows: ");
int rows = Convert.ToInt32(Console.ReadLine());
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= rows - i; j++)
{
Console.Write(" ");
}
for (int k = 1; k <= i; k++)
{
Console.Write("*");
}
Console.WriteLine();
}
*****
****
***
**
*
using a for loop.
Console.Write("Enter the 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(" ");
}
for (int k = i; k <= rows; k++)
{
Console.Write("*");
}
Console.WriteLine();
}
1
121
12321
1234321
123454321
using a for loop.
Console.Write("Enter the number of rows: ");
int rows = Convert.ToInt32(Console.ReadLine());
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= rows - i; j++)
{
Console.Write(" ");
}
for (int k = 1; k <= i; k++)
{
Console.Write(k);
}
for (int l = i - 1; l >= 1; l--)
{
Console.Write(l);
}
Console.WriteLine();
}
Console.Write("Enter the value of n: ");
int n = Convert.ToInt32(Console.ReadLine());
double sum = 0;
for (int i = 1; i <= n; i++)
{
sum += 1.0 / i;
}
Console.WriteLine("Sum: " + sum);
Console.Write("Enter the value of n: ");
int n = Convert.ToInt32(Console.ReadLine());
double sum = 0;
for (int i = 1; i <= n; i++)
{
sum += i;
}
double average = sum / n;
Console.WriteLine("Average: " + average);
Console.Write("Enter the lower bound: ");
int lowerBound = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the upper bound: ");
int upperBound = Convert.ToInt32(Console.ReadLine());
int sum = 0;
for (int number = lowerBound; number <= upperBound; number++)
{
bool isPrime = true;
for (int i = 2; i <= Math.Sqrt(number); i++)
{
if (number % i == 0)
{
isPrime = false;
break;
}
}
if (isPrime && number > 1)
{
sum += number;
}
}
Console.WriteLine("Sum of prime numbers: " + sum);
Console.Write("Enter a number: ");
int number = Convert.ToInt32(Console.ReadLine());
int sum = 0;
while (number > 0)
{
sum += number - (number / 10) * 10;
number /= 10;
}
Console.WriteLine("Sum of digits: " + sum);
Console.Write("Enter a number: ");
int number = Convert.ToInt32(Console.ReadLine());
int reverse = 0;
while (number > 0)
{
reverse = reverse * 10 + number % 10;
number /= 10;
}
Console.WriteLine("Reversed number: " + reverse);
Console.Write("Enter a number: ");
int number = Convert.ToInt32(Console.ReadLine());
int originalNumber = number;
int reverse = 0;
while (number > 0)
{
reverse = reverse * 10 + number % 10;
number /= 10;
}
bool isPalindrome = reverse == originalNumber;
Console.WriteLine("Is Palindrome: " + isPalindrome);
Console.Write("Enter a number: ");
int number = Convert.ToInt32(Console.ReadLine());
string strippedInput = input.Replace(" ", "").ToLower();
bool isPalindrome = true;
for (int i = 0; i < strippedInput.Length / 2; i++)
{
if (strippedInput[i] != strippedInput[strippedInput.Length - i - 1])
{
isPalindrome = false;
break;
}
}
Console.WriteLine("Is Palindrome: " + isPalindrome);
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: " + factorial);
Console.Write("Enter a number: ");
int number = Convert.ToInt32(Console.ReadLine());
int a = 0;
int b = 1;
Console.Write("Fibonacci series: " + a + " " + b + " ");
for (int i = 3; i <= number; i++)
{
int c = a + b;
Console.Write(c + " ");
a = b;
b = c;
}
Console.WriteLine();
Console.Write("Enter a number: ");
int number = Convert.ToInt32(Console.ReadLine());
int digitCount = 0;
while (number != 0)
{
number /= 10;
digitCount++;
}
Console.WriteLine("Number of digits: " + digitCount);
int[] array = { 10, 5, 20, 15, 30 };
int max = array[0];
for (int i = 1; i < array.Length; i++)
{
if (array[i] > max)
{
max = array[i];
}
}
Console.WriteLine("Largest element: " + max);
int[] array = { 10, 5, 20, 15, 30 };
int evenCount = 0;
int oddCount = 0;
for (int i = 0; i < array.Length; i++)
{
if (array[i] % 2 == 0)
{
evenCount++;
}
else
{
oddCount++;
}
}
Console.WriteLine("Even count: " + evenCount);
Console.WriteLine("Odd count: " + oddCount);
int[,] array = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
int sum = 0;
for (int i = 0; i < array.GetLength(0); i++)
{
for (int j = 0; j < array.GetLength(1); j++)
{
sum += array[i, j];
}
}
Console.WriteLine("Sum: " + sum);
int[,] matrix = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
int rows = matrix.GetLength(0);
int columns = matrix.GetLength(1);
int[,] transpose = new int[columns, rows];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
transpose[j, i] = matrix[i, j];
}
}
Console.WriteLine("Original matrix:");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
Console.Write(matrix[i, j] + " ");
}
Console.WriteLine();
}
Console.WriteLine("Transposed matrix:");for (int i = 0; i < columns; i++)
{
for (int j = 0; j < rows; j++)
{
Console.Write(transpose[i, j] + " ");
}
Console.WriteLine();
}
int[,] matrix = { { 1, 2, 3 }, { 2, 4, 5 }, { 3, 5, 6 } };
bool isSymmetric = true;
int rows = matrix.GetLength(0);
int columns = matrix.GetLength(1);
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
if (matrix[i, j] != matrix[j, i])
{
isSymmetric = false;
break;
}
}
if (!isSymmetric)
{
break;
}
}
Console.WriteLine("Is symmetric: " + isSymmetric);
int[,] matrix = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
int sum = 0;
int size = matrix.GetLength(0);
for (int i = 0; i < size; i++)
{
sum += matrix[i, i];
}
Console.WriteLine("Sum of main diagonal elements: " + sum);
int[,] matrix = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
int sum = 0;
int size = matrix.GetLength(0);
for (int i = 0; i < size; i++)
{
sum += matrix[i, size - i - 1];
}
Console.WriteLine("Sum of secondary diagonal elements: " + sum);
Console.Write("Enter a string: ");string input = Console.ReadLine();
int vowelCount = 0;
int consonantCount = 0;
string vowels = "aeiouAEIOU";
for (int i = 0; i < input.Length; i++)
{
if (Char.IsLetter(input[i]))
{
if (vowels.Contains(input[i]))
{
vowelCount++;
}
else
{
consonantCount++;
}
}
}
Console.WriteLine("Vowel count: " + vowelCount);
Console.WriteLine("Consonant count: " + consonantCount);
Console.Write("Enter a string: ");string input = Console.ReadLine();
Console.Write("Enter a character: ");char character = Convert.ToChar(Console.ReadLine());
int count = 0;
for (int i = 0; i < input.Length; i++)
{
if (input[i] == character)
{
count++;
}
}
Console.WriteLine("Occurrences of " + character + ": " + count);
Console.Write("Enter a sentence: ");string sentence = Console.ReadLine();
string[] words = sentence.Split(' ');
int maxLength = 0;
for (int i = 0; i < words.Length; i++)
{
if (words[i].Length > maxLength)
{
maxLength = words[i].Length;
}
}
Console.WriteLine("Length of longest word: " + maxLength);
Console.Write("Enter a string: ");string input = Console.ReadLine().ToLower();
bool[] letters = new bool[26];
for (int i = 0; i < input.Length; i++)
{
if (Char.IsLetter(input[i]))
{
letters[input[i] - 'a'] = true;
}
}
bool isPangram = true;
for (int i = 0; i < letters.Length; i++)
{
if (!letters[i])
{
isPangram = false;
break;
}
}
Console.WriteLine("Is pangram: " + isPangram);
Read Now Top 15 Reasosns why you need a website for your BusinessYou don’t need to do all of these alone, We got you covered!! Contact us now your satisfaction is always our priority. price definitely won't be a problem.
« Previous Simplified Instruction to the Difference between C# IEnumerable, IEnumerator and IQueryable |
Next » Major Types of Phishing Attacks scammers use to fool their victims |
Written by: Idika Destiny
Reading time:
Published 17 hours Ago On Wednesday, August 9, 2023
Updated 17 hours Ago On Wednesday, August 9, 2023
466 Views