I have a program inputing n number (exm.1000, 10000), and i have to get that number and figure out in a nested for loop only (No Arrays) the factor (or divisors) and after that if the factor of n number is even output = open
if its odd output = closed.
int a, b,
while (n <= SENTINEL)
for (a =1; a<= n number; a ++)
for (b=1; b<=n number; b++)// the is wrong but i cant figure out how to getthe
factors from the first for loop in the second
and the determine if they are closed or open.
I do wana say that i am having a hard time with logic behind it also,
#include <iostream>
usingnamespace std;
int main(){
int number = 0;
cout << "Enter Number to be tested" << endl;
cin >> number;
cin.ignore();
// i <= number /2 because no point testing past half of that number
for(int i = 2; i <= number / 2; i )
if(number%i == 0) // if number is dividable by i than print out that it is
cout << i << " is a factor of the number" << endl;
if(number%2 == 0) // test if it is even because number modulate by 2 is zero if even
cout << number << " is even" << endl;
else
cout << number << " is odd" << endl;
cin.get();
return 0;
}
// Purpose: This program is a game that requires an input for a n lockers.
// After the input the first sudent will open the lockers.
// Then the second student will close all the even-numberd lockers.
// Then the third student will chek every third locker
// if it's open it will close if it's closed it will open it.
// So every (n)-th student will check (n)-th locker and close
// it if it's open and open if its closed.
// When this program is over, it should print the total lockers
// that are open and the number of the lockers that are open.
//
// Input: The number of lockers.
//
// Output: The total lockers that are open.
// The number of the lockers that are open.
//
//********************************************************************************
// include statement(s).
#include <iostream>
#include <cmath>
#include <iomanip>
// using namespace statement.
using namespace std;
// Declare named constants, if necessary.
const int SENTINEL = -999;
int main()
{
int numLockers; //number of lockers
int a; //first for loop use
int b; //secon for loop use
int total; //tTotal lockers open
cout <<"Enter the number of the lockers: ";
cin >> numLockers;
cout <<"The number you entered is: " << numLockers << endl;
//while (n != SENTINEL)
for(a=1; a<=numLockers; a )
//cout << setw(3) << a << endl;
for (b=2; b<=a/2; b)// from this point on i cant seem to go any further
So you want to count how many numbers between 1 and n, have an odd number of factors.
Change your approach. When a number will have an odd quantity of factors?
Hint: if p is a factor of n then q = n/p is also a factor of n.
Suppose that you find a divisor of n. Then n/p = q equivalent to n = p*q
But then q is also a divisor of n.
So, when you find a divisor you actually find 2 divisors (p and q)
Example: 2 is a divisor of 6, and 6/2=3 is also a divisor of 6
So you could be tempted to say: all numbers have an even amount of divisors.
But that will be wrong, as it is easy to provide a counterexample 4 has as divisors 1, 2, 4 only 3 divisors (and odd number)
Now, what make 4 so especial?