1 / 5

1. What does the following segment of code print out on the screen int x,y,n; n 15; x1; whilexn x4; cout x x

. 2. What does the following segment of code print out on the screen? int x,y,n; n=5; for(x=1; x==n; x ) //for(y=x; y<=x; y =3)cout << "x=" << x << " y=" << y << endl;. Answer nothing . . Look at the following code, and determine what value is printed int trace1=0, trace2=0; for (j=0; j <

hina
Download Presentation

1. What does the following segment of code print out on the screen int x,y,n; n 15; x1; whilexn x4; cout x x

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


    1. 1. What does the following segment of code print out on the screen? int x,y,n; n = 15; x=1; while(x<=n) x+=4; cout << "x=" << x << " y=" << y << endl;

    2. 2. What does the following segment of code print out on the screen? int x,y,n; n=5; for(x=1; x==n; x++) //for(y=x; y<=x; y+=3) cout << "x=" << x << " y=" << y << endl;

    3. Look at the following code, and determine what value is printed int trace1=0, trace2=0; for (j=0; j < 4; j = j+1) { for (k=0; k < 2; k = k+1) { trace1++; } trace2++; } printf ("%d", trace1+trace2); ... A. 4 B. 6 C. 8 D. 12 15 ANSWER: D

    4. The greatest common divisor (GCD) of two integers is the largest integer that evenly divides each of the numbers. Write a function gcd that returns the greatest common divisor of two integers. Answer: /* Greatest Common Divisor GCD * * Hussam eddin Najib November 2008 */ #include <iostream.h> void main() { int n1,n2,i; cout << "Enter two numbers? "; cin >> n1 >> n2; // find the smallest of the two numbers int start = (n1<n2 ? n1:n2); // start from the smallest of the two numbers and work down to 1 for(i=start; i>1; i--) if((n1%i == 0) && (n2%i == 0)) break; cout << "GCD of " << n1 << " and " << n2 << " is " << i << endl; }

More Related