Home

5b   5c    5d   5e   5f   5g   5h  5i   5j   5k   5l  5m

 

 

5 a)

#include<iostream.h>

#include<conio.h>

void main()

{

int feet;

const int inch_conversion=12;

clrscr();

cout<<"\nEnter feet...";

cin>>feet;

cout<<"\nConverted to inches...."<<feet*inch_conversion;

}

 

Input -7 for feet.

 

Explanation:

We assume 7 as the user-input for feet. Feet is then multiplied with inch_conversion.

The output is 84.

inch_conversion is declared as const in because, the value is standard therefore should not be changed.  You cannot modify the value stored in inch_conversion.

 

Top

5 b)

#include<iostream.h>

#include<conio.h>

void main()

{

int l=1,s=0;

clrscr();

while(l++<=5)

{

cout<<"\n"<<l;

s+=l;

}

cout<<"\nValue of the variable l after executing

 

the while loop..."<<l<<"\nSum..."<<s;

}

 

Explanation

l is initialized to 1 and s is initialized to 0;

while(l++<=5)

 

Here the value stored in l is checked for a lesser value than 5 and then it gets incremented.

 

Iteration

Value in l

Condition

Result

Value in l after postfix

Output

s

I

 

1

1<=5

True

2

2

S=0+2

II

2

2<=5

True

3

3

S=2+3

III

3

3<=5

True

4

4

S=5+4

IV

4

4<=5

True

5

5

S=9+5

V

5

5<=5

True

6

6

S=14+6

VI

6

6<=5

False

7 [loop terminates]

 

 

 

 

Output:

2

3

4

5

6

Value of the variable l after executing  the while loop...7 Sum….20

 

Note that l gets printed inside the loop and sum is printed outside the loop. The value in l gets incremented after checking condition, and it is added consecutively.

Top

 

 

5 c)

#include<iostream.h>

#include<conio.h>

void main()

{

int i=1,sum=0;

clrscr();

while(++i<=5)

{

cout<<"\n"<<i;

sum+=i;

}

cout<<'\n'<<i<<'\t'<<sum;

}

 

Explanation:

In the previous program postfix incrementation was used and in this program prefix is used. Let us see how it changes the output.

 

Iteration

Value in i

Value ini after prefix

Condition

Result

Output

s

I

 

1

2

2<=5

True

2

S=0+2

II

2

3

3<=5

True

3

S=2+3

III

3

4

4<=5

True

4

S=5+4

IV

4

5

5<=5

True

5

S=9+5

V

5

6

6<=5

False [loop terminates]

 

 

 

 

The value in i is incremented first and then condition is checked. The value of  i is printed in every iteration of the loop. When the value in i becomes 6 the condition is violated and the loop terminates. The value in i is printed outside the loop also. Since it got incremented beforehand the value in i now is 6 and the sum is 14.

Output:

2

3

4

5

6                    14

Top

 

 

 

5 d)

#include<iostream.h>

#include<conio.h>

void main()

{

int i=1,sum=0;

clrscr();

for(i=1;i<=5;i++)

{

cout<<"\n"<<i;

sum+=i;

}

cout<<"\n"<<i<<"\t"<<sum;

 

for(i=1;i<=5;++i)

{

cout<<"\n"<<i;

sum+=i;

}

cout<<"\n"<<i<<"\t"<<sum;

}

 

Explanation:

 

There are two sets of for loop in the program. In the first loop postfix expression is used and in the second, prefix is used. As stated earlier, this does not make any difference in the number of times the two loops are executed. Let us see how.

 

Iteration

Value in i

Condition

Result

Output

s

Increment

I

 

1

1<=5

True

1

S=0+1

2

II

2

2<=5

True

2

S=1+2

3

III

3

3<=5

True

3

S=3+3

4

IV

4

4<=5

True

4

S=6+4

5

V

5

5<=5

True

5

S=10+5

6

VI

6

6<=6

False[loop terminates]

 

 

 

 

 

 

The value in i when it comes out of the loop is 6. But in the second loop we reinitialize i to 1. But s is not reinitialized so, it retails the value 15. Here is the loop execution of the second for loop.

 

Iteration

Value in i

Condition

Result

Output

s

Increment

I

 

1

1<=5

True

1

S=15+1

2

II

2

2<=5

True

2

S=16+2

3

III

3

3<=5

True

3

S=18+3

4

IV

4

4<=5

True

4

S=21+4

5

V

5

5<=5

True

5

S=25+5

6

VI

6

6<=6

False[loop terminates]

 

 

 

 

When i comes out of the second loop, its value is 6 again. But the value of s is 30 as the values are added consecutively with the value it retained from the previous loop.

 

Output:

1

2

3

4

5

6                    15

1

2

3

4

5

6                    30

Top

 

5 e)

#include<iostream.h>

#include<conio.h>

void main()

{

int i=1,j=1;

clrscr();

do

  {

      while(j<=i)

      {

         cout<<"#";

         j++;

       }

      cout<<"\n";

      i++;

      j=1;

    }while(i<=5);

getch();

}

 

Explanation:

 

Can  you identify the structure used in this program? Nested loop is used. If you are not sure about what it is, here is a recap;

 

While()

{

while()

{

Statement 1;

Statement2;

……

}

Statement3;

Statement4;

…………..

            }

           

The loop and statements in blue are called Outer loop and outer loop statements. The loop and  statements in green are called inner loop statements and inner loop. For every iteration of the outer loop the inner loop gets executed n times. Got the basics? Lets get into the program.

 

Here do…while loop structure is used in the outer loop and while loop is used in the inner loop. Both I and j are initialized to 1. Remember in case of do…while loop first the statements are executed and after that the condition is checked.

 

Iteration

Value in i

Value in j

Condition for i

Result

Output

Increment of j

Increment of i

Condition for i

I

1

1

1<=1

True

#

2

No increment

 

II

1

2

2<=1

False

 

 

2

2<=5[True]

III

2

1

1<=2

True

#

2

No increment

 

IV

2

2

2<=2

True

#

3

No increment

 

V

2

3

3<=2

False

 

 

3

3<=5[True]

VI

3

1

1<=3

True

#

2

No increment

 

VII

3

2

2<=3

True

#

3

No increment

 

VIII

3

3

3<=3

True

#

4

No increment

 

IX

3

4

3<=4

False

 

 

4

4<=5[True]

X

4

1

1<=4

True

#

2

No increment

 

XI

4

2

2<=4

True

#

3

No increment

 

XII

4

3

3<=4

True

#

4

No increment

 

XIII

4

4

4<=4

True

#

5

No increment

 

XIV

4

5

5<=4

False

 

 

5

5<=5[True]

XV

5

1

1<=5

True

#

2

No increment

 

XVI

5

2

2<=5

True

#

3

No increment

 

XVII

5

3

3<=5

True

#

4

No increment

 

XVIII

5

4

4<=5

True

#

5

No increment

 

XIX

5

5

5<=5

True

#

6

No increment

 

XX

5

6

6<=5

False

 

 

6

6<=5[False. loop terminates]

 

Explanation:

I think the tabular column is self-explanatory. Let me clear a few points. The outer loop is executed 5 times. In the first iteration of the outer loop inner loop is executed 2 times. In the second iteration of the outer loop the inner loop is executed 2 times and so on.

 

Output:

#

# #

# # #

# # # #

# # # # #

Top

 

 

 

 

5 F)

#include<iostream.h>

#include<conio.h>

main()

{

int num=1784,s=0,d=0,x;

x=num;

clrscr();

for(;num>0;)

{

d=num%10;

s+=d;

num=num/10;

}

cout<<"\nThe sum of digis of"<<x<<" is:"<<s;

getch();

}

 

 

Explanation:

 

Here is how the loop is executed.

Iteration

d

s

num

I

4[1784 %10]

4[0+4]

178[1784/10]

II

8[178%10]

12[8+4]

17[178/10]

III

7[17%10]

19[12+7]

1[17/10]

IV

1[1%10]

20[19+1]

0[loop terminates]

 

 

Modulus operator(%) is used to find the remainder when a number is divided by another. For example

k=32%5

When 32 is divided by 5, the remainder is 2. 2 gets stored in k.

 

Important Note: When the divisor is 10, modulus extracts the last digit of a number while binary division(/) removes the last digit of a number. You don’t have to divide and check when the divisor is 10. Just remember this simple aspect.

 

Output:

The sum of the digits of 1784 is 20.

Top

 

 

 

 

5 g)

#include<iostream.h>

#include<conio.h>

main()

{

clrscr();

for(int i=1,s=0;;i++)

{

 if(i%2==0)

  continue;

s+=i;

if(i>9)

 break;

}

cout<<"\nThe sum is.."<<s;

getch();

}

 

Explanation:

This program calculates the sum of first 5 odd natural numbers. The for loop generally consists of 3 parts; initialization, condition checking and increment. Here the second part is left out. This is legal provided semicolon is  given to tell the compiler that you are skipping that portion. The continue statement when encountered continues with the next iteration, skipping the statements below it for that particular iteration. Whereas the break statement when encountered breaks out of the loop even before the loop finishes its normal execution. The first if statement inside the loop simply checks whether the number is divisible by 2. If it is divisible, the loop continues omitting the sum part. The second if statement is used to break out of the loop when the control variable exceeds 9.

 

Note that when the value stored in i is an even number the second if condition is not checked, as it continues with the next iteration. Therefore, when i becomes 10 since it is an even number, remaining parts are skipped and i is incremented.

i

s

i>9

1

0 +1

False

2

skipped

Not checked

3

1+3

False

4

skipped

Not Checked

5

4+5

False

6

skipped

Not Checked

7

9+7

False

8

skipped

Not Checked

9

25

False

10

skipped

Not Checked

11

25+11

True[Terminates loop]

 

Output:

The sum is.. 36

Top

 

 

5 h)

#include<iostream.h>

#include<conio.h>

void main()

{

 

clrscr();

for(int i=1,x=0;i<=5;i++)

 x=x+i%2==0?i+1:i*-1;

cout<<x;

getch();

}

 

Explanation:

The loop executes 5 times.

 

i

x+i%2

Condition

x

1

0+1=1

False

-1[1*-1]

2

-1+0=-1

False

-2[2*-1]

3

1+1=2

False

-3[3*-1]

4

-2+0=-2

False

-4[4*-1]

5

2+1=3

False

-5[5*-1]

 

 

Add x and the remainder when i is divided by 2. Checks if the sum is equal to 0. If it is equal to 0, adds 1 to i and it is stored in x; else i is multiplied by -1 and stored in x.

 

The statement right after the for loop is alone executed in the absence of braces to group more than one statements. Therefore the final value of x alone is printed.

 

Output:

-5

Top

 

 

5 i)

#include<iostream.h>

#include<conio.h>

void main()

{

 

clrscr();

do

{

cout<<"\ndo loop..";

}while(0);

getch();

}

 

Explanation:

 

 In the do…while loop, condition is checked after executing the statements. So, even if the condition is false initially, the loop gets executed atleast once. In this program there is no condition specified inside while. Keep in mind that 0 always represents false and 1 represents true in c++.  for and while loops execute the statements under it only when the condition is true.  “While false” we are trying to execute statements. In the very first iteration the condition is false so loop is terminated but not before executing the statements once as we are using do..while loop.

 

Output:

do loop…

Top

 

 

5 j)

#include<iostream.h>

#include<conio.h>

void main()

{

 

clrscr();

int i=0;

for(i=-5;i>=5;i--)

  cout<<"Bjarne Stroustrup";

cout<<"\nReturning to Edit window...";

getch();

}

 

 

Explanation:

Initial value of i is -5. The condition is i should be greater than or =5. The condition is false in the first iteration itself. Therefore the loop terminates without executing the statement. Since there are no braces to group the two statements, it is understood only the first cout is the statement of the loop. On termination the statement in the second cout is printed.

 

Output:

Returning to Edit window….

Top

 

5 k)

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int month=5;

if(month++==6)

cout<<"\nMay...";

else if(month==6)

 cout<<"\n June...";

else if(--month==5)

cout<<"\nMay again...");

getch();

}

 

Explanation:

month is initialized to 5. Postfix increment is used inside if, so the value stored in month is checked for 6 and then it gets incremented. The if part fails because at the time of checking the value in month is 5. The else if part returns true therefore June.. is printed and comes out of if.

 

Output:

June…

Top

 

 

 

5 l)

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int day=3;

switch(day)

{

case 0:cout<<"\nSunday...";

case 1: cout<<"\nMonday...";

case 2: cout<<"\nTuesday...";

case 3:cout<<"\nWednesday...";

case 4 :cout<<"\nThursday...";

case 5 :cout<<"\nFriday...";break;

case 6 :cout<<"\nSaturday...";

getch();

}

 

Explanation:

day is initialized to 3. The statement under case 3 is to be evaluated. so Wednesday… is printed. Since there is no break, case 4 and case 5 is also executed. Thursday… and Friday… is printed. A break statement is encountered, comes out of switch case.

 

Output:

Wednesday….

Thursday….

Friday….

Top

5 m)

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int bool=2,b=4;

while(bool)

{

cout<<bool<<'\t'<<++b<<'\n';

bool--;

b--;

}

getch();

}

 

Explanation:

There is no relational operator used inside while. The condition is similar to

 

while(bool!=0)

 

The loop executes twice.

 

bool

b

output

2

4

2   5

1

4

1   5

0 [loop terminates]

 

 

 

Output:

2                    5

1          5

Top

 

Hosted by www.Geocities.ws

1