📄 find the middle value.c
字号:
// USE DESCRIPTION:
// - This program will compute the prime divisor of an integer number between 2 and 10,000.
// The program will first prompt the user to enter an integer number, then show the integer number as the
// multiplication of its prime divisors. Note that the prime divisors are ordered accendingly.
// DESIGN DESCRIPTION:
// - 1. ask user to enter a number.
// 2. use two int variables to store the number, one is for estimate if the number just can be divided by itself or
// else; the other is for estimate which numbers can be divided.
// 3. using "for" repetition to force user to enter a valid number
// 4. "while" repetition combines with "if" selection to divid the number
// - when the divisor i less than the number then the number goes into the loop
// - when the number can be divided then number equals to the results, do it again; until the changing
// number can not be divided by the divisor, the divisor pluses 1 and do the same process untill the
// divisor equals the number - 1; get out of the loop and do the next step
// - when the divisor get out of the loop, it equals the number.
// - if it also equals to the origial number, the number must be the number, which just can be divided by
// itself; if it just equals to the final number, it means it can be divided by other numbers.
// Required header files
#include <iostream>
using namespace std;
// Start of main function
int main()
{
// Named constants
const int minNum = 2; //the minium number
const int maxNum = 10000; //the maxium number
// Variable declarations
int num; //to store the enter number
int selfDivisor; //to estimate if the number just can
//divided by itself or not
int i = 2; //to limit the loope
cout << "Please enter an integer number in the range of 2 to 10,000"<<endl; //require a number
cin >> num; //get a number
//error checking
for ( ;num < minNum || num > maxNum; )
{
cout << "The integer number must be in the range of 2 to 10,000"<<endl;
cout << "Please enter an integer number in the range of 2 to 10,000"<<endl;
cin >> num;
}
cout << "You have entered " << num << " and" << endl;
cout << num << " = ";
selfDivisor = num; //for estimating selfdivisor or not
//dealing with the result
while ( i < selfDivisor)
{
if ( (selfDivisor % i) == 0 )
{
cout << i << " * ";
selfDivisor /= i;
}
else
i++;
}
if ( i == selfDivisor && i != num )
cout << selfDivisor << endl;
else
cout << "1" << " * " << num <<endl;
// Successful completion
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -