Largest Adjuscent product of four numbers.
Program to find the adjustent 4 numbers in a list of 1000 random numbers which has the highest multiplication value when multiplied among themselves.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ManualResetEvent_
{
class Largest_product
{
int[] a;
List<Array> arr_list;
int[] b;
int count = 0;
public Largest_product()
{
a= new int[1000];
arr_list = new List<Array>();
}
//
Filling the array with random numbers
public void fill_array()
{
Random rnd =new Random();
for (int i = 0; i < 1000; i++)
{
if(i == 0)
{
a[i] = 1 * rnd.Next(1,9) %
10;
}
else
{
a[i] = i * rnd.Next(1, 9) %
10;
}
}
}
// The
processing on the array maily the algorith to get the adjuscent numbers having
maximum value of multipication
public void array_process()
{
int multiplication =1;
int count = 0;
for (int i = 0; i < 1000; i=i+4)
{
b = new int[5];
for (int j = i; j < 4+i; j++)
{
multiplication=multiplication*a[j];
b[count] = a[j];
count++;
if (count == 4)
{
b[count] =
multiplication;
}
}
arr_list.Add(b);
multiplication = 1;
count = 0;
}
}
//
Displying the array
public void display_array()
{
int j=0;
for (int i = 0; i < 1000; i++)
{
Console.Write(a[i]);
j++;
if (j == 50)
{
Console.Write("\n");
j = 0;
}
}
}
//Display
the list of numbers in the array having maximum value for multiplication
public void display_number_list()
{
int[] array_b = new int[5];
int max = 0;
foreach (int[] array in arr_list)
{
if (max < array[4])
{
max = array[4];
b = array;
}
}
Console.WriteLine(" The max
value is :" + max);
foreach (int a in b)
{
count++;
if (count < 5)
{
Console.WriteLine("The Adjustcent Number are :" + a);
}
}
}
}
class Number
{
public static void Main()
{
Largest_product lpr = new Largest_product();
lpr.fill_array();
lpr.display_array();
lpr.array_process();
lpr.display_number_list();
Console.ReadLine();
}
}
}
Comments
Post a Comment