Ranges of arrays program in C#
Write a
program that:
Reads 10
numbers, one by one, using a for loop. Use
switch-case statement to do following: If the number is between 1 and 5, store
it in one array, if the number is between 6 and 10, store it in a second array,
if the number is between 11 and 15 store it in a third array. In all other
cases display a message saying “The number is out of range! Enter a number
between 1 and 15”.
In the end, display the elements of each of the
three arrays with some type of heading.
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace khw
{
class Program
{
static void Main(string[] args)
{
int[] inp, arr1, arr2, arr3;
int i, i1, i2, i3;
inp = new int[10];
arr1 = new int[10];
arr2 = new int[10];
arr3 = new int[10];
i = i1 = i2
= i3 = 0;
for(;i<10;i++)
{
inp[i]=int.Parse(Console.ReadLine());
switch (inp[i])
{
case 1:
case 2:
case 3:
case 4:
case 5:
arr1[i1]=inp[i];
i1++;
break;
case 6:
case 7:
case 8:
case 9:
case 10:
arr1[i2] = inp[i];
i2++;
break;
case 11:
case 12:
case 13:
case
14:
case 15:
arr1[i3] = inp[i];
i3++;
break;
default:
Console.WriteLine("The number is out of range! Enter a number between
1 and 15");
i--;
break;
}
}
if(i1>0)
{
Console.WriteLine("array1
has following "+i1.ToString()+"
elements");
for (i = 0; i < i1; i++)
{
Console.WriteLine(arr1[i].ToString());
}
}
if (i2 > 0)
{
Console.WriteLine("array2
has following " + i2.ToString() + "
elements");
for (i = 0; i < i2; i++)
{
Console.WriteLine(arr1[i].ToString());
}
}
if (i3 > 0)
{
Console.WriteLine("
array3 has following " + i3.ToString() + "elements");
for (i = 0; i < i3; i++)
{
Console.WriteLine(arr1[i].ToString());
}
}
}
}
}
Screen Shot
OutPut
No comments:
Post a Comment