English

Give the Output of the Following Program Segment and Also Mention the Number of Times - Computer Applications

Advertisements
Advertisements

Question

Give the output of the following program segment and also mention the number of times the loop is executed:

int a,b;
for (a=6, b=4; a< =24; a=a + 6)
{
if (a%b= =0)
break;
}
System, out .println(a);

One Line Answer

Solution

Output is 12. Twice.
In the loop : for (a = 6, b=4; a< =24; a = a+6), the value of a will be incrementing as 6, 24 and upon incrementing the value to 42, the loop will terminate.
Accordingly the loop has to execute two times.
But within the loop there is a condition : if(a%b = =0) break;
This means when remainder on dividing a by b comes out to be 0 (at 24/4 = 0), the condition breaks and from the above it is clear that value of a is incremented from 6 to 24 when the loop executes second time. 

shaalaa.com
Introduction of Nested Loop
  Is there an error in this question or solution?
2016-2017 (March) Set 1

RELATED QUESTIONS

Write the output:
charch= ‘F’;
int m= ch;
m=m+5;
System.out.println(m+ ” ” +ch);


Convert following do-while loop into for loop.
int i = 1;
int d = 5;
do {
d=d*2;
System.out.println(d);
i+ + ; } while (i< =5);


Analyze the given program segment and answer the following questions : 

for(int i = 3; i <= 4; i++){
    for(int j = 2; j <i; j++){
        System.out.print("");
    }
    System.out.println("WIN");
}

(i) How many times does the inner loop execute ?
(ii) Write the output of the program segment.


State the difference between while and do-while loop.


Convert the following while loop to the corresponding for loop: 

int m = 5, n = 10;
while (n >= 1)
{
System.out.println(m*n);
n--;
}

Analyze the given program segment and answer the following questions:

(i) Write the output of the program segment.
(ii) How many times does the body of the loop gets executed?

for (int m=5; m<=20;m+=5)
{   if(m%3==0)
   break;
else
if(m%5==0)
System.out.println(m);
continue;
}


Give the output of the following program segment and also mention how many times the loop is executed : 

int i; 
for (i = 5; i > 10; i++) 
System.out.println(i); 
System.out.println(i * 4);

What is an infinite loop ? Write an infinite loop statement.


Convert the following do...while loop to for loop:

int x = - 10;
do

{x - -;
System.out.print(x);
}while(r >= 1);

Define a class to overload the method perform as follows:

double perform (double r, double h) to calculate and return the value of Curved surface area of cone CSA π rl
`l = sqrt(r^2 + h^2)`
void perform (int r, int c) Use NESTED FOR LOOP to generate the following format r = 4, c = 5 output -
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
void perform (int m, int n, char ch) to print the quotient of the division of m and n if ch is Q else print the remainder of the division of m and n if ch is R

Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×