Page by Perpetual PC's

Beginning programming ?

Back to Link page | Back to javquad page

Something extra - -     A C++ factoring program:


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.
update (2004-02-14) After looking at this more closely I have decided that it is somewhat useless, except to show what one might NOT want to do. It might have a use, however, as a program to see how hard your processor can work and not overheat! It works with the 36000 limit but it is still not good coding.Try this one:
a better version to find the factors of a number

#include <iostream>
using namespace std;

/*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 nLoopCount = 0; //declare variables
	int nFirst ;

	
	cout << "Enter Number to be factored:\n";  //get info to and from the user
	cin  >> nLoopCount;                       //if the user enters a huge number this will keep
						//their machine from going into a long loop
	if(nLoopCount > 36000){
	   nLoopCount = 0;
	cout << "Number to be factored must be less than 36001:\n";
	}
		
     for (int j=1; j <= nLoopCount; j++){   //loops through int and stops a point set by nLoopCount
		for (int i=1; i <=nLoopCount; i++){  //loops through the other number in the factors
	     nFirst = j*i;                       //multiplies the 2 numbers together nLoopCount times
		

		
		if(nFirst <= nLoopCount && nFirst > nLoopCount-1){ //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
			
	}
	}
    }
	return 0;
}









Back to Link page | Back to javquad page

Page by: Perpetual PC's
Copyright (c) 2002 David Tarsi. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with the Invariant Sections being no invariant sections, with the Front-Cover Texts being no Front-Cover Texts, and with the Back-Cover Texts being no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".