Joined: Tue Mar 27, 2007 10:55 pm Posts: 2103 Location: Earth Has thanked: 39 time Have thanks: 56 time
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; }
_________________ Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )
Shimano
Question subject: Re: Factor of number using C++ code
Posted: Sun Sep 07, 2008 2:45 am
Joined: Sun Sep 07, 2008 2:09 am Posts: 2 Has thanked: 0 time Have thanks: 0 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; }
msi_333
Question subject: Re: Factor of number using C++ code
Posted: Sun Sep 07, 2008 3:03 am
Joined: Tue Mar 27, 2007 10:55 pm Posts: 2103 Location: Earth Has thanked: 39 time Have thanks: 56 time
Big thanks to you "Shimano"
_________________ Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )
Atropos
Question subject: Re: Factor of number using C++ code
Posted: Tue Sep 13, 2011 2:44 am
Joined: Tue Sep 13, 2011 2:39 am Posts: 2 Has thanked: 0 time Have thanks: 0 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; }
Guest
Question subject: Re: Factor of number using C++ code
Posted: 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 }