C++ :: Function To Determine And Print Prime Numbers

Mar 30, 2013

/* Task: create a function that determines prime number and use it to print out prime numbers from 0-50: */

Function prototype: Should return a `bool` (because it answers a yes/no question) and should take in a single int (to ask whether it's prime).

- An int greater than 2 is prime if it is not divisible by any number between 2 and itself, not including itself.
- Use this function to print out the primes less than 50. Check it against my list:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47

#include <iostream>
using namespace std;
int prime(int&x) {
if(x%2==1)

[Code] ....

It is printing out the correct prime numbers but also printing this between each number: 1629974960

View 3 Replies


ADVERTISEMENT

C++ :: N Prime Numbers - Print Out And Sum

Mar 3, 2014

Write a c++ program to print out the first n prime numbers and the sum of the first n prime numbers.

View 7 Replies View Related

C++ :: Print Prime Numbers - Vectors

Jan 25, 2015

I am trying to print the prime numbers, so i put the numbers into the vector and then erase the non prime numbers. if i comment the break statement i get runtime error saying out of bound, but if i don't comment it out it prints the primes but it also prints the multiples of 3.

void Eratos( int length, vector< int > &v ) {
int i;
for (i=0; i<length; ++i) {
v.push_back(i);
cout <<v[i];

[Code] ....

View 3 Replies View Related

C :: Program To Print All Of Prime Numbers To Screen

May 29, 2013

I'm trying to write a program that prints all of the prime numbers to the screen. I'm getting angry because it only prints EVERY number. I think it has something to do with the bool change not being able to leave the for-loop but how to fix it.

Code:
#import <stdio.h>
#include <stdbool.h>
int main(void){
printf("
The Primes Are: 1");

[Code] ....

View 3 Replies View Related

C++ :: Program Which Print Out All Prime Numbers Less Than Or Equal To Given Number

Nov 11, 2013

how to make a programm in native c++ which print out all prime numbers less than or equal to given number????????

View 5 Replies View Related

C :: Calculate Prime Numbers In Range And Return Count Of Prime Numbers

Apr 28, 2013

I wrote a program which sends a starting and ending range to other processes and the processes calculate the prime numbers in that range and return the count of prime numbers to the head process, process 0. But this is not working properly at the moment. I realize I still have to split up the range based on how many processes I have...I still have not figured out how I want to set that up. I

Code:

#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
int isPrime(int num);
int main(int argc, char **argv){
}

[code]....

View 7 Replies View Related

C/C++ :: Function For Prime Numbers

Mar 9, 2015

I keep getting this error:

||In function 'int main()':|
|19|error: too few arguments to function 'int isPrime(int, int)'|
|13|note: declared here|

Code:

#include <iostream>
#include <fstream>
using namespace std;
int isPrime(int , int);
int main() {
int result = 0;

[Code] ....

View 2 Replies View Related

C :: Won't Print Numbers Sorted When Use Print Function

Nov 17, 2013

I am making a program where the user enters numbers into an array and then a number,x. The array is sorted, then x is inserted into the appropriate place. I wrote my selection sort

Code:

void Sort(int ary[], int size)
{
int temp;
int smallest;
int current;
int move;
}

[code]....

put it wont print the numbers sorted when I use my print function, just the unsorted numbers.

View 1 Replies View Related

C++ :: Prime Number Program Not Working With Odd Non-prime Numbers

Dec 2, 2014

I have a program that gets prime numbers that doesn't work. It looks like it works for all prime numbers except odd non-prime ones. Here is the code:

Code:
#include <iostream>
using namespace std;
bool isPrime(int prime) {
while(true) {
int track = 0;

[Code] ...

View 3 Replies View Related

C/C++ :: Finding All Prime Numbers To Nth Prime Number

Aug 24, 2014

I'm a new coder for C++, and I've recently learned java before starting this programming language. I'm attempting to find all prime numbers up to the number a user enters (i.e. User enters 10, System shows "1,2,3,5,7"),

#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char** argv) {
int num;
cout << "Enter a positive number." << endl;

[Code] ....

I've been looking at my forloop, and I can't seem to pinpoint the problem with it.

View 1 Replies View Related

C++ :: Find Prime Numbers Between Given Pair Of Numbers And Store Them Into Array?

Apr 18, 2014

Find all the prime numbers between a given pair of numbers. Numbers should be read in from an input file called "numbers.txt" and find all the prime numbers between them. Store the prime numbers in an array, then sort the array from greatest to least. Display the array before and after the sort.

I'm stuck on how to put the prime numbers into an array.

The input file has the numbers 1 & 100.

Here's what I have so far.

#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream fin;
fin.open("numbers.txt");

[Code] .....

View 1 Replies View Related

C++ :: How To Determine If Two Numbers Differ From A Specific Value

Dec 2, 2014

I have to write a program that use a while loop and that each time around the loop reads two double, prints out the largest, the smallest and if they are equal.

I have no problem with this program but I can't understand how to modify it following the author : Change the program so that it writes out "the numbers are almost equal" after writing out which is the larger and the smaller if the two numbers differ by less than 1.0/100.

for now this is my code :

int main() {
double a = 0;
double b = 0;
while (cin >> a >> b) {
cout << "first number is : " << a << "
second number is : " << b << "

[Code] ....

The problem is that I don't know how to test if the numbers differ by less than 0.01 because my if statement doesn't work in any case. If I enter 2 and 1.99 It doesn't work, why ?

View 4 Replies View Related

C :: Determine How Many Numbers In A Range Are Divisible By Third - Loop

Mar 1, 2013

I've pretty much finished the entire program, except for the actual calculation part.

"Given a range of values determine how many integers within that range, including the end points, are multiples of a third value entered by the user. The user should be permitted to enter as many of these third values as desired and your output will be the sum of total multiples found."

I've defined functions to take user input for the low range, high range and a do-while loop to take as many third inputs as the user wants (terminated by entering -1, as requested by the question)

To actually calculate if they're divisible, I found out that if A%B = 0, then they are divisible, so I thought I would create a loop where each value in the range between A and B is checked against the third value to see if they output a zero.

What I need to end up with is a program that tells the user how many integers are divisible by the numbers in the range, i.e: "Enter the low range value: 335 Enter the high range value: 475 Enter a value to check within the range: 17 Enter a value to check within the range: -1 There are 8 total values that are divisible by the numbers in the range." Going back to my original question, how would I create a loop or something to "check" how many values are equal to zero, and consequently increment a variable for each instance? (This is how I think it should be done)

Code:

#include <stdio.h>
//GLOBAL DECLARATIONS
int getlowR();
int gethighR(int);

[Code].....

View 4 Replies View Related

C :: Sum Of Prime Numbers

Feb 21, 2014

Code:

#include<stdio.h>
int main(){

int num,i,count,min,max,sum=0;

printf("Enter min range: ");
scanf("%d",&min);

[code]....

when I ran the program , I got "4" with prime numbers. I only way I was able to remove the "4" was this "num!=4".

View 1 Replies View Related

C++ :: Program To Determine Slope Of A Line - Stuck On Commas When Entering Numbers

Jan 1, 2013

The program I have is from a tutorial where the user enters two points on a line, and then the program calculates the mid-point and slope.

I want to modify it from it's initial form so that co-ordinates can be input in (x,y) fashion. Right now the user has to enter the x-coordinate, enter a space, and then enter the y-coordinate (lame...)

I saw people using SStream and using that to either write functions to ignore the comma or similar things for converting one file into an array, but not quite what I am trying here.

// program to determine slope of a line

#include <ios>
#include <iostream>
#include <istream>
#include <limits>

using namespace std;
void myflush ( istream& in ) {

[Code] .....

View 2 Replies View Related

C++ :: Generate A List Of 200 Random Numbers - Determine Median / Mode And Average

Oct 24, 2013

I am just starting out programming and have an assignment due in the near future. the problem is I cant seem to figure it out. I started it already but got stuck cant seem to figure it out. Here is the assignment.

Create a program that will generate a list of 200 random numbers (ranging from 1-1000) and determine the medium, mode, and average of the list of numbers. Have the program display the original list ant then display the list in ascending and descending order on the screen.

View 2 Replies View Related

C :: Prime Numbers And Arrays

May 27, 2013

I was going through a book I have about C trying to learn about arrays, and one of the first few examples of what could been done with an array was showing how to use an array to generate a list of prime numbers. I've been staring at this program for about an hour trying to understand how it works (it does), but the workings of one for loop within the program.

Code:

#include <stdio.h>
#include <stdbool.h>
int main () {
int p, i, primes[50], primeIndex = 2;
bool isPrime;

[Code]...

So that is saying in order for this loop to go on, really two conditions must be met since there's that && operator. isPrime must be true (I think that's what it means by just having "isPrime" and not setting it equal to anything) and p / primes[i] must be greater than or equal to primes[i].So at the beginning of the loop, since i = 1, p = 5 (as per surrounding loop), and prime[i] = 3 ( as per the variable assignment at the beginning of the program ), the loop condition would be "isPrime && 5 / 3 >= 3"

"5 / 3 >= 3" The loop should stop right there! 1.666666667 is NOT greater than or equal to 3!

View 14 Replies View Related

C :: Factorial Using Prime Numbers?

Feb 3, 2014

Code:

#include <stdio.h>
int find_next_prime(int num);
int is_prime(int num);
int main()

[Code]......

How would I go about counting the number of times a factorial has a specific prime number?

For example, 5! = (2^3)*(3^1)*(5^1), 6! = (2^4)*(3^2)*(5^1).

How would I begin to design my find_prime_count function in order to count how many times each occurs? My program is to read in a number between (2<=N<=100) from a text file and output the results exactly like above which I still have to figure out after, I'll assume I have to use fscanf.

View 8 Replies View Related

C++ :: Prime Numbers In A Given Range

Feb 17, 2013

I have an assignment where I have to use two for loops. I need to ask the user for any two numbers and be able to list all the numbers in between and their factors and state whether or not the number is prime or not.

View 2 Replies View Related

C/C++ :: Prime Numbers In Calendar?

Sep 25, 2014

The object is to have the user enter in the number of days then the day the calendar would start on. This part I was able to achieve and run okay. Once I wanted to get the values/days that were prime to have a P besides them this is where I ran into trouble. In this case P stands for prime numbers.

The issue I have is that some of the values that are prime have the correct P but others have the P as well such as 9, 15, and 25 - which are not prime numbers.

I was able to create a program that checked for prime numbers separately but I had issues merging them. Below is the merged code followed by the prime checker.

#include <stdio.h>
int main(int argc, const char * argv[]) {
int i, N, Start;
int j, GetNumber, PrimeNumber = 0;

[code].....

View 2 Replies View Related

C/C++ :: Generate Prime Numbers Up To N

Oct 22, 2014

Given a number "n" supplied by the user, write all the prime numbers less than or equal to "n"

not at all how to make the number I enter me for the numbers that I need even a couple makes the prime number if the command is used repeat for but and decision making but the truth is that I can think of no way to do for this exercise is that the prime numbers are those numbers which are divided only by 1 and themselves also

until the moment I have this code

#include <stdio.h>
#include <stdlib.h>
main(){
int n;
n=0;
printf("enter the desired number: ");
scanf("%f",&n);
system("pause");
}

View 5 Replies View Related

C++ :: Finding Divisers Of Non-prime Numbers?

Mar 17, 2013

I have written a program that finds the divisers of non-prime numbers and outputs them. However, the output is only one diviser per line(because of the repetition to find the divbisers), but the instructor wants us to have 5 to 10 divisers per line. How can i write this loop so it will only output the divisers in one line?

View 1 Replies View Related

C++ :: Finding Only Prime Numbers With For Loops

Feb 18, 2013

A prime number is a number greater than 1 which is only evenly divisible by 1 and itself. For this assignment you will find which numbers from 2 to n (where n is a user-specified number) are prime. Ask the user for a number, n, greater than 2. Keep asking for a number until a number greater than 2 is provided. Assume that the user will only enter numbers (that is, you do not need to check if a user enters text).

Use a loop to iterate on a variable, i, from 2 through n. For each iteration, check all numbers from 2 through i to determine whether the number is prime. If it is prime, print out i and the word "Prime". Use the modulus operator, %, to determine if a number is prime

Example output:

Please enter a number larger than two: -939 Please enter a number larger than two: 2 Please enter a number larger than two: 20 2 Prime 3 Prime 5 Prime 7 Prime 11 Prime 13 Prime 17 Prime 19 Prime

This is what I have so far. I suppose i'm not really understanding how to continue with the second for loop. The first one just prints out a list of numbers from 2 to the number designated by the user. In my case that variable is user_input.

Code:
// Alen
// Prime
//

# include <iostream>
using namespace std;

[Code] ....

View 11 Replies View Related

C :: Find Prime Numbers - How To Use Bit Array

Aug 19, 2013

I wrote some code for class to find prime numbers.The teacher says that I need to revise my code with the requirement below: use a bit array to store the prime number checking. The bit array will also be in the heap. Use the max value of an unsigned 32-bt integer (UINT_MAX) to be the maximum size of the prime number you want to check.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <getopt.h>
#include <ctype.h>
#include <stdint.h>

//function to set all non-primes to 0
void *zero_multiples(void *threadid);

[Code] .....

View 4 Replies View Related

C++ :: Program That Display Prime Numbers From 1 To 100

Oct 4, 2014

This is a program that display prime numbers from 1 to 100. it has problem linking when i run the program.

#include <iostream>
class Prime {
public:
int PrimeNo();
int Display();
};
// class Prime number
int Prime::PrimeNo()

[Code] ....

View 3 Replies View Related

C++ ::  Error In Prime Numbers Program

Jan 29, 2013

Here is what I have:

#include <iostream>
#include <cmath>

using std::cout;
using std::endl;
using std::cin;

[Code] .....

This is the output after compiling when I enter 10 as 'x':
3
5
5
5
7
7
7
7
7
9

View 1 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved