
OPA May 8 Unix
Write a shell script to find the difference between sum of squares of Even and sum of the squares of Odd numbers in the given list.
Script should read the count of numbers to be considered as user input followed by set of numbers one by one and process the data accordingly towards the given requirement. Below sample input describes the order / type of the data, in which shell script should read the required input data to process towards the given requirement .
Hint:
Use read statement of shell script to read the input, as mentioned in sample input.
Loop through the count of input elements to read individual elements one by one , find out the number is Even or Odd and apply the relevant logic to find the difference between sum of squares of Even numbers and Odd numbers in the list of input
Sample input / Test case input format:
a. The first line represents the count of numbers considered to calculate the difference of sum of squares of Even and Odd.
b.The second line contains the 1st number .
c. The third line contains the 2nd number and so on, will be repeated till the count(count read in point#a) is reached .
Example:
If we want to provide 2, 3 4 and 5 as the input numbers , then provide the input as
4
2
3
4
5
The first line of above input represents the total number of input values to be read.
The output for this example will be as below;
-14
Output explanation :
Even numbers in the sample input are: 2 and 4 ; Sum of squares of Even numbers = 2*2 + 4*4=20
Odd numbers in the sample input are : 3 and 5 ; Sum of squares of Odd numbers = 3*3 + 5*5 =34
Sum of squares of Even numbers - Sum of squares of Odd numbers = -14 (ie.. 20-34 )
Output:
-14
Note:
If sum of Squares of Even numbers is more than the sum of squares of odd numbers, then the output will be a positive number.
​
Solutions:
​
​
#!/bin/bash
read
awk 'BEGIN{e=0;o=0} {if ($0%2==0) {e+=$0*$0} else { o+=$0*$0}} END{print (e-o)}'