C# Create a console application that prompts you to enter three unique integers
ID: 3733654 • Letter: C
Question
C#
Create a console application that prompts you to enter three unique integers between 0 and 10 inclusive (0 and 10 are allowed). You must ensure that these numbers are all unique and that they must be between 0 and 10. That is, if any of these numbers is not valid, the application will keep on prompting the user to enter the correct values. Finally, display a simple chart showing the positions of these numbers so that it looks like the figure (note that 5, 6, and 10 are the three inputs in this example).
0 1 2 3 4 5 6 7 8 9 10
* * *
|-+-+-+-+-|-+-+-+-+-|-
Explanation / Answer
using System;
using System.IO;
public class Program
{
public static void Main()
{
int count=0,flag=0,Number;
int[] array= new int[3];
string str=String.Empty;
while(count<=2)
{
Console.WriteLine(" Enter any number from 0 to 10:-");
Number = int.Parse(Console.ReadLine());
if(Number>=0 && Number<=10)
{
if(count>0)
{
for(int i=0;i<count;i++)
{
if(array[i]==Number)
{
flag=1;
break;
}
}
}
if(flag==0)
{
array[count]=Number;
count++;
}
}
flag=0;
}
Console.WriteLine("0 1 2 3 4 5 6 7 8 9 10");
Console.WriteLine(" ");
count=0;
Array.Sort(array);
for(int i=0;i<=10;i++)
{
if(array[count]==i)
{
str=str+"* ";
count++;
}
else
{
str=str+" ";
}
if(count==3)
{
break;
}
}
Console.WriteLine("{0}",str);
Console.WriteLine(" ");
Console.WriteLine("|-+-+-+-+-|-+-+-+-+-|-");
Console.ReadLine();
}
}
Description:-The program given above will give you the desired output.Starting from the while loop,which takes the count of valid intigers place inside the array.after that i have printed the chart from 0 to 10 and showing the * at exactly the given number.i have checked for the dublicate number logic inside the while loop.when first number is entered,we dont neet to compare.so i have written the condition if(count>0).for loops reused to buid the string.for your clearance,create a memorry graph to see the flow.Thanks
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.