write a console application that request 10 numbers from the user as individual inputs
each number must be between 10 and 10 inclusive
compare each number to the last number and determine if it is a duplicate if so ask the user for another number
output each number to the screen once all conditions are met
end the program after the loop by requesting the user to hit enter to end the program
MahaPosted Nov 12, 2014, 6:03 AM
VulpesPosted Nov 12, 2014, 5:53 AM
When it says each number must be between 10 and 10 inclusive, I assume it means that it must be between 1 and 10 inclusive.
Also it says compare each number to the last number and determine if it's a duplicate. I assume this just means the last number entered and not any previously entered numbers.
Your solution, Maha, doesn't address the first point and I think you've assumed that it shouldn't duplicate ANY of the previously entered numbers which may or may not be correct.
What I'd have done is this:
MahaPosted Nov 12, 2014, 5:08 AM
using System;
class Program
{
static void Main(string[] args)
{
int[] num = new int[10];
int i;
for (i = 0; i < num.Length; i++)
{
Console.Write("\nInput a number ");
int n = int.Parse(Console.ReadLine());
while (true)
{
if (num[0] != n && num[1] != n && num[2] != n && num[3] != n && num[4] != n && num[5] != n && num[6] != n && num[7] != n && num[8] != n && num[9] != n)
{
num[i] = n;
Console.WriteLine("\n------------------- num[{0}] = {1} -----", i, n);
break;
}
else
{
Console.WriteLine("\nNumber is " + n + " already exist. Try again");
Console.Write("\nInput a number ");
n = int.Parse(Console.ReadLine());
}
}
}
Console.WriteLine("\nEnd of the program");
Console.ReadKey();
}
}
Sibeesh VenuPosted Nov 9, 2014, 11:51 PM