Answer to Question #340508 in UNIX/Linux Programming for Raphael Caamano

Question #340508

You need to submit the code and program output.

 

Q1. you will write a C++ program that uses a for statement to calculate and display the sum of all even numbers between 5 and 24 inclusive on the screen.


Q2. Write statements that assign random integers to the variable n in the following ranges: Use the random function to generate the numbers. Put the statements in a C++ Program and run it.


a) 1 ≤ n ≤ 87 


b) 1 ≤ n ≤ 925 


c) 0 ≤ n ≤ 424 


d) 2000 ≤ n ≤ 3138


e) –16 ≤ n ≤ 65




1
Expert's answer
2022-05-13T17:02:01-0400

Q1 program:

#include <iostream>




int main(int argc, char **argv)
{
	int sum = 0;
	for (int n = 5; n <= 24; n++)
	{
		if (n % 2 == 0)
		{
			sum += n;
		}
	}
	std::cout << sum << std::endl;


	return 0;
}

Q1 output: 150


Q2 code:

#include <iostream>
#include <stdlib.h>




int main(int argc, char **argv)


{
	int n;
	n = random() % 87 + 1;
	std::cout << "a) " << n << std::endl;
	n = random() % 925 + 1;
	std::cout << "b) " << n << std::endl;
	n = random() % 425;
	std::cout << "c) " << n << std::endl;
	n = random() % 1139 + 2000;
	std::cout << "d) " << n << std::endl;
	n = random() % 82 - 16;
	std::cout << "e) " << n << std::endl;


	return 0;
}


Q2 output:

a) 38

b) 887

c) 77

d) 3122

e) -9


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS