Reverse a number
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a Number");
int numb = int.Parse(Console.ReadLine());
int reverse = 0;
while (numb > 0)
{
int rem = numb % 10;
reverse = (reverse * 10) + rem;
numb = numb / 10;
}
Console.WriteLine("Reverse number={0}", reverse);
Console.ReadLine();
}
}
Output
----------
Enter a Number
4567
Reverse number=7654
Palindrome Program
class program
{
public static void Main()
{
int num, temp, remainder, reverse = 0;
Console.WriteLine("Enter an integer \n");
num = int.Parse(Console.ReadLine());
temp = num;
while (num > 0)
{
remainder = num % 10;
reverse = reverse * 10 + remainder;
num /= 10;
}
Console.WriteLine("Given number is = {0}", temp);
Console.WriteLine("Its reverse is = {0}", reverse);
if (temp == reverse)
Console.WriteLine("Number is a palindrome \n");
else
Console.WriteLine("Number is not a palindrome \n");
Console.ReadLine();
}
}
//Output--535 is a prime number(if its reverse also 535 only)
Armstrong Number
An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself.
class Program
{
static void Main(string[] args)
{
int number, remainder, sum = 0;
Console.Write("enter the Number");
number = int.Parse(Console.ReadLine());
for (int i = number; i > 0; i = i / 10)
{
remainder = i % 10;
sum = sum + remainder*remainder*remainder;
}
if (sum == number)
{
Console.Write("Entered Number is an Armstrong Number");
}
else
Console.Write("Entered Number is not an Armstrong Number");
Console.ReadLine();
}
}
Output
----------
Enter the Number : 371
Entered Number is an Armstrong Number
Explantion:3Cube+7cube+1cube=371
Sum of given Number
static void Main(string[] args)
{
int num, sum = 0, r;
Console.WriteLine("Enter a Number : ");
num = int.Parse(Console.ReadLine());
while (num != 0)
{
r = num % 10;
num = num / 10;
sum = sum + r;
}
Console.WriteLine("Sum of Digits of the Number : "+sum);
Console.ReadLine();
}
//Output--Enter a Number : 123
//Sum of Digits of the Number : 6
Prime Numbers Between 1 to 10
class Program
{
static void Main(string[] args)
{
bool isPrime = true;
Console.WriteLine("Prime Numbers : ");
for (int i = 2; i <= 10; i++)
{
for (int j = 2; j <= 10; j++)
{
if (i != j && i % j == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
{
Console.Write("\t" + i);
}
isPrime = true;
}
Console.ReadKey();
}
}
//output--2 3 5 7
strong number
A number is called strong number if sum of the factorial of its digit is equal to number itself.
Ex : 1! + 4! + 5! = 1 + 24 + 120 = 145
Code:
static void Main(string[] args)
{
Int32 num, i, f, r, sum = 0, temp;
Console.WriteLine("enter the number");
num = Convert.ToInt16((Console.ReadLine()));
temp=num;
while (num != 0)
{
i = 1; f = 1;
r = num % 10;
while (i <= r)
{
f = f * i;
i++;
}
sum = sum + f;
num = num / 10;
}
if (sum == temp)
Console.WriteLine("Strong No", temp);
else
Console.WriteLine("not Strong NO", temp);
Console.ReadLine();
}
static void Main(string[] args)
{
string Str, reversestring = "";
int Length;
Console.Write("Enter A String : ");
Str = Console.ReadLine();
Length = Str.Length - 1;
while (Length >= 0)
{
reversestring = reversestring + Str[Length];
Length--;
}
Console.WriteLine("Reverse String Is {0}", reversestring);
Console.ReadLine();
}
collasition?
swap even and odd characters in a string
static void Main(string[] args)
{
string input = "Gupta"; //Sample String
// O/P--uGtpa
StringBuilder output = new StringBuilder();
char[] characters = input.ToCharArray();
for (int i = 0; i < characters.Length; i++)
{
if (i % 2 == 0)
{
if ((i + 1) < characters.Length)
{
output.Append(characters[i + 1]);
}
output.Append(characters[i]);
}
}
Console.WriteLine(output);
Console.ReadLine();
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a Number");
int numb = int.Parse(Console.ReadLine());
int reverse = 0;
while (numb > 0)
{
int rem = numb % 10;
reverse = (reverse * 10) + rem;
numb = numb / 10;
}
Console.WriteLine("Reverse number={0}", reverse);
Console.ReadLine();
}
}
Output
----------
Enter a Number
4567
Reverse number=7654
Palindrome Program
class program
{
public static void Main()
{
int num, temp, remainder, reverse = 0;
Console.WriteLine("Enter an integer \n");
num = int.Parse(Console.ReadLine());
temp = num;
while (num > 0)
{
remainder = num % 10;
reverse = reverse * 10 + remainder;
num /= 10;
}
Console.WriteLine("Given number is = {0}", temp);
Console.WriteLine("Its reverse is = {0}", reverse);
if (temp == reverse)
Console.WriteLine("Number is a palindrome \n");
else
Console.WriteLine("Number is not a palindrome \n");
Console.ReadLine();
}
}
//Output--535 is a prime number(if its reverse also 535 only)
An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself.
class Program
{
static void Main(string[] args)
{
int number, remainder, sum = 0;
Console.Write("enter the Number");
number = int.Parse(Console.ReadLine());
for (int i = number; i > 0; i = i / 10)
{
remainder = i % 10;
sum = sum + remainder*remainder*remainder;
}
if (sum == number)
{
Console.Write("Entered Number is an Armstrong Number");
}
else
Console.Write("Entered Number is not an Armstrong Number");
Console.ReadLine();
}
}
Output
----------
Enter the Number : 371
Entered Number is an Armstrong Number
Explantion:3Cube+7cube+1cube=371
Sum of given Number
static void Main(string[] args)
{
int num, sum = 0, r;
Console.WriteLine("Enter a Number : ");
num = int.Parse(Console.ReadLine());
while (num != 0)
{
r = num % 10;
num = num / 10;
sum = sum + r;
}
Console.WriteLine("Sum of Digits of the Number : "+sum);
Console.ReadLine();
}
//Output--Enter a Number : 123
//Sum of Digits of the Number : 6
Prime Numbers Between 1 to 10
class Program
{
static void Main(string[] args)
{
bool isPrime = true;
Console.WriteLine("Prime Numbers : ");
for (int i = 2; i <= 10; i++)
{
for (int j = 2; j <= 10; j++)
{
if (i != j && i % j == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
{
Console.Write("\t" + i);
}
isPrime = true;
}
Console.ReadKey();
}
}
//output--2 3 5 7
strong number
A number is called strong number if sum of the factorial of its digit is equal to number itself.
Ex : 1! + 4! + 5! = 1 + 24 + 120 = 145
Code:
static void Main(string[] args)
{
Int32 num, i, f, r, sum = 0, temp;
Console.WriteLine("enter the number");
num = Convert.ToInt16((Console.ReadLine()));
temp=num;
while (num != 0)
{
i = 1; f = 1;
r = num % 10;
while (i <= r)
{
f = f * i;
i++;
}
sum = sum + f;
num = num / 10;
}
if (sum == temp)
Console.WriteLine("Strong No", temp);
else
Console.WriteLine("not Strong NO", temp);
Console.ReadLine();
}
Number sort
int temp = 0;
int[] arr = new int[] { 20, 65, 98, 71, 64, 11, 2, 80, 5, 6, 100, 50, 13, 9, 80, 454 };
for (int i = 0; i < arr.Length; i++)
{
for (int j = i + 1; j < arr.Length; j++)
{
if (arr[i] > arr[j])
{
temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
Console.WriteLine(arr[i]);
}
string reversestatic void Main(string[] args)
{
string Str, reversestring = "";
int Length;
Console.Write("Enter A String : ");
Str = Console.ReadLine();
Length = Str.Length - 1;
while (Length >= 0)
{
reversestring = reversestring + Str[Length];
Length--;
}
Console.WriteLine("Reverse String Is {0}", reversestring);
Console.ReadLine();
}
collasition?
swap even and odd characters in a string
static void Main(string[] args)
{
string input = "Gupta"; //Sample String
// O/P--uGtpa
StringBuilder output = new StringBuilder();
char[] characters = input.ToCharArray();
for (int i = 0; i < characters.Length; i++)
{
if (i % 2 == 0)
{
if ((i + 1) < characters.Length)
{
output.Append(characters[i + 1]);
}
output.Append(characters[i]);
}
}
Console.WriteLine(output);
Console.ReadLine();
}
remove duplicate characters from string.
void removeDuplicate()
{
string value1 = RemoveDuplicateChars("Devarajan");
}
static string RemoveDuplicateChars(string key)
{
string table = "";
string result = "";
foreach (char value in key)
{
if (table.IndexOf(value) == -1)
{
table += value;
result += value;
}
}
return result;
}
Number of occurrences in a given string
static void Main(string[] args)
{
string input = "ABCA";
while (input.Length > 0)
{
Console.Write(input[0] + " : ");
int count = 0;
for (int j = 0; j < input.Length; j++)
{
if (input[0] == input[j])
{
count++;
}
}
Console.WriteLine(count);
input = input.Replace(input[0].ToString(), string.Empty);
}
Console.ReadLine();
}
//O/P
//A-2
//B-1
//C-1