What do the following program segments print? Find the answers by tracing the code, not by using the computer.
- a. int n = 1;
for (int i = 2; i < 5; i++) { n = n + i; }
System.out.print(n);
- b. int i;
double n = 1 / 2;
for (i = 2; i <= 5; i++) { n = n + 1.0 / i; }
System.out.print(i);
- c. double x = 1;
double y = 1;
int i = 0;
do
{
y = y /2;
x = x + y;
i++;
}
while (x < 1.8);
System.out.print(i);
- d. double x = 1;
double y = 1;
int i = 0;
while (y >= 1.5)
{
x = x / 2;
y = x + y;
i++;
}
System.out.print(i);
Want to see the full answer?
Check out a sample textbook solutionChapter 4 Solutions
BIG JAVA: LATE OBJECTS
Additional Engineering Textbook Solutions
C How to Program (8th Edition)
Starting Out with Java: From Control Structures through Objects (6th Edition)
Starting Out With Visual Basic (8th Edition)
Artificial Intelligence: A Modern Approach
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Starting Out With Visual Basic (7th Edition)
- int toplam = 0;for (int i = 0; i < 10; i++){if (i % 2 == 1) continue;for (int j = 1; j <= 5; j++){if (i * j > 30) continue;toplam += j;}} What will be the total number of iterations when the code runs?arrow_forwardQuestion #4: Re-write the program of Question #3 using data files. The input data numbers.txt contains (positive) integer numbers. Each time the program reads a number, check whether it is a prime and stores the result in the output file Results.txt. 373 373 is Prime!! 11 11 is Prime!! 2552 is NOT prime!! 5 is Prime!! 3561 is NOT prime!! 2552 3561 numbers.txt Results.txtarrow_forwardDescription A mathmatician Goldbach's conjecture: any even number(larger than 2) can divide into two prime number’s sum.But some even numbers can divide into many pairs of two prime numbers’ sum. Example:10 =3+7, 10=5+5, 10 can divide into two pairs of two prime number. Input Input consist a positive even number n(4<=n<=32766). Output Print the value of how many pairs are this even number can be divided into. Sample Input 1 1234 Sample Output 1 25arrow_forward
- System.out.println (x); for (int i - 0; i < x.length System.out.println (x); for (int i = 1; i < x.lengh; i++) System.out.println (x[i]); for (int i -0; i < x.length; i++) System.out.println (x[i]); None of the above 3. int [] x new int[6]; To print all array elements we use: a. b. 1; i++) C. d. e.arrow_forwardint funcB(int); int funcA(int n) { if (n 4) { return n* funcA(n - 5); } else { return n- funcB(n - 1); int main() { cout << funcA(13); return 0; What is the output of this program? Please show our work.arrow_forward#include int k=0; int f(int t) { k += t; heturn k; } void main() { int i, result=1; for(i = 1;i <= 3;i++) result = f(i); printf("%d\n", result); } A 3 B C) 1 D 123arrow_forward
- #include double f(double x) { return 4 * exp(-x); } double trapezoidalRule(double a, double b, int N) { double h = (b - a) / N; double sum = 0.5 * (f(a) + f(b)); for (int i = 1; i < N; i++) { sum += f(a + i * h); } return h * sum; } int main() { double a = 0.0; // lower limit double b = 1.0; // upper limit int N = 5; // number of trapezoids double integral = trapezoidalRule(a, b, N); std::cout << "The estimate of the integral of f(x) = 4e^-x between " << a << " and " << b << " using " << N << " trapezoids is: " << integral << std::endl; return 0; } Please write down the explanation, step by step for this trapezoidal rule program, Emphasize on this part:for (int i = 1; i < N; i++) { sum += f(a + i * h); }arrow_forward%3B for ( int i = 0; i < n; ++i ) { for ( int j= 0; j< n; ++j) { sum += f( j, 10 ); } } iii. int sum = 0; %3D Assume f(n, m) runs in O( n + m) time.arrow_forwardcode in java pls Summary: Given integer values for red, green, and blue, subtract the gray from each value. Computers represent color by combining the sub-colors red, green, and blue (rgb). Each sub-color's value can range from 0 to 255. Thus (255, 0, 0) is bright red, (130, 0, 130) is a medium purple, (0, 0, 0) is black, (255, 255, 255) is white, and (40, 40, 40) is a dark gray. (130, 50, 130) is a faded purple, due to the (50, 50, 50) gray part. (In other words, equal amounts of red, green, blue yield gray). Given values for red, green, and blue, remove the gray part. Ex: If the input is: 130 50 130 the output is: 80 0 80 Find the smallest value, and then subtract it from all three values, thus removing the gray.arrow_forward
- Python quesarrow_forwardNeed help in python. Problem: 2D random walk. A two dimensional random walk simulates the behavior of a particle moving in a grid of points. At each step, the random walker moves north, south, east, or west with probability 1/4, independently of previous moves. Compose a program that takes a command-line argument n and estimates how long it will take a random walker to hit the boundary of a 2n+1-by-2n+1 square centered at the starting point. //Given codeImport stdioImport randomImport sysn = int(sys.argv[1])//write code herestdio.write('The walker took ')stdio.write(c)stdio.writeln(' steps')arrow_forwardFind an error in the following code and fix it. int x = 23;int* p = x; Group of answer choices int* x = 23;int* p = x; int x = 23;int& p = *x; int x = 23;int* p = *x; int x = 23;int* p = &x;arrow_forward
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr