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
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
Comments
Leave a comment