CSE110 Principles of Programming with Java Quiz A6 Last Name

CSE110 Principles of Programming with Java Quiz A6 Last Name (print): First Name (print) 6. Show the output would occur the following code, including proper spacing. (2 pts) that from Show output here: for (int 1 0: 5; for (int k 0: k 5: k++) if (j t- k) System-out. print else System out print System-out printin some code that inputs a set ofinteger values and computes its average. Ask the user first how many will be input. Make sure that your code cannot produce a division by zero int values error. (3 pts)

Solution

6Q)

for(int j=0;j<5;j++)
{
   for(int k=0;k<5;k++)
     
       if(j!=k)
           System.out.print(\"_\");
       else
           System.out.print(\"*\");
     
   System.out.println();
}

__________________

output:

*____
_*___
__*__
___*_
____*

________________

7)

Average.java

import java.util.Scanner;

public class Average {

   public static void main(String[] args) {
       // Declaring avriables
       int nos, n, sum = 0;
       double avg;

       // Scanner object is used to get the inputs entered by the user
       Scanner sc = new Scanner(System.in);

       // Getting the total how many numbers used want to enter
       System.out.print(\"How many number you want to enter ?\");
       nos = sc.nextInt();

       // Getting each value entered by the user and calculating their sum
       for (int i = 0; i < nos; i++) {
           // getting each value entered by the user
           System.out.print(\"Enter number \" + (i + 1) + \":\");
           n = sc.nextInt();
           sum += n;
       }

       // calculating the average
       avg = (double) sum / nos;

       // Displaying the average
       System.out.println(\"The Average those \" + nos + \" numbers is \" + avg);
   }

}

_______________

output:

How many number you want to enter ?5
Enter number 1:10
Enter number 2:20
Enter number 3:30
Enter number 4:40
Enter number 5:50
The Average those 5 numbers is 30.0

___________________

1Ans )b

Reason:As we are not writing any condition while entering into the do-while loop .So the body of the loop will execute atleast once.We are checking the condition only after entering into the do-while loop.But in case of while loop we have to check the condition before entering into the while loop.if the condition fails then the control will not enters into the while loop.So its body will not execute even for atleast once like do-while loop.

_____________________

Q2)

   int x=10;
   do
   {
       System.out.println(x);
       x--; (//Here every time the value of x will be decremented by 1)
   }while(x>0); //Every time we will check whether the value of x is greater then 0 or not.
   }

Ans) 10 times

_________________

Q3)

   int x=10;
   while(x>0) //This loop continues to execute until the value of x is freater than zero
   {
       System.out.println(x);
       x--; (//Here every time the value of x will be decremented by 1)
   }
   }

Ans) 10 times

____________________

Q4)

for(int j=0;j<100;j++) //The outer loop will execute for 100 times
   for(int k=100;k>0;k--) //For every iterration of outer loop the inner loop will execute for 100 times

       x++; //so 100 X 100 =10000 times

Ans)10000 times

__________________

Q5)

for(int row=5;row>=1;row--) //The outer loop will execute for five times

{ // for every iteration of while loop the value of row is decremented by 1
   for(int col=1;col<=row;col++) //This loop will execute based on the value of row variable
// if the row value is 5 then it executes for 5 times ,if row=4 it executes for 4 times

System.out.print(col+\" \"); //Every time it print the vale of col variable
  
   System.out.println(\" \");
}

Ans)

1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

_____________________

 CSE110 Principles of Programming with Java Quiz A6 Last Name (print): First Name (print) 6. Show the output would occur the following code, including proper sp
 CSE110 Principles of Programming with Java Quiz A6 Last Name (print): First Name (print) 6. Show the output would occur the following code, including proper sp
 CSE110 Principles of Programming with Java Quiz A6 Last Name (print): First Name (print) 6. Show the output would occur the following code, including proper sp

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site