C# Set0 Saurabh Shukla Doc
C# Set0 Saurabh Shukla Doc
using System;
int n, n1 = 0, n2 = 1, n3;
n = int.Parse(Console.ReadLine());
n3 = n1 + n2;
n1 = n2;
n2 = n3;
using System;
int sum = 0;
int temp = number;
int digits = 0;
while (temp != 0)
{
digits++;
temp /= 10;
}
temp = number;
while (temp != 0)
{
int digit = temp % 10;
sum += (int)Math.Pow(digit, digits);
temp /= 10;
}
if (sum == number)
{
Console.WriteLine("{0} is an Armstrong number", number);
}
else
{
Console.WriteLine("{0} is not an Armstrong number", number);
}
Console.ReadLine();
}
}
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the first string");
string str1 = Console.ReadLine();
Console.WriteLine("Enter the Second string");
string str2 = Console.ReadLine();
if (str1 != str2)
{
Console.WriteLine("The strings arn't anagrams");
}
else
{
char[] strArr1 = str1.ToCharArray();
char[] strArr2 = str2.ToCharArray();
Array.Sort(strArr1);
Array.Sort(strArr2);
bool isEqual = true;
for ( int i = 0; i < strArr1.Length; i++)
{
if (strArr1[i] != strArr2[i])
{
isEqual = false;
break;
}
}
if (isEqual)
{
Console.WriteLine("Strings are Anagram");
}
else
{
Console.WriteLine("Strings aren't Anagram");
}
}
Console.ReadLine();
}
}
using System;
class Program
{
static void Main(string[] args)
{
Console.Write("Enter a positive integer: ");
int num = int.Parse(Console.ReadLine());
if (num == 1 || num == 0)
{
isPrime = false;
}
else
{
for (int i = 2; i <= Math.Sqrt(num); i++)
{
if (num % i == 0)
{
isPrime = false;
break;
}
}
}
if (isPrime)
{
Console.WriteLine("{0} is a prime number.", num);
}
else
{
Console.WriteLine("{0} is not a prime number.", num);
}
Console.ReadLine();
}
}
while (num != 0)
{
remainder = num % 10;
reversedNum = reversedNum * 10 + remainder;
num = num / 10;
}
Console.WriteLine("Sorted array:");
for (int i = 0; i < n; i++)
{
Console.Write(arr[i] + " ");
}
}
}
string[] words = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
Program to find an array element such that all elements are divisible by it.
using System;
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the number of elements in the array:");
int n = int.Parse(Console.ReadLine());
int[] arr = new int[n];
Console.WriteLine("Enter the elements of the array:");
for (int i = 0; i < n; i++)
{
arr[i] = int.Parse(Console.ReadLine());
}
if (flag)
{
Console.WriteLine(gcd);
}
else
{
Console.WriteLine("-1");
}
}
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a string:");
string str = Console.ReadLine();
string reverse = "";
for (int i = str.Length - 1; i >= 0; i--)
{
reverse += str[i];
}
if (str == reverse)
{
Console.WriteLine("The string is a palindrome.");
}
else
{
Console.WriteLine("The string is not a palindrome.");
}
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the number of elements in the array:");
int n = int.Parse(Console.ReadLine());
int[] arr = new int[n];
Console.WriteLine("Enter the elements of the array:");
for (int i = 0; i < n; i++)
{
arr[i] = int.Parse(Console.ReadLine());
}
int min = arr[0];
int max = arr[0];
for (int i = 1; i < n; i++)
{
if (arr[i] < min)
{
min = arr[i];
}
if (arr[i] > max)
{
max = arr[i];
}
}
Console.WriteLine("The smallest number in the array is: " + min);
Console.WriteLine("The largest number in the array is: " + max);
}
}
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a string:");
string str = Console.ReadLine();
Dictionary<char, int> charCount = new Dictionary<char, int>();
foreach (char c in str)
{
if (charCount.ContainsKey(c))
{
charCount[c]++;
}
else
{
charCount[c] = 1;
}
}
char maxChar = '\0';
int maxCount = 0;
foreach (KeyValuePair<char, int> pair in charCount)
{
if (pair.Value > maxCount)
{
maxChar = pair.Key;
maxCount = pair.Value;
}
}
if (maxCount > 1)
{
Console.WriteLine("The most frequently occurring character in the string is: " + maxChar);
}
else
{
Console.WriteLine("no");
}
}
}
121
12321
1234321
123454321
12345654321
using System;
class Program {
static void Main(string[] args) {
int n = 6; // number of rows
for (int i = 1; i <= n; i++) {
// print left spaces
for (int j = 1; j <= n - i; j++) {
Console.Write(" ");
}
// print left half of row
for (int j = 1; j <= i; j++) {
Console.Write(j);
}
// print right half of row
for (int j = i - 1; j >= 1; j--) {
Console.Write(j);
}
// print right spaces
for (int j = 1; j <= n - i; j++) {
Console.Write(" ");
}
Console.WriteLine();
}
}
}
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a string:");
string str = Console.ReadLine();
Console.Write("The ASCII values of the string are: ");
for (int i = 0; i < str.Length; i++)
{
Console.Write((int)str[i] + " ");
}
}
}
using System;
class Program
{
static void Main(string[] args)
{
int sum = 0;
for (int i = 1; i <= 20; i++)
{
if (i % 3 == 0 && i % 4 == 0)
{
sum += i;
}
}
Console.WriteLine("The sum of all the numbers between 1 and 20 which are divisible by 3 and 4 is: " + sum);
}
}