Total members 11894 |It is currently Sun Nov 24, 2024 2:19 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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;




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time

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;
}



Author:
Newbie
User avatar Posts: 2
Have thanks: 0 time

Big thanks to you "Shimano" :grin:

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time

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;
}



Author:
Newbie
User avatar Posts: 2
Have thanks: 0 time

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
}



Author:
Post new topic Reply to topic  [ 5 posts ] 

  Related Posts  to : Factor of number using C++ code
 Get factor of number using C- find a prime factor     -  
 Pseudo Randon Number Generator code??     -  
 code to find a number all divisors using recursion     -  
 How to write a code for sorting array of 100 number in C++     -  
 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     -  
 i want code for connecting mobile and pc can u send me code     -  
 Freeman chain code algorithm code     -  



Topic Tags

C++ Math
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com