ASSIGNMENT4
(FOR LOOP,WHILE LOOP)
QUESTIONS AND ANSWERS
Q3. Find the output.
int sum=0;
for(int x=1;x<=10;x++)
{
if(x%3==0 || x%5==0)
sum=sum+x;
}
cout<<"\nsum="<<sum;
Q4. Find the output.
int x,y;
for(x=10;x>6;x--)
{
for(y=1;y<3;y++)
{
cout<<x<<"@"<<y;
}
if(x%2==0 || x%3==0)
cout<<"\n";
}
Q5. Find the output.
if the value of position is (i) 251 (ii) 302
if (position < 100) {
cout<<"Do something!";
} else if ((position >= 100) && (position < 300)) {
cout<<"Do something else!";
} else {
cout<<"Do something even more different!";
}
Q6. Find the output.
int i = 9;
switch (i)
{
case 0:
cout<<"i is zero.";
break;
case 1:
cout<<"i is one.";
break;
case 2:
cout<<"i is two.";
break;
default:
cout<<"i is greater than 2.";
}
Q7. Find the output.
int i = 20;
if (i == 10)
cout<<"i is 10";
else if (i == 15)
cout<<"i is 15";
else if (i == 20)
cout<<"i is 20";
else
cout<<"i is not present";
Q8. Find the output.
int i,j;
for( i=10; i<=50; i+=10)
{
j=i/2;
cout<< j<< " "
}
Q9. Find the output.
int i;
for (i=0; i<8; i++)
{
switch(i)
{
case 0:
i += 1;
case 1:
i += 2;
case 5:
i += 3;
default:
i += 4;
break;
}
cout<<i<<",";
}
Q1. Find the output:
for (int i = 0; i < 10; i++)
{
// terminate loop when i is 5.
if (i == 5)
break;
cout<<"i: "<<i;
}
cout<<"Loop complete.";
{
// terminate loop when i is 5.
if (i == 5)
break;
cout<<"i: "<<i;
}
cout<<"Loop complete.";
Q2. Find the output.
int N=15;
for(int x=1;x<10;x++)
{
if(N%x==0)
cout<<x<<" ,";
}
cout<<"\nLoops End";
for(int x=1;x<10;x++)
{
if(N%x==0)
cout<<x<<" ,";
}
cout<<"\nLoops End";
Q3. Find the output.
int sum=0;
for(int x=1;x<=10;x++)
{
if(x%3==0 || x%5==0)
sum=sum+x;
}
cout<<"\nsum="<<sum;
Q4. Find the output.
int x,y;
for(x=10;x>6;x--)
{
for(y=1;y<3;y++)
{
cout<<x<<"@"<<y;
}
if(x%2==0 || x%3==0)
cout<<"\n";
}
Q5. Find the output.
if the value of position is (i) 251 (ii) 302
if (position < 100) {
cout<<"Do something!";
} else if ((position >= 100) && (position < 300)) {
cout<<"Do something else!";
} else {
cout<<"Do something even more different!";
}
Q6. Find the output.
int i = 9;
switch (i)
{
case 0:
cout<<"i is zero.";
break;
case 1:
cout<<"i is one.";
break;
case 2:
cout<<"i is two.";
break;
default:
cout<<"i is greater than 2.";
}
Q7. Find the output.
int i = 20;
if (i == 10)
cout<<"i is 10";
else if (i == 15)
cout<<"i is 15";
else if (i == 20)
cout<<"i is 20";
else
cout<<"i is not present";
Q8. Find the output.
int i,j;
for( i=10; i<=50; i+=10)
{
j=i/2;
cout<< j<< " "
}
Q9. Find the output.
int i;
for (i=0; i<8; i++)
{
switch(i)
{
case 0:
i += 1;
case 1:
i += 2;
case 5:
i += 3;
default:
i += 4;
break;
}
cout<<i<<",";
}