Answer for Worksheet
Question number 1
// receive a number and determine whether it is odd or even.
using System;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
//Todetermine whether it is odd or even
int num;
[Link]("Enter Number");
num = Convert.ToInt32([Link]());
if (num % 2 == 0)
{
[Link]("Even Number");
}
else
{
[Link]("Odd Number");
}
[Link]();
}
}
}
Question number 2
// obtain two numbers from the keyboard, and determine and
display which (if either) is the larger of the two numbers.
using System;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
//To determine and display which is the larger of the two numbers
int num1,num2;
[Link]("Enter Number");
num1 = Convert.ToInt32([Link]());
num2 = Convert.ToInt32([Link]());
if (num1 > num2)
{
[Link]("{0} is largerst number", num1);
}
else if (num2>num1)
{
[Link]("{0} is largerst number", num2);
}
[Link]();
}
}
}
Question number 3
using System;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
// display ascending order from smallest to largest
int num1,num2,num3;
[Link]("Enter First Number");
num1 = Convert.ToInt32([Link]());
[Link]("Enter second Number");
num2 = Convert.ToInt32([Link]());
[Link]("Enter Third Number");
num3 = Convert.ToInt32([Link]());
if ((num1 > num2) && (num1 > num3))
{
if (num2 > num3)
{
[Link]("{0},{1},{2}",num3, num2, num1);
}
else
{
[Link]("{0},{1},{2}", num2, num3, num1);
}
}
else if ((num2 > num1) && (num2 > num3))
{
if (num1 > num3)
{
[Link]("{0},{1},{2}", num3, num1, num2);
}
else
{
[Link]("{0},{1},{2}", num1, num3, num2);
}
}
else if ((num3 > num1) && (num3 > num2)) {
if (num1 > num2)
{
[Link]("{0},{1},{2}" ,num2, num1,num3);
}
else {
[Link]("{0},{1},{2}", num1, num2, num3);
}
}
[Link]();
}
}
}
Question number 4
// add the numbers from 1 to 100 and display the sum
Question number 5
// add the even numbers between 0 and any positive integer
number given by the user.
using System;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int sum = 0;
[Link]("Enter Number");
int a = Convert.ToInt32([Link]());
for (int i = 0; i <= a; i++)
{
if (i % 2 == 0)
{
sum += i;
}
}
[Link]("sum of number is {0}", sum);
[Link]();
}
}}
Question number 6
// find the average of two numbers given by the user.
using System;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
double average;
[Link]("Enter First Number");
double num1 = [Link]([Link]());
[Link]("Enter second Number");
double num2 = [Link]([Link]());
average = (num1 + num2) / 2;
[Link]("average of two number {0}",average);
[Link]();
}
}
}
Question number 7
using System;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
double sum, average;
[Link]("Enter First Number");
double num1 = [Link]([Link]());
[Link]("Enter second Number");
double num2 = [Link]([Link]());
[Link]("Enter Third Number");
double num3 = [Link]([Link]());
if ((num1 > num2) && (num1 > num3)) {
if (num2 > num3)
{
[Link]("Minimum number {0}", num3);
}
else {
[Link]("Minimum number {0}", num2);
}
[Link]("Maximum number {0}", num1);
} else if ((num2>num1)&&(num2>num3)) {
if (num1 > num3)
{
[Link]("Minimum number {0}", num3);
}
else
{
[Link]("Minimum number {0}", num1);
}
[Link]("Maximum number {0}", num2);
}
else if ((num3 > num1) && (num3 > num1))
{
if (num1 > num2)
{
[Link]("Minimum number {0}", num2);
}
else
{
[Link]("Minimum number {0}", num1);
}
[Link]("Maximum number {0}", num3);
}
sum = num1 + num2 + num3;
average = (num1 + num2+num3) / 3;
[Link]("average of three numbers {0}",average);
[Link]("Sum of three numbers {0}", sum);
[Link]();
}
}
}
Question number 9
using System;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int a = 10;
int b = 20;
//before swap
[Link]("Value of a is {0}", a);
[Link]("Value of b is {0}", b);
int temp = a;
a = b;
b = temp;
//after swap
[Link]("after swap ");
[Link]("Value of a is {0}", a);
[Link]("Value of b is {0}", b);
[Link]();
}
}
}