【发布时间】:2022-11-30 04:41:32
【问题描述】:
我想用一个for/while循环来划分用户输入的数字。
比如我想让程序在输入的数可以被除的时候除以2。
假设用户输入了数字 8,那么答案是:
8 被 2 除 3(8/2=4;4/2=2;2/2=1)
下面的表达式是减法,我需要被除的数字。
#include <iostream> using namespace std; int main() { int n; int counter = 0; cout << "Enter a positive integer n: "; cin >> n; for(int k = n; k > 1; k--){ cout<<"\nYour numbers are : " << k; counter++; } cout <<" \n your number is divded :" << counter << " times "; return 0; }
【问题讨论】:
-
for 循环的最后一部分包含每次循环都会计算的表达式。目前,它是
k--,而你想要k /= 2(或k = k / 2)
标签: c++ loops for-loop math divide