C++ :: Finding Largest Prime Factor Of A Number?
Mar 10, 2013
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).
View 5 Replies
ADVERTISEMENT
Feb 10, 2015
i want to write a program which find the biggest prime factor of a number for example the biggest prime factor of six is three or the biggest prime factor of fifteen is five. What is my program bug
Code:
#include <stdio.h> // main functions
#include <math.h> // for sqrt function
int main()
{
int i, j, k, f; // F = Flag;
printf("Enter K
[Code]...
View 9 Replies
View Related
Nov 20, 2014
I am attempting to write code that receives a number from the user and outputs the largest prime number underneath the user's number. There are no errors in my code, but no matter what number is imputed, the program says the largest prime number is 1. I cannot find where the issue is in the code. Here is the code I wrote:
#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;
bool isPrime(int num);//function prototype
[Code] ....
View 2 Replies
View Related
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
Aug 30, 2013
While finding the primes , I do not know how to make it into one array so that...
View 7 Replies
View Related
Mar 23, 2013
What is the largest prime factor of the number 600851475143 ?
This is what I have. If I replace 600851475143 by 1000 (for example), it works fine until 2 divides 1000 to 500, 250, 125...but when it has to find the next divisible prime (ie 5) it cannot do that and the program stops working.
Code:
#include<iostream>
using namespace std;
bool find_prime(int number) {
int factor, num;
factor = 2;
num = number;
[Code] .....
View 1 Replies
View Related
Oct 12, 2014
I want to find third largest character in ascii as alphanumeritic. The problem is i cannot use arrays. How can I find third largest number ? Have to I compare all chars with each other ?
void third_largest(){
char ch;
do{
scanf("%c",&ch);
}
while(ch!=' ');
}
The chars can be hold up to press space ...
View 1 Replies
View Related
Feb 10, 2014
I need to calculate the expected time it takes to do an activity using a formula provided and after the loop is broken it needs to show the number of projects processed and the project with the longest expected time. I've got everything working except for finding the the project with the longest expected time. What I have here just keeps displaying the number of the last project processed regardless of if it was the largest or not, specifically lines 54-61 is what i can't seem to figure out.
#include <stdio.h>
int main( void ) {
unsigned int counter;
int projectnumber;
int optimistictime;
int realistictime;
[Code] .....
View 1 Replies
View Related
Sep 10, 2013
I am stuck looking at whats the problem ....
#include <iostream>
using namespace std;
int main() {
float average, largest = 0, smallest = 0;
int count = 1;
int num;
[Code] .....
View 1 Replies
View Related
Oct 3, 2013
cin.ignore (10);
return 0;
}
Its not working
View 2 Replies
View Related
Oct 3, 2013
We need it to say if the number is prime or not we need the equation for the if statement or if we have something else wrong to let me know.
#include <iostream>
using namespace std;
int main () {
int number;
bool countr;
for (countr = 0; countr <= 2; countr ++)
[Code] ....
View 1 Replies
View Related
Sep 26, 2013
I'm trying to make an array that takes a group of numbers and finds the largest number into a template class.
template<class TYPE>
void Integers(TYPE data) {
int integers[] = {4, 25, 32, 85, 150, 12, 98, 200};
int i = 0;
int Max=integers[0];
for (i=1; i < 8; i++) {
[Code] ....
I'm sure I'm going about it all wrong, but I'm not sure as to get it so that it will accept the arrays input.
View 2 Replies
View Related
Mar 18, 2015
I am trying to find the prime factors of a number but when i compile and run my code after i enter a number it just closes it doesn't print ....
included library : [URL] ....
#include "std_lib_facilities.h";
void prime(int n) {
int result;
result = 0;
int i;
for (i = 2; i == n*n; ++i)
[Code] .....
View 14 Replies
View Related
Feb 20, 2015
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;
View 1 Replies
View Related
Sep 15, 2013
I have a very large number in word ~6million digits long
I wish to make a program to check if it is prime.
View 5 Replies
View Related
Jan 30, 2013
find inserted numbers, let say 10 numbers and find the largest and second largest number with their position in an array?
View 3 Replies
View Related
Apr 28, 2014
Write a program that uses two functions; one finds the largest number and second largest number; and second function finds the average. The data, comprising of 20 different temperature values, is available on a file.
View 1 Replies
View Related
May 1, 2013
Write a C-program that efficiently multiplies a number by a factor 2 to the power n. The number to multiply and n are variables, which get a value at the start of the program.
Clue: 1 shift to the left is the same as multiplying by 2. 2 shifts to the left are the same as multiplying by 4. 3 shifts to the left are the same as multiplying by 8
View 7 Replies
View Related
Mar 25, 2013
I'm trying to find the two largest numbers out of ten. I've re-done this problem several times and it still doesn't work.
Code:
#include <stdio.h>
int main(void) {
float number;
float largest;
float secondLargest;
float counter;
float temp;
}
[code]....
View 9 Replies
View Related
Feb 10, 2013
My code compiles fine but it doesn't seem to want to calculate the max integer. It calculates min and average fine but I'm not seeing what is wrong with my code. The max integer keeps coming out wrong.
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include <cstdlib>
#include <algorithm>
using std::swap;
[Code] ....
View 1 Replies
View Related
Oct 10, 2013
I need codes for a program in C or C++ that will show the real factor (the smallest one, without the number 1) when an integer is given as input.
When the program is started, it will ask to enter a number and press enter. After entering a number, it will first check if it is a prime number or not. If prime, it will notice that it is a prime number, otherwise, it will print the smallest real factor of the given integer.
For example, if 12 is entered, it will print, the smallest real factor for this number is: 2
If 27 is entered, it will print, the smallest real factor for this number is: 3
...and so on
View 12 Replies
View Related
Feb 9, 2013
I'm trying to write a program that will allow a user to enter infinite numbers (one at a time) with a way to stop entering values, and once it stops it immediately displays:
Lowest number:
Highest number:
Number of values entered:
Average of numbers:
what I can't figure out is:
-how to end the loop with something other than a number, preferably a char, so that it doesn't affect the average/lowest/highest
-how to output the lowest and highest numbers in a loop (would be easier to figure out were it finite and I could just type a bunch of printf/scanf)
What I have so far:
Code:
#include <stdio.h>#include <stdlib.h>
#define pause system("pause")
main(){
//n = amount of numbers input
//x = number given by user
//s = smallest number
//l = largest number
}
[code].....
View 4 Replies
View Related
Sep 29, 2014
I've been working at this for awhile and it seems like it should be correct but it isn't giving me the desired output. Here's my code.
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <iomanip>
[Code]....
And here is my output when I run the program.
75
Max: 90
Min: 0
Press any key to continue . . I am reading from a file in this program which contains a set of 30 numbers and it has the average correct and the max correct but the minimum number in the set is 56 however it keeps giving me a zero for the min.
View 4 Replies
View Related
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
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
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