Error(s) found: '1'

  • Unable to perform the query 'UPDATE cpp_code SET clickthru=clickthru+1 WHERE fid=21'. Table 'cpp_code' is read only.

  • C++ MADE EASY
    Code About Us Tutorial




       
     

    Home > myCode > Determine all right primes

    November 29, 2009 3:14 pm

       

    Determine all right primes


    Posted on: 2001-12-02 06:09:28
    Number of times viewed: 966

     
     
    /*
        File   : rightprime.cpp
        Author : Nuno Alves
        Login  : nca@bu.edu
    
      Purpose: The purpose of this program is to determine all right primes in a
      sequence of numbers.The program that prompts the user to enter a positive 
      integer n, and then displays all "right primes" between 2 and n. 
    
    */
    
    #include<iostream>
    using namespace std;
    
    bool prime(int);
    bool rightPrime(int);
    
    void main(){
      int user_input=0;
      
      while (user_input<2){
        cout<<"Enter an integer greater than two: ";
        cin>>user_input;
        cout<<"\nThe right primes between 2 and "<<user_input<<" are: \n";
      }
    
      //Calls rightPrime function to see if every number from 2 to the 
      //number inputed by the user is a rightprime.
      //Ifit is true then it will output the number
      for(int i=2;i<=user_input;i++)
        if (rightPrime(i)) cout<<i<<"\n";
    }
    
    //The function rightPrime returns true or false depending on whether 
    //its input is a right prime. 
    bool rightPrime(int input_number){
      
      while(input_number>0){
        //if the current number is prime then drops a digit
        //from the right and keeps doing thast until it reaches
        //its last prime form or until current number is no longer
        //a prime number
        if ((prime(input_number))&&(input_number!=1))
          input_number=input_number/10;
        else 
          //if not a prime number then it returns a 0
          return(false);
      }
      
      //if the function hasnt returned already then it is a right prime
      //number
      return(true);
    }
    
    //the function prime, which reports whether or not a 
    //given integer is prime. It scans all dividers from the
    //current number to 1 seeing if their modulus is diferent
    //than zero. If the modulus for all dividers are different
    //than zero then it is a prime number
    bool prime(int input_number){
      for (int i=input_number-1;i>1;i--){
        if ((input_number%i)==0) 
          return (false);
      }
      //if all diveders were different than zero
      //it returns true (1)
      return(true);
    }
    

    Please Rate this Code:



     «12039» THINKQUEST TEAM C0111571 © 2001. All Rights Reserved.