50 likes | 395 Views
. 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 <
E N D
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;
}