C# program to generate numbers that are multiples of 5 using the LINQ parallel query

Here, we are going to learn how to generate numbers that are multiples of 5 using the LINQ parallel query in C#?
Submitted by Nidhi, on August 20, 2020

In this program, we will generate random numbers that are multiples of 5 using LINQ parallel query, here we will put a condition to check number is multiple of 5 or not in the specified range. Here we used range 1 to 20.

Program:

The source code to generate numbers that are multiples of 5 using the LINQ parallel query in C# is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//Program to generate numbers that are 
//multiples of 5 using LINQ parallel query.

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        IEnumerable<int> Numbers = ((ParallelQuery<int>)ParallelEnumerable.Range(1,30))
        .Where(num => num%5==0)
        .Select(val => val);

        foreach (int num in Numbers) 
        {
            Console.WriteLine(num); 
        }
    }
}

Output:

5
10
20
25
15
30
Press any key to continue . . .

Explanation:

In the above program, we created a Program class that contains the Main() method.

IEnumerable Numbers = ((ParallelQuery)ParallelEnumerable.Range(1,30))
.Where(num => num%5==0)
.Select(val => val);

Here we created integer numbers collection of IEnumerable type. Here we generated numbers that are multiples of 5. Then we printed the random numbers between the range 1 to 50 on the console screen.

C# LINQ Programs »


ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.