cls
# Odd and Even numbers script
# Gives Sum of odd, then even numbers and counts them, then compares.
# The result come in form of one line with headers.
$InputFile = "D:\SCRIPT\input-output\input.txt"
$OutputFileOdd = "D:\SCRIPT\input-output\OddArray.csv"
$OutputFileEven = "D:\SCRIPT\input-output\EvenArray.csv"
$OutputFileArray = "D:\SCRIPT\input-output\NumbersArray.csv"
$Content = Get-Content $InputFile
# The following line is for creating an empty array.
# @()
# The following line is for creating array of numbers in place.
# [int[]] $NumbersArray = 0 .. 10
# Following is zeroing of variables.
$Flag = 0
$OddCount = 0
$OddSum = 0
$EvenCount = 0
$EvenSum = 0
$OddArrayCount = 0
$EvenArrayCount = 0
$Sum = 0
$Count = 0
# This array is usable in case of creating multi-line Excel but it's not our case.
# $CSVArray = New-Object System.Collections.ArrayList
# The following lines create arrays for odd and even numbers.
$OddArray = New-Object System.Collections.ArrayList
$EvenArray = New-Object System.Collections.ArrayList
# The cycle finds the remainder that help us to understand if the number is odd or even.
# Then odd and even arrays are filled up with odd and even numbers.
ForEach ($Number in $NumbersArray)
{
If ($Number -ne 0)
{
$Remainder = $Number % 2
If ($Remainder -eq 0) { $Flag = "Even" } Else { $Flag = "Odd" }
If ($Flag -eq "Odd")
{
$OddArrayNumber = New-Object -Type PSObject
$OddArrayNumber | Add-Member -MemberType NoteProperty -Name "Odd" -Value "$Number"
$OddArray.Add($OddArrayNumber)
$OddCount = $OddCount + 1
$OddSum = $OddSum + $Number
}
If ($Flag -eq "Even")
{
$EvenArrayNumber = New-Object -Type PSObject
$EvenArrayNumber | Add-Member -MemberType NoteProperty -Name "Even" -Value "$Number"
$EvenArray.Add($EvenArrayNumber)
$EvenCount = $EvenCount + 1
$EvenSum = $EvenSum + $Number
}
$Count = $Count + 1
$Sum = $Sum + $Number
}
$Flag = 0
}
$OddArray | Export-Csv $OutputFileOdd -NoTypeInformation
$EvenArray | Export-Csv $OutputFileEven -NoTypeInformation
$OddArrayCount = $OddArray.Count
$EvenArrayCount = $EvenArray.Count
# New object $ArrayLine is created in every loop for Excel line.
# It fills up with count and sum for odd and even numbers.
$ArrayLine = New-Object -Type PSObject
$ArrayLine | Add-Member -MemberType NoteProperty -Name "OddArrayCount" -Value "$OddArrayCount" -Force
$ArrayLine | Add-Member -MemberType NoteProperty -Name "OddCount" -Value "$OddCount" -Force
$ArrayLine | Add-Member -MemberType NoteProperty -Name "OddSum" -Value "$OddSum" -Force
$ArrayLine | Add-Member -MemberType NoteProperty -Name "EvenArrayCount" -Value "$EvenArrayCount" -Force
$ArrayLine | Add-Member -MemberType NoteProperty -Name "EvenCount" -Value "$EvenCount" -Force
$ArrayLine | Add-Member -MemberType NoteProperty -Name "EvenSum" -Value "$EvenSum" -Force
$ArrayLine | Add-Member -MemberType NoteProperty -Name "SUM" -Value "$SUM" -Force
$ArrayLine | Add-Member -MemberType NoteProperty -Name "COUNT" -Value "$COUNT" -Force
# Adding values to two-dimensional array for output to Excel.
# $CSVArray.Add($ArrayLine) | Out-Null
$ArrayLine | Export-Csv $OutputFileArray -NoTypeInformation
Write-Host Flag = $Flag
Write-Host OddCount = $OddCount
Write-Host OddSum = $OddSum
Write-Host EvenCount = $EvenCount
Write-Host EvenSum = $EvenSum
Write-Host OddArrayCount = $OddArrayCount
Write-Host EvenArrayCount = $EvenArrayCount
Write-Host General SUM = $SUM
Write-Host General COUNT = $COUNT
Write-Host "The job is Done"
Write-Host ""
# Odd and Even numbers script
# Gives Sum of odd, then even numbers and counts them, then compares.
# The result come in form of one line with headers.
$InputFile = "D:\SCRIPT\input-output\input.txt"
$OutputFileOdd = "D:\SCRIPT\input-output\OddArray.csv"
$OutputFileEven = "D:\SCRIPT\input-output\EvenArray.csv"
$OutputFileArray = "D:\SCRIPT\input-output\NumbersArray.csv"
$Content = Get-Content $InputFile
# The following line is for creating an empty array.
# @()
# The following line is for creating array of numbers in place.
# [int[]] $NumbersArray = 0 .. 10
# Following is zeroing of variables.
$Flag = 0
$OddCount = 0
$OddSum = 0
$EvenCount = 0
$EvenSum = 0
$OddArrayCount = 0
$EvenArrayCount = 0
$Sum = 0
$Count = 0
# This array is usable in case of creating multi-line Excel but it's not our case.
# $CSVArray = New-Object System.Collections.ArrayList
# The following lines create arrays for odd and even numbers.
$OddArray = New-Object System.Collections.ArrayList
$EvenArray = New-Object System.Collections.ArrayList
# The cycle finds the remainder that help us to understand if the number is odd or even.
# Then odd and even arrays are filled up with odd and even numbers.
ForEach ($Number in $NumbersArray)
{
If ($Number -ne 0)
{
$Remainder = $Number % 2
If ($Remainder -eq 0) { $Flag = "Even" } Else { $Flag = "Odd" }
If ($Flag -eq "Odd")
{
$OddArrayNumber = New-Object -Type PSObject
$OddArrayNumber | Add-Member -MemberType NoteProperty -Name "Odd" -Value "$Number"
$OddArray.Add($OddArrayNumber)
$OddCount = $OddCount + 1
$OddSum = $OddSum + $Number
}
If ($Flag -eq "Even")
{
$EvenArrayNumber = New-Object -Type PSObject
$EvenArrayNumber | Add-Member -MemberType NoteProperty -Name "Even" -Value "$Number"
$EvenArray.Add($EvenArrayNumber)
$EvenCount = $EvenCount + 1
$EvenSum = $EvenSum + $Number
}
$Count = $Count + 1
$Sum = $Sum + $Number
}
$Flag = 0
}
$OddArray | Export-Csv $OutputFileOdd -NoTypeInformation
$EvenArray | Export-Csv $OutputFileEven -NoTypeInformation
$OddArrayCount = $OddArray.Count
$EvenArrayCount = $EvenArray.Count
# New object $ArrayLine is created in every loop for Excel line.
# It fills up with count and sum for odd and even numbers.
$ArrayLine = New-Object -Type PSObject
$ArrayLine | Add-Member -MemberType NoteProperty -Name "OddArrayCount" -Value "$OddArrayCount" -Force
$ArrayLine | Add-Member -MemberType NoteProperty -Name "OddCount" -Value "$OddCount" -Force
$ArrayLine | Add-Member -MemberType NoteProperty -Name "OddSum" -Value "$OddSum" -Force
$ArrayLine | Add-Member -MemberType NoteProperty -Name "EvenArrayCount" -Value "$EvenArrayCount" -Force
$ArrayLine | Add-Member -MemberType NoteProperty -Name "EvenCount" -Value "$EvenCount" -Force
$ArrayLine | Add-Member -MemberType NoteProperty -Name "EvenSum" -Value "$EvenSum" -Force
$ArrayLine | Add-Member -MemberType NoteProperty -Name "SUM" -Value "$SUM" -Force
$ArrayLine | Add-Member -MemberType NoteProperty -Name "COUNT" -Value "$COUNT" -Force
# Adding values to two-dimensional array for output to Excel.
# $CSVArray.Add($ArrayLine) | Out-Null
$ArrayLine | Export-Csv $OutputFileArray -NoTypeInformation
Write-Host Flag = $Flag
Write-Host OddCount = $OddCount
Write-Host OddSum = $OddSum
Write-Host EvenCount = $EvenCount
Write-Host EvenSum = $EvenSum
Write-Host OddArrayCount = $OddArrayCount
Write-Host EvenArrayCount = $EvenArrayCount
Write-Host General SUM = $SUM
Write-Host General COUNT = $COUNT
Write-Host "The job is Done"
Write-Host ""