Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C# Sharp Exercises: Remove all the values except integer values from a given array of mixed values

C# Sharp Basic: Exercise-91 with Solution

Write a C# Sharp program to remove all the values except integer values from a given array of mixed values.

Sample Solution:

C# Sharp Code:

using System;
using System.Linq;
namespace exercises
{
    class Program
    {
        static void Main(string[] args)
        {
            object[] mixedArray = new object[6];
            mixedArray[0] = 25;
            mixedArray[1] = "Anna";
            mixedArray[2] = false;
            mixedArray[3] = System.DateTime.Now;
            mixedArray[4] = -112;
            mixedArray[5] = -34.67;
            Console.WriteLine("Original array elements:");
            for (int i = 0; i < mixedArray.Length; i++)
            {
                Console.Write(mixedArray[i]+" ");
            }
            int[] new_nums = test(mixedArray);
            Console.WriteLine("\n\nAfter removing all the values except integer values from the said array of mixed values:");
            for (int i = 0; i < new_nums.Length; i++)
            {
                Console.Write(new_nums[i]+" ");
            }
        }
        public static int[] test(object[] nums)
        {
            return nums.OfType<int>().ToArray();
        }
    }
}

Sample Output:

Original array elements:
25 Anna False 4/24/2021 11:43:11 AM -112 -34.67 

After removing all the values except integer values from the said array of mixed values:
25 -112 

Flowchart:

Flowchart: C# Sharp Exercises - Remove all the values except integer values from a given array of mixed values.

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a C# Sharp program to count number of ones and zeros in the binary representation of a given integer.
Next: Write a C# Sharp program to find the next prime number of a given number. If the given number is a prime number, return the number.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.