While Loop

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
a = 30;
b = 20;
c = 10;
void Generate(){
 while(a <= 90 && b - c >= -80){
  a = a + 1;
 }
}
while(a >= 80){
 a = 0;
 b = b + 4;
 c = c + 7;
 Generate();
}
result = a + b + c;
print(result);

What number which has been printed?


The answer is 371.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

3 solutions

At the beginning a = 30 a=30 and ( b c ) = ( 20 10 ) = ( 10 ) > 80 (b-c)=(20-10)=(10) > -80

Loop 1: If the values satisfies the condition of the first loop, the program enters into the loop and adds 1 1 to the value of a a , but b b and c c remains unchanged.

Loop 2: When a = 80 a=80 , the program enters into the second loop. Then the value of a a becomes 0 0 . It adds 4 4 to the value of b b and 7 7 to the value of c c .

Now again it goes to first loop. When a = 80 a=80 it comes to second loop and change the value of b b and c c again.

Suppose after going to the second loop n n times, b c < 80 b-c<-80 which doesn't satisfy the condition of the first loop and will not go through any loop again.

( b + n × 4 ) ( c + n × 7 ) < 80 (b+n\times 4) - (c+n\times 7) < -80

( 20 + n × 4 ) ( 10 + n × 7 ) < 80 (20+n\times 4) - (10+n\times 7) < -80

3 × n < 90 -3 \times n < -90

n > 30 n > 30 [Dividing both sides by 3 -3 ]

That means after going to the second loop 31 31 times, b c < 80 b-c<-80 .

Then, b = 20 + 31 × 4 = 144 b=20+31\times 4 = 144

c = 10 + 31 × 7 = 227 c=10+31\times 7 = 227

After last loop, a = 0 a=0

Hence, a + b + c = 0 + 144 + 227 = 371 a+b+c = 0 + 144 + 227 = \boxed{371}

According to me ..there is an extra bracket after the completion of the first while loop...due to which method Generate() cant be run further....

Tanya Gupta - 7 years, 2 months ago

Log in to reply

I think you are correct. The second while loop should never run. Thus the answer should be 60.

Steven Perkins - 7 years, 2 months ago

Log in to reply

Yeah...On thinking abt it...thats the answer I got...Thanx!!

Tanya Gupta - 7 years, 2 months ago

the Generate function is just written in main so it is not playing any role..over there but the condition for while loop is in appropriate since at the beginning value of a is 30 so it won't go inside while loop and result 60 will be printed...how can you expect by just defining function in main it will be executed by compiler so,in your proof Farhim Shahriar Shakkhor loop 1 won't be executed..so in above case Ans- 60.

Maurice Patel - 7 years, 2 months ago

Log in to reply

Maurice, you are correct. I've given credit to everyone who entered "60" as an answer, as that was the correct answer as it was written. I've also changed the wording of the problem so that the Generate() function is actually run before the second loop now.

Arron Kau Staff - 7 years, 2 months ago

Log in to reply

Thank You very much!!!

Tanya Gupta - 7 years, 2 months ago

Well, I become confuse with my problem here ._. Thanks for explain this problem :)

Yoga Nugraha - 6 years, 10 months ago
Nanda Khoyri
Apr 29, 2014

my MatLab Program

function main clear all close all clc

a = 30; b = 20; c = 10;

[a b c] = generate(a,b,c); while(a>=80) a=0; b=b+4; c=c+7; [a b c] = generate(a,b,c); end

a+b+c end

function [y1 y2 y3]=generate(x1,x2,x3) while(x1<=90 & x2-x3 >= -80) x1=x1+1; end y1=x1; y2=x2; y3=x3; end

Angga Pranata
Apr 21, 2014

Actually at first time I answer 60 by reading manually from the code but i thing the code should be like this I wrote it in VB.NET

Sub Main() Dim a As Integer = 30 Dim b As Integer = 20 Dim c As Integer = 10

    a = Generate(a, b, c)

    While a > 80
        a = 0
        b = b + 4
        c = c + 7
        a = Generate(a, b, c)
    End While

    Dim result As Integer = a + b + c
    Console.Write(result)       
    Console.Read()
End Sub

Public Function Generate(ByVal a As Integer, ByVal b As Integer, ByVal c As Integer) While (a <= 90 And b - c >= -80) a = a + 1 End While Return a End Function

With this function will print 371

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...