Page by Perpetual PC's

Beginning programming ?

Back to Link page | Back to javquad page

Something extra - -     A C++ factoring program: ( version 2 )

page last modified    19 August 2003

This program will display the factors of a number, and the sums and differences of the factors. It should complile fine on RedHat 8.0 ( 2.4.18-14). If you are just starting and want to learn this would be a good start. It is not intended to be used as anything other than an example. It is not necessarily an example of good coding,either. If you want something quick and just need to know the sums, differences, and factors of numbers its ok.
You can copy and paste it into an editor, save it and compile from there. To compile:    name it   factor.cpp    then type   < g++ -o factor factor.cpp > (not the < or the >) then hit enter. When your command prompt returns type < ./factor > and hit enter then follow the directions.
//copy the part below this

#include <iostream>
using namespace std;
int FactCount(int nLoopCount);

/*simple factoring program written by David Tarsi.Will function well on
 a machine with at least 64 megs of ram at around 400 mhz.
 gets slow above inputs over 36000.Any suggestions welcome at dtarsi@premier1.net
 Any one may use this program providing this documentation is included.The author assumes absolutely
 no responsibility whatsoever for any consequenses or damages resulting from the use of this program */


 int main()

{
	int input;
	cout << "Enter Number to be factored:\n";
        cin  >> input;
		FactCount(input);
return 0;
}            			

	

	                     
	int FactCount(int nLoopCount){
	int nFirst;					
	if(nLoopCount > 1000000){	//prevent long loop count
	   nLoopCount = 0;
	cout << "Number to be factored must be less than 1000000:\n";
	}
		
     for (int j=2; j <= nLoopCount; j++){   //loops through int and stops a point set by nLoopCount
		for (int i=2; i <=nLoopCount; i++){  //loops through the other number in the factors
	     nFirst = j*i;                       //multiplies the 2 numbers together nLoopCount times

		if(nFirst > 1000000)
			break;
		
		if(nFirst == nLoopCount){ //sort out the numbers we want
		
		
		cout << j <<" * "<< i <<"  =  "<< nFirst << "  sum of factors =   "<< j+i <<
		"  difference of factors =  " << j-i << " , " << i-j << "\n"; //display result
	
	if(j < i)
	break;
	return 0;
			
	}
	}
    }
	}





//copy the part above this



Back to Link page | Back to javquad page

Page by: Perpetual PC's
Copyright (c) 2002 David Tarsi. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
A copy of the license is included in the section entitled "GNU GPL".


he license is included in the section entitled "GNU GPL".