Write a program that allows the user to enter an integer n a

Write a program that allows the user to enter an integer n, and then generates n random numbers between 0 and 100. The program calculates and prints the sums for even and odd numbers. Draw the flowchart for the program on the hack of this paper. Open Text Pad and save the source tile on the desktop using the file name as lastname_final Write your name on the first line of the source file, and short comment for each task in the program. Leave the Text Pad open and this paper on the keyboard when you finish the exam. Here is an example of output screen:

Solution

MATLAB code:

clear all
close all
clc
n=input(\'Enter an integer -> \');
rn = randi([0 100],1,n);
j=1;
k=1;
for i=1:n
if mod(rn(i),2)==0
ev(j)= rn(i);
j=j+1;
else
od(k)=rn(i);
k=k+1;
end
end
  
  
disp(\'The random numbers are:\')
disp(rn)
disp(\'The Even numbers are:\')
disp(ev())
fprintf(\'The sum of even numbers is : %d\ \',sum(ev()))
disp(\'The Odd numbers are:\')
disp(od())
fprintf(\'The sum of odd numbers is : %d\ \',sum(od()))

sample output is,

Enter an integer -> 25
The random numbers are:
Columns 1 through 13

91 89 33 70 19 3 75 50 48 91 61 62 86

Columns 14 through 25

81 58 18 24 89 2 49 16 98 71 50 47

The Even numbers are:
70 50 48 62 86 58 18 24 2 16 98 50

The sum of even numbers is : 582
The Odd numbers are:
91 89 33 19 3 75 91 61 81 89 49 71 47

The sum of odd numbers is : 799
>>

 Write a program that allows the user to enter an integer n, and then generates n random numbers between 0 and 100. The program calculates and prints the sums f

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site