Total members 9950 | Gratitudes |It is currently Sat Feb 11, 2012 3:17 am Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 5 posts ] 
Author Question
 Question subject: Factor of number using C++ code
PostPosted: Sun May 25, 2008 5:03 pm 
Offline
Mastermind
User avatar

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 )


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: Factor of number using C++ code
PostPosted: Sun Sep 07, 2008 2:45 am 
Offline
Newbie
User avatar

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


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: Factor of number using C++ code
PostPosted: Sun Sep 07, 2008 3:03 am 
Offline
Mastermind
User avatar

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" :grin:

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: Factor of number using C++ code
PostPosted: Tue Sep 13, 2011 2:44 am 
Offline
Newbie
User avatar

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


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: Factor of number using C++ code
PostPosted: 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
}


TOP
  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 5 posts ] 
Quick reply


  


 Similar topics
 Topic title   Forum   Author   Comments 
 java project code  Java  Anonymous  0
 Read your gmail using Java code  Java examples  msi_333  5
 What's wrong with my code?  Java  Anonymous  3
 project source code in java  Java  Anonymous  0
 Ajax Source code to Suggest application with JSP Server side  AJAX  msi_333  5

All times are UTC [ DST ]


Users browsing similar posts

Users browsing this forum: No registered users and 2 guests



Jump to:  
Previous Question | Next Question 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team