1)Write a C program to swap two
variables without using a temporary variable?
Swap the
value with or without temp
A=10 , B=20
T=A Give 10 to T
A=B Give 20 to A
B=T Give 10 to B
A=A+B A=30
B=A-B B=20
A=A-B A=10
So the moral
of the story is, dont try to be smart when writing code to swap variables. Use
a temporary variable. It’s not only fool proof, but also easier to understand
and maintain
2)Two method
for factorial?
1)With
recursion
Console.WriteLine("Enter a number");
int number
= Convert.ToInt32(Console.ReadLine());
long fact =
GetFactorial(number);
Console.WriteLine("{0} factorial is {1}", number,
fact);
Console.ReadKey();
private static long GetFactorial(int number)
{
if (number
== 0)
{
return 1;
}
return number
* GetFactorial(number-1);
}
2)Without
recursion
int
i, number, fact;
Console.WriteLine("Enter the
Number");
number = int.Parse(Console.ReadLine());
fact = number;
for (i = number - 1; i >= 1; i--)
{
fact = fact * i;
}
Console.WriteLine("\nFactorial of Given Number is: "+fact);
Console.ReadLine();
3)Find largest number?
int max = 0;
List<int> iList = new List<int>();
iList.Add(100);
iList.Add(10);
iList.Add(200);
iList.Add(35);
iList.Add(25);
foreach (int i in iList)
{
if (i > max)
max = i;
}
4)Generate Random Password?
string allowedChars = "";
allowedChars += "1,2,3,4,5,A,B,C,D,E";
char[]
sep = { ',' };
string[]
arr = allowedChars.Split(sep);
string passwordString = "";
string temp = "";
Random rand = new Random();
for (int i = 0; i < 8; i++)
{
temp =
arr[rand.Next(0, arr.Length)];
passwordString += temp;
}
string result = passwordString;
5)Check Prime
Numbers
Console.WriteLine("Enter
a number");
int number = Convert.ToInt32(Console.ReadLine());
int result = Check_Prime(number);
if (result == 0)
{
Console.WriteLine("{0} is not a prime number", number);
}
else
{
Console.WriteLine("{0} is a prime number", number);
}
Console.Read();
int number = Convert.ToInt32(Console.ReadLine());
int result = Check_Prime(number);
if (result == 0)
{
Console.WriteLine("{0} is not a prime number", number);
}
else
{
Console.WriteLine("{0} is a prime number", number);
}
Console.Read();
private static int Check_Prime(int number)
{
int i;
for (i = 2; i <= number - 1; i++)
{
if (number % i == 0)
{
return 0;
}
}
if (i == number)
{
return 1;
}
return 0;
}
{
int i;
for (i = 2; i <= number - 1; i++)
{
if (number % i == 0)
{
return 0;
}
}
if (i == number)
{
return 1;
}
return 0;
}
6)Reverse Integer Number?
Console.WriteLine("Enter a Integer Number");
int number = Convert.ToInt32(Console.ReadLine());
int length = number.ToString().Length;
string reverseNumber = string.Empty;
for (int i = 0; i < length; i++)
{
reverseNumber += (number % 10).ToString();
number = number /
10;
}
Console.WriteLine(reverseNumber);
Console.Read();
7)Duplicate Count in List of numbers?
int count=0;
int[] data = { 1, 2, 3, 4, 5, 6, 7, 8, 5, 9, 8, 10, 11, 12, 10, 13,
14, 15, 12, 16, 17, 18, 15, 19, 20 };
var res =
data.GroupBy(t => t);
foreach(var t in res)
{
if (t.Count() > 1)
{
Console.WriteLine("The number
" + t.Key + " repeated " + t.Count() + " times");
}
}
8)Pyramid like this
*
***
*****
*******
*********
***
*****
*******
*********
int numberoflayer = 5, Space, Number;
Console.WriteLine("paramid");
for (int i = 1; i <= numberoflayer; i++) // Total number of layer for pramid
{
for (Space = 1; Space <= (numberoflayer - i);
Space++) //
Loop For Space
Console.Write(" ");
for (Number = 1; Number <= i; Number++) //increase the value
Console.Write("*");
for (Number = (i - 1); Number >= 1; Number--) //decrease
the value
Console.Write("*");
Console.WriteLine();
}
Console.ReadKey();
9)Simple Calculator
int num1;
int num2;
string operand;
float answer;
Console.Write("Please enter
the first number: ");
num1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Please enter
an operand (+, -, /, *): ");
operand = Console.ReadLine();
Console.Write("Please enter
the second number: ");
num2 = Convert.ToInt32(Console.ReadLine());
switch (operand)
{
case "-":
answer = num1 - num2;
break;
case "+":
answer = num1 + num2;
break;
case "/":
answer = num1 / num2;
break;
case "*":
answer = num1 * num2;
break;
default:
answer = 0;
break;
}
Console.WriteLine(num1.ToString() + " " +
operand + " " + num2.ToString() + "
= " + answer.ToString());
Console.ReadLine();
10)Check if string palindrom or not
string s, revs = "";
Console.WriteLine(" Enter
string");
s = Console.ReadLine();
for (int i = s.Length - 1; i >= 0; i--) //String Reverse
{
revs += s[i].ToString();
}
if (revs == s) //
Checking whether string is palindrome or not
{
Console.WriteLine("String is
Palindrome \n Entered String Was {0} and reverse string is {1}", s, revs);
}
else
{
Console.WriteLine("String is
not Palindrome \n Entered String Was {0} and reverse string is {1}", s, revs);
}
Console.ReadKey();
No comments:
Post a Comment