Answers To Exercises In Getting Started With .NET 4.5 and C# 5.0
1. Accept inches from user and display the equivalent centimeters
using System;
namespace st
{
class InchToCm
{
public static void Main()
{
Console.Write("Enter Inches :");
string input = Console.ReadLine();
int inches = Int32.Parse(input);
Console.WriteLine("There are {0} centimeters for {1} inches",
inches * 2.5, inches);
} // main
} // class
} // namespace
2. Write a program to take a number and display highest factor for that number
using System;
namespace st
{
class HighestFactor
{
static void Main()
{
Console.Write("Enter a number :");
string input = Console.ReadLine();
int num = Int32.Parse(input);
for (int i = num / 2; i > 0; i--)
{
if (num % i == 0)
{
Console.WriteLine("Highest factor for {0} is {1}", num, i);
break;
}
} // for
} // main
} // class
} // namespace
3. Write a program to take a series of numbers from user and display the highest of all the numbers
using System;
namespace st
{
class Highest
{
static void Main()
{
int highest = 0;
while (true)
{
Console.Write("Enter a number :");
string input = Console.ReadLine();
int num = Int32.Parse(input);
// stop if user's input is 0
if (num == 0)
break;
if (num > highest)
highest = num;
}
Console.WriteLine("Highest of all numbers is : {0}", highest);
} // main
} // class
} // st
4. Create a function that takes an array and returns the first two highest numbers
using System;
namespace st
{
class TwoHighest
{
public static void Main()
{
int fh, sh;
int[] a = { 10, 30, 40, 22, 55 };
FirstTwoHighest(a, out fh, out sh);
Console.WriteLine("First Highest {0}, Second Highest : {1}", fh, sh);
}
public static void FirstTwoHighest(int[] a, out int fh, out int sh)
{
fh = sh = 0;
foreach (int n in a)
{
if (n > fh)
{
sh = fh;
fh = n;
}
else
if ( n > sh )
sh = n;
} // foreach
} // FirstTwoHighest
} // class
} // namespace
5. Create a function that takes two numbers and returns the highest common factor
using System;
namespace st
{
class HCF
{
public static void Main()
{
Console.WriteLine(Hcf(30, 55));
Console.WriteLine(Hcf(28, 27));
}
public static int Hcf(int n1, int n2)
{
// start the loop from half of the smallest of two numbers
for (int i = n1 < n2 ? n1 / 2 : n2 / 2; i > 1; i--)
{
if (n1 % i == 0 && n2 % i == 0)
return i; // found Highest common factor
} // for
return 1; // Didn't find highest common factor, so 1 is treated as so
} // HFC
} // class
} // namespace
6. Declare an array of 10 elements. Read values from user and insert them into array. Display how many values in the array
are positive, negative and zeros
using System;
namespace st
{
public class ArrayCount
{
public static void Main()
{
int[] a = new int[10];
for (int i = 0; i < a.Length; i++)
{
Console.Write("Enter a number :");
a[i] = Int32.Parse(Console.ReadLine());
}
int nop, non, noz;
nop = non = noz = 0;
foreach (int n in a)
{
if (n > 0)
nop++;
else
if (n < 0)
non++;
else
noz++;
}
Console.WriteLine("No. of positives : {0}", nop);
Console.WriteLine("No. of negatives : {0}", non);
Console.WriteLine("No. of zeroes : {0}", noz);
} // main
} // class
} // namespace
7. Accept a number on the command line and display whether it is a strong number
using System;
namespace st
{
class StrongNumber
{
public static void Main(string[] args)
{
int num = Int32.Parse(args[0]);
int orgnum = num;
int total = 0;
while (num != 0)
{
// take a digit
int digit = num % 10;
int factorial = 1;
for (int i = 1; i <= digit; i++)
factorial *= i;
total += factorial;
// remove digit
num /= 10;
}
if (total == orgnum)
Console.WriteLine("{0} is a strong number", orgnum);
else
Console.WriteLine("{0} is not a strong number", orgnum);
} // main
} // class
} // st
8. Create a function that takes a number by value and returns smallest and largest factors of the number through pass by reference.
using System;
namespace st
{
class HighestLowestFactors
{
public static void Main()
{
int highfact, lowfact;
Console.WriteLine("Enter a number : ");
int num = Int32.Parse(Console.ReadLine());
GetHighestLowestFactors( num, out highfact, out lowfact);
Console.WriteLine(
"For number {0}, Highest Factor is {1} and Lowest Factor is : {2}",
num, highfact,lowfact );
}
public static void GetHighestLowestFactors(int num, out int hf, out int lf)
{
// get lowest factor other than 1. In case of prime number 1
lf = 1;
for (int i = 2; i <= num / 2; i++)
{
if (num % i == 0)
{
lf = i;
break;
}
}
// get highest factor other than the number. In case of prime number it is number itself
hf = num;
for (int i = num/2 ; i > 1; i--)
{
if (num % i == 0)
{
hf = i;
break;
}
} // for
} // GetHighestLowestFactors
} // class
} // namespace