++c

bennyכלליLeave a Comment

/*************************
discription: the program can chack if num.
**************************/
#include <iostream>
using namespace std;

bool perfect(int num) // this functiom can chack if number is comleated or not

{
int holdNum, numLoop, modolo, sum = 0;

holdNum = num; // holdNum created to keep the num value in case the num change dworing the program
modolo = num – 1;
numLoop = num;

for (int i = 1; i < numLoop; i++) // this loop take the input "num" and chack all the numbers from this num down to 1, ….
{ //….. (for example if the num is: '7', the loop chack 6,5,4,3,2,1) and do "%num", if the result is 0 its mean that the num divisible in this number

holdNum = num;

holdNum = num%modolo;

if (num%modolo == 0) // its mean: the num (the input) divisible on this number
{
sum += modolo; // so in that case the program added the number to the sum
}

modolo–; // in the next rotation to do "num%(num -1)" , and in the next . '"num%(num -2)"….

}

if (sum == num) // "sum" here, us the sum of the numbers that num can be divisible in, so if sum = num , the number is complete
{
return true;
}
else
{
return false;
}

}

void function2(int n ) // this functiom can get number and can separate is to 1,2,3,4,5,6……… to nember and each number sent to the first function to check if is compleat number
{

int num, chack;

num = n;
n = 1;
for (int i = n; i < num; i++)
{

chack = perfect(n);
if (chack)
{

cout << n << " ";

}

n++;
}

}

int main()
{

int number;

cin >> number;
while (number < 0) // the while loop here is to make shure that the input is not negative number.
{
cout << "EROOR" << endl;
cin >> number;
}

function2(number); // the input number will go to the function2 to separate it to 1,2,3,4,5,6……… to nember and each number will…..
cout << endl; //….. be sent to rhe first function (perfect) to chek if between the numbers there is a compleate number.

function2(500); //we wont that the progran print anyway the compleat nubers between 0 to 500. no metter what the input is.

// system("pause");
return 0;
}

/*********OUTPUT*****8

enter a number:
445
6 28
6 28 496

**********************

enter a number:
3

6 28 496

*********************/

כתיבת תגובה

האימייל לא יוצג באתר. שדות החובה מסומנים *