Answer to Question #60001 in C# for josa

Question #60001
1) Receive a number and determine whether it is odd or even.
2) Obtain two numbers from the keyboard, and determine and display which (if either) is the larger of the two numbers.
3) Receive 3 numbers and display them in ascending order from smallest to largest
4) Add the numbers from 1 to 100 and display the sum
5) Add the even numbers between 0 and any positive integer number given by the user.
6) Find the average of two numbers given by the user.
7) Find the average, maximum, minimum, and sum of three numbers given by the user.
1
Expert's answer
2016-05-20T06:27:19-0400
// 1) Receive a number and determine whether it is odd or even.
Console.Write("Enter a number : ");
int a = Convert.ToInt32(Console.ReadLine());
// odd check
if ((a % 2) == 0)
Console.WriteLine(a.ToString() + " - even number");
else
Console.WriteLine(a.ToString() + " - odd number");
// 2) Obtain two numbers from the keyboard, and determine and display which (if either) is the larger of the two numbers.
Console.Write("Enter number 1 : ");
int n1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter number 2 : ");
int n2 = Convert.ToInt32(Console.ReadLine());
// comparison
if (n1 > n2)
Console.WriteLine("{0} is greater than {1} ", n1, n2);
else if (n1 < n2)
Console.WriteLine("{0} is less than {1} ", n1, n2);
else
Console.WriteLine("{0} is equal to {1} ", n1, n2);
//3) Receive 3 numbers and display them in ascending order from smallest to largest
int[] mas = new int[3];
for (int i = 0; i < 3; i++)
{
Console.Write("Enter number " + (i + 1).ToString() + " : ");
mas[i] = Convert.ToInt32(Console.ReadLine());
}
// sorting
Array.Sort(mas);
Console.Write("Numbers in ascending order : ");
foreach (int number in mas)
Console.Write(number.ToString() + " ");
//4) Add the numbers from 1 to 100 and display the sum
int sum = 0;
for (int i = 1; i <= 100; i++)
sum += i;
Console.WriteLine("Sum of numbers from 1 to 100 : " + sum.ToString());
//5) Add the even numbers between 0 and any positive integer number given by the user.
Console.Write("Enter any positive integer number : ");
int max = Convert.ToInt32(Console.ReadLine());
int sum = 0;
for (int i = 0; i < max; i = i + 2)
sum += i;
Console.WriteLine("Sum of even numbers between 0 and {0} : {1}", max, sum);

//6) Find the average of two numbers given by the user.
Console.Write("Enter number 1 : ");
double n1 = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter number 2 : ");
double n2 = Convert.ToDouble(Console.ReadLine());
double average = (n1 + n2) / 2;
Console.WriteLine("Average of {0} and {1} : {2} ", n1, n2, average);
//7) Find the average, maximum, minimum, and sum of three numbers given by the user.
double[] mas = new double[3];
for (int i = 0; i < 3; i++)
{
Console.Write("Enter number " + (i + 1).ToString() + " : ");
mas[i] = Convert.ToDouble(Console.ReadLine());
}
double max = mas.Max();
double min = mas.Min();
double sum = mas.Sum();
double average = mas.Average();
Console.WriteLine("Average = {0} ; maximum = {1} ; minimum = {2} ; sum = {3} ", average, max, min, sum);

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS