**Iteration**

**Language Support**

**While**

**1.**Write a statement to declare and initialize an array named denominations that contains exactly six elements of type of int.

Your declaration statement should initialize the elements of the array to the following values: 1, 5, 10, 25, 50, 100. (The value 1 goes into the first element, the value 100 to the last.)

int[] denominations = { 1 , 5 , 10 , 25 , 50 , 100 } ;

**2.**Given an int variable n that has already been declared and initialized to a positive value, use a while loop to print a single line consisting of n asterisks. Use no variables other than n.

int k = 0 ;

while(k<=(n-1))

{

System.out.printf("*") ;

k+=1 ;

}

System.out.println("") ;

**3.**Given int variables k and total that have already been declared, use a while loop to compute the sum of the squares of the first 50 counting numbers, and store this value in total. Thus your code should put 1*1 + 2*2 + 3*3 +...+ 49*49 + 50*50 into total. Use no variables other than k and total.

k = 1 ;

total = 0 ;

while ( k <= 50 )

{

total = total + ( k * k ) ;

k = k + 1 ;

}

**4.**Given an int variable n that has been initialized to a positive value and, in addition, int variables k and total that have already been declared, use a while loop to compute the sum of the cubes of the first n whole numbers, and store this value in total. Use no variables other than n, k, and total and do not modify the value of n. More...

Example:

Thus if n equals 4, your code should put 1*1*1 + 2*2*2 + 3*3*3 + 4*4*4 into total.

k = 0 ;

total = 0 ;

while ( k <= n )

{

total = total + ( k * k * k) ;

k++ ;

}

**Do_while**

**1.**Given an int variable k that has already been declared, use a do...while loop to print a single line consisting of 53 asterisks. Use no variables other than k. More...

Example:

So, when your code has finished executing 53 asterisks will have been printed on a line by itself.

k=1;

do

{

System.out.print("*");

k++;

}while(k<=53);

**2.**Given an int variable n that has already been declared and initialized to a positive value, use a do...while loop to print a single line consisting of n asterisks. Use no variables other than n.

do{

System.out.print("*") ;

n-- ;

} while(n>=1) ;

**3.**Given int variables k and total that have already been declared, use a do...while loop to compute the sum of the squares of the first 50 counting numbers, and store this value in total. Thus your code should put 1*1 + 2*2 + 3*3 +... + 49*49 + 50*50 into total. Use no variables other than k and total.

total=0;

k=1;

do{

total=total+k*k;

k++;

}while(k<=50);

**4.**Given an int variable n that has been initialized to a positive value and, in addition, int variables k and total that have already been declared, use a do...while loop to compute the sum of the cubes of the first n whole numbers, and store this value in total. Use no variables other than n, k, and total. More...

Example:

Thus if n equals 4, your code should put 1*1*1 + 2*2*2 + 3*3*3 + 4*4*4 into total.

total=0;

k=1;

do{

total=total+k*k*k;

k++;

}while(k<=n);

**For**

**1.**Given an int variable k that has already been declared, use a for loop to print a single line consisting of 97 asterisks. Use no variables other than k. More...

Example:

So, when your code has finished executing 97 asterisks will have been printed on a line by itself.

for (int xRows = 1 ; xRows <= 97 ; xRows++)

{

System.out.print("*") ;

}

**2.**Given an int variable n that has already been declared and initialized to a positive value, and another int variable j that has already been declared, use a for loop to print a single line consisting of n asterisks. Thus if n contains 5, five asterisks will be printed. Use no variables other than n and j. More...

Example:

So, if n has the value 7, 7 asterisks will have been printed on a line by itself.

for(j=1;j<=n;++j)

{

System.out.print("*");

}

3. Given int variables k and total that have already been declared, use a for loop to compute the sum of the squares of the first 50 counting numbers, and store this value in total. Thus your code should put 1*1 + 2*2 + 3*3 +... + 49*49 + 50*50 into total. Use no variables other than k and total.

total=0;

for(k=1;k<=50;++k)

{

total=total+(k*k);

}

4. Given an int variable n that has been initialized to a positive value and, in addition, int variables k and total that have already been declared, use a for loop to compute the sum of the cubes of the first n whole numbers, and store this value in total. Use no variables other than n, k, and total. More...

Example:

Thus if n equals 4, your code should put 1*1*1 + 2*2*2 + 3*3*3 + 4*4*4 into total.

total=0;

for(k=1;k<=n;++k)

{

total=total+(k*k*k);

}

**Loop_Examples**

**1.**Assume the int variables i and result have been declared but not initialized. Write a for loop header, i.e. something of the form

for ( . . . )

for the following loop body:

result = result * i;

When the loop terminates, result should hold the product of the odd numbers between 10 and 20.

result=1;

for(i=11;i<20;i+=2)

**2.**Assume the int variables i, lo, hi, and result have been declared and that lo and hi have been initialized. Assume further that result has been initialized to the value 0.

Write a for loop that adds the integers from lo up through hi (inclusive), and stores the result in result.

Your code should not change the values of lo and hi. Also, do not declare any additional variables -- use only i, lo, hi, and result.

i=lo;

for(i=lo;i<=hi;++i)

{

result=result+i;

}

**3.**Assume that the int variables i and j have been declared, and that n has been declared and initialized.

Using for loops (you may need more than one), write code that will cause a triangle of asterisks of size n to be output to the screen. More...

Example:

For example, if the value of n is 4, the output should be

*

**

***

**** (The line above this one is the last line printed-- note that the first character of each line is an asterisk and that no characters come after a line of asterisks.)

for(i = 0; i < n; i++)

{

for(j = 0; j <= i; j++)

System.out.print("*");

System.out.println();

}

**RunningTotal**

**1.**Write a statement that increases the value of the int variable total by the value of the int variable amount. That is, add the value of amount to total and assign the result to total.

total=total+amount ;

Quite helpful sample source codes.

ReplyDeletejust wanted to say thanks

ReplyDelete