Switch to full style
For C/C++ coders discussions and solutions
Post a reply

Factor of number using C++ code

Sun May 25, 2008 5:03 pm

Here is the code for a factor of integer number
Code:
#include<iostream.h>
int fact(int x);
void main()
{
    
char y;
    do
    {
        
int x;
        
cout<<"plz,enter no."<<endl;
        
cin>>x;
        
cout<<fact(x)<<endl;
        
cout<<"do u want to try again(y/n)?";
a:        cin>>y;
        if (
y!='y' && y!='n')
        {
            
cout<<"plz enter y/n";
            
goto a;
        }
    }
    while(
y=='y');
}
int fact(int x)
{
    
int i,f=1;
    for(
i=1;i<=x;i++)
        
f*=i;
    return 
f;




Re: Factor of number using C++ code

Sun Sep 07, 2008 2:45 am

Just fixed a couple of errors i saw in this code. Btw NEVER use goto statements in c++ unless you really really have to.
Code:
#include<iostream>

int fact(int x);

using namespace std;

void main()
{
   char y;
   do
   {
      int x;
      cout<<"plz,enter no."<<endl;
      cin>>x;
      cout<<fact(x)<<endl;
      cout<<"do u want to try again(y/n)?";
      cin>>y;
      while (y!='y' && y!='n')
      {
         cout<<"plz enter y/n";
         cin>>y;
      }
   }
   while(y=='y');
}
int fact(int x)
{
   int i,f=1;
   for(i=1;i<=x;i++)
      f*=i;
   return f;
}


Re: Factor of number using C++ code

Sun Sep 07, 2008 3:03 am

Big thanks to you "Shimano" :grin:

Re: Factor of number using C++ code

Tue Sep 13, 2011 2:44 am

Nice work guys and on the corrections but shouldn't void main() be int main(void) big help on giving me an idea for a program i have to make though ;)

Code:
#include<iostream>

int fact(int x);

using namespace std;

void main()
{
   char y;
   do
   {
      int x;
      cout<<"plz,enter no."<<endl;
      cin>>x;
      cout<<fact(x)<<endl;
      cout<<"do u want to try again(y/n)?";
      cin>>y;
      while (y!='y' && y!='n')
      {
         cout<<"plz enter y/n";
         cin>>y;
      }
   }
   while(y=='y');
}
int fact(int x)
{
   int i,f=1;
   for(i=1;i<=x;i++)
      f*=i;
   return f;
}


Re: Factor of number using C++ code

Mon Sep 19, 2011 1:21 am

Here is another method based off this program i did for my class. Would have added it to my other post i do not see any editing button though :/.

Code:
#include <iostream>
#include <iomanip>

using namespace std;

//GLOBAL VARIABLES
char y;
int n;
int f;
int counter = 0;

int main()
{
   do
   {
      cout<<"Enter Number to be factored:";
      cin>>n;
      cin.clear();
      cin.ignore(100,'\n');
      if(n < 0) //IF THE NUMBER IS NEGATIVE LET THE USER KNOW
      {
         cout << "Error: you must enter a positive number to factor." << endl;
      }
      for(f = 2; f <= n; f++)
      {
         if(n % f == 0)
         {
            cout << setw(10) << f;
            counter ++; //INCREASE COUNTER EVERY TIME LOOP EXECUTES
            if(counter == 4)
            {
               cout << endl; //IF COUNTER EQUALS 4 OUTPUT END LINE
               counter = 0; //RESET COUNTER TO 0 AFTER COUNTER REACHES 4
            }
         }
      }
      
      cout <<"\nWould you like to do another number (y/n)?" << endl;
      cin>>y;
      while (y!='y' && y!='n')
      {
         cout<<"Enter y = yes or n = no:";
         cin>>y;
      }
   }
   while(y == 'y'); //IF y WAS ENTERED START AGAIN ELSE PROGRAM WILL EXIT
}


Post a reply
  Related Posts  to : Factor of number using C++ code
 Get factor of number using C- find a prime factor     -  
 How to write a code for sorting array of 100 number in C++     -  
 Pseudo Randon Number Generator code??     -  
 code to find a number all divisors using recursion     -  
 compute maximum and minimum values, also scaling factor.     -  
 convert integer number to octal,hexadecimal number systems     -  
 convert octal number to decimal number     -  
 convert decimal number to octal number     -  
 Freeman chain code algorithm code     -  
 i want code for connecting mobile and pc can u send me code     -  

Topic Tags

C++ Math