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.
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.
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?
Write a program that prompts the user to enter a number larger than 2. The program should use the Number class to determine the prime numbers between 2 and the number entered, and display them in the console window.I've been stuck for a while now and just lost in implementing classes and contstructors.
#include <iostream> using namespace std; int main(int argc, char * argv[]) { cout << "Enter a number larger than 2: " << endl; int n; cin >> n;
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){ }
I am trying to find the largest prime factor of a number. But when I am trying to determine if a number is a prime number in the function:
int is_prime(int number), I am unable to exit the loop.
Here is the code:
#include<iostream> using namespace std; int is_prime(int number) //the problem is in this function { int num = number; int factor=0; do{ num++; for(int i=1;i<(num+1);i++){
[code].....
So when the program runs, it first divides 20 by 2, to get 10, then divides 10 by 2 to get 5. Since, // condition 1 is not met, it passes 2 to the function int is_prime(int number). The function is able to return 3, but cannot exit the loop when num is 4.
I think the problem lies in the function: int is_prime(int number).
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");
User enters sentence "The Smiths have two daughters, three sons, two cats and one dog." (The numbers may change depending on what the user chooses to enter. He told us the range would be from zero to nine.) and we have to convert the written numbers within the sentence into actual decimal numbers and print out the new sentence. Ex. The Smiths have 2 daughters, 3 sons...etc.
I have written the following bit of code which reads the string and finds all the "written numbers" but I am not sure how to proceed from there. I am stuck on how to print out the new sentence with the converted numbers as my professor mentioned something about creating the new string using dynamic memory allocation.
Code: #include <stdio.h>#include <string.h> int main () { char A[100]; int length = 0; int i;
i am trying to write a program to get a number from user and then print even numbers as well as 0,2,4 and so on.what changes i should do ?
PHP Code:
# include <iostream> using namespace std; int main () { int n,i; cout << " Enter number "; cin >>n; i = 0; while ( i%2 == n) { cout <<i<<" "; i = i+1; } system ("PAUSE"); return 0; }
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!
#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.
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.
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;
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"); }
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.