C++ Looping | Find output programs | Set 3

This section contains the C++ find output programs with their explanations on C++ Looping (set 3).
Submitted by Nidhi, on June 06, 2020

Program 1:

#include <iostream>
using namespace std;

int main()
{
    int i = 0;
    int num = 5432;

    while (num > 0) {
        i++;
        num = num / 10;
    }

    cout << i;

    return 0;
}

Output:

4

Explanation:

The above code is used to count the digits of the number.

Here we declared two local variables i and num with initial values 0 and 5432 respectively. The loop condition will be true till the variable num is greater than 0. Now we dry run the program:

i=0, num=5432, loop condition is true.
	num = 5432/10;
	num = 543;
And I become 1.

i=1, num=543, loop condition is true.
	num = 543/10;
	num = 54;
And I become 2, loop condition is true.
	num = 54/10;
	num = 5;
And I become 3, loop condition is true.
	num = 5/10;
	num = 0;

And, when i become 4 and the loop condition will false. Then, we printed the value of i that will be equal to the number of digits of the specified number.

Program 2:

#include <iostream>
using namespace std;

int main()
{
    int f = 1;

    int num = 5;

    while (num > 0) {
        f = f * num;
        num--;
    }

    cout << f;
    return 0;
}

Output:

120

Explanation:

The code is using to calculate the factorial of a number. In the above code, we calculated the factorial of variable num that contains 5.

Now we understand the program. Here we declared two local variables f and num with initial values 1 and 5 respectively.

Here we used while loop, that will execute till variable num contains a value greater than 0. Now we dry run the while loop.

Interation1:
f=1, num=5 and condition is true
	f = 1*5
	  = 5 
And decrease num that become 4.

Interation2:
f=5, num=4 and condition is true
	f = 5*4
	  = 20 
And decrease num that become 3.

Interation3:
f=20, num=3 and condition is true
	f = 20*3
	  = 60 
And decrease num that become2.

Interation4:
f=60, num=2 and condition is true
	f = 60*2
	  = 120 
And decrease num that become 1.

Interation5:
f=120, num=1 and condition is true
	f = 120*1
	  = 120 

And decrease num that becomes 0. 
And then the condition will false. 
And then print 'f', it will print 120, 
which is the factorial of 5.

Program 3:

#include <iostream>
using namespace std;

int main()
{
    int i = 1, j = 1;

    while (i <= 5) {
        j = 1;
        while (j <= i) {
            cout << j;
            j++;
        }
        cout << endl;
        i++;
    }

    return 0;
}

Output:

1
12
123
1234
12345

Explanation:

The above code will print the above pattern.

Let's understand the program step by step.

Here we declared two variables i and j and initialized with 1. Here we used nested looping.

Iteration1:
It will print 1.

Iteration2:
It will print 12.

Iteration3:
It will print 123.

Iteration4:
It will print 1234.

Iteration5:
It will print 12345. Then the outer loop will terminate.





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.