多线程循环输出abcc++_C ++循环| 查找输出程序| 套装3

多线程循环输出abcc++

Program 1:

程序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:

在这里,我们声明了两个局部变量inum ,初始值分别为0和5432。 直到变量num大于0为止,循环条件才成立。现在我们干运行该程序:

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.

而且,当变成4时,循环条件将为假。 然后,我们打印i的值,该值将等于指定数字的位数。

Program 2:

程式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.

该代码用于计算数字的阶乘 。 在上面的代码中,我们计算了包含5的变量num的阶乘。

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

现在我们了解了程序。 在这里,我们声明了两个局部变量fnum ,其初始值分别为1和5。

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

在这里,我们使用了while循环,它将执行直到变量num的值大于0。现在我们开始运行while循环。

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:

程式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.

在这里,我们声明了两个变量ij,并用1进行了初始化。在这里,我们使用了嵌套循环。

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.

Recommended posts

推荐的帖子

翻译自: https://www.includehelp.com/cpp-tutorial/looping-find-output-programs-set-3.aspx

多线程循环输出abcc++

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值