0% found this document useful (0 votes)
44 views10 pages

C# Set0 Saurabh Shukla Doc

The document contains code snippets for several C# programs: 1. A program to print the Fibonacci series up to a given number of terms. 2. A program to check if a number is an Armstrong number. 3. A program to check if two strings are anagrams by comparing character counts.

Uploaded by

sheeba
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)
44 views10 pages

C# Set0 Saurabh Shukla Doc

The document contains code snippets for several C# programs: 1. A program to print the Fibonacci series up to a given number of terms. 2. A program to check if a number is an Armstrong number. 3. A program to check if two strings are anagrams by comparing character counts.

Uploaded by

sheeba
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/ 10

C# Set-0

1.Program to print Fibonacci series up to n terms.

using System;

public class Fibo

public static void Main(String[] args)

int n, n1 = 0, n2 = 1, n3;

Console.Write("enter a number to find Fibonacci series: ");

n = int.Parse(Console.ReadLine());

Console.Write(n1 + " " + n2 + " ");

for (int i = 2; i < n; i++)

n3 = n1 + n2;

Console.Write(n3 + " ");

n1 = n2;

n2 = n3;

2. Program to check whether the number is Armstrong Number or


not.
Hint: Armstrong number is sum of cube of digits equal to the number
itself.
e.g.: 371 is Armstrong. 33 + 73 + 13=371

using System;

public class Test


{

public static void Main()


{
Console.Write("Enter a number: ");
int number = int.Parse(Console.ReadLine());

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();
}
}

3. Program to check whether the two strings are anagram or not.


Hint: both strings should contain equal count of each characters.
e.g.: dead and daed is anagram, but dead and deaf is not.

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();
}
}

Program to check whether the number is Prime Number or not.

using System;

class Program
{
static void Main(string[] args)
{
Console.Write("Enter a positive integer: ");
int num = int.Parse(Console.ReadLine());

bool isPrime = true;

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();
}
}

Program to reverse a number.


using System;

public class Program


{
public static void Main(string[] args)
{
int num, reversedNum = 0, remainder;

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


num = int.Parse(Console.ReadLine());

while (num != 0)
{
remainder = num % 10;
reversedNum = reversedNum * 10 + remainder;
num = num / 10;
}

Console.WriteLine("The reversed number is: " + reversedNum);


}
}

Program to sort an array of elements using bubble sort algorithm


using System;

public class Program


{
public static void Main(string[] args)
{
Console.Write("Enter the number of elements in the array: ");
int n = Convert.ToInt32(Console.ReadLine());

int[] arr = new int[n];

Console.WriteLine("Enter the elements of the array:");

for (int i = 0; i < n; i++)


{
arr[i] = Convert.ToInt32(Console.ReadLine());
}

for (int i = 0; i < n - 1; i++)


{
for (int j = 0; j < n - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{

int temp = arr[j];


arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}

Console.WriteLine("Sorted array:");
for (int i = 0; i < n; i++)
{
Console.Write(arr[i] + " ");
}
}
}

Program to print Factorial Number up to n terms.


using System;

public class Program


{
public static void Main(string[] args)
{
Console.Write("Enter the value of n: ");
int n = int.Parse(Console.ReadLine());

Console.WriteLine("Factorial Numbers up to n terms: ");

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


{
int factorial = 1;

for (int j = i; j >= 1; j--)


{
factorial *= j;
}

Console.Write(factorial + " ");


}
}
}

Program to convert number to words i.e. 231 as two three one.


using System;

public class Program


{
static void Main(string[] args)
{
Console.Write("Enter a number: ");
int number = int.Parse(Console.ReadLine());

Console.Write("The number in words is: ");

string[] words = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };

foreach (char c in number.ToString())


{

int digit = int.Parse(c.ToString());


Console.Write(words[digit] + " ");
}
}
}

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());
}

int gcd = arr[0];


for (int i = 1; i < n; i++)
{
gcd = GCD(gcd, arr[i]);
}

bool flag = true;


for (int i = 0; i < n; i++)
{
if (arr[i] % gcd != 0)
{
flag = false;
break;
}
}

if (flag)
{
Console.WriteLine(gcd);
}
else
{
Console.WriteLine("-1");
}
}

static int GCD(int a, int b)


{
if (b == 0)
{
return a;
}
else
{
return GCD(b, a % b);
}
}
}

4. Program to check whether the string is Palindrome or not.


Hint: Reverse of the string should be same as the original string
e.g.: Madam.

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.");
}
}
}

5. Find the largest and the smallest number from an array


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());
}
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);
}
}

6. Print the max repeated character from string

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");
}
}
}

7. Program for triangle pattern

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();
}
}
}

8. Program to find the ascii value of a string.


Hint: Ascii value of “hello” is 104 101 108 108 111

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] + " ");
}
}
}

9. Program to print sum of all the numbers between 1 and 20 which


are divisible by 3 and 4.

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);
}
}

You might also like