C++ :: Receive Number From User And Output Largest Prime Number

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


ADVERTISEMENT

C++ :: Find Largest Prime Number K Of Array 1

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

C++ :: Largest Prime Factors Of The Number 600851475143

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

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 View Related

C :: Program That Allow User To Enter A Number N And Print Nth Prime Number

Nov 3, 2013

I need to write a program that will allow the user to enter a number "n" and the program tell you that the nth prime number is .....

EXAMPLE

user enters 55

printf("The 55th prime number is %i", variable");

View 1 Replies View Related

C :: Find The Largest And Smallest Number Entered By User?

Mar 6, 2015

How to find the largest and smallest number entered by a user. So far I'm able to add, find the average and count how many numbers are entered.

I'm just having trouble with find the max and min. I tried if statements and it breaks the program will it wont let me exit the while loop or the program will do a force close after entering a value.

Code:

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

int maxNum=0, minNum=0, value, count=0, sum=0;
double average;

[Code] ....

View 4 Replies View Related

C++ :: User Defined Functions - Find Largest And Smallest Number

Nov 16, 2013

I am writing this code, and I every time I run question A, no matter what numbers I put in, I get "larger = 0.

#include <iostream>
using namespace std;
int awesome ();
int best ();
int crazy ();
int main () {
char letter;
int smallestNumber;
int largestNumber;

[Code] .....

View 2 Replies View Related

C++ :: Determines Whether A Number Which User Inputs Is Prime Or Not

Jan 11, 2015

I am fairly new to C++ and I am trying to write a code that determines whether a number which the user inputs is prime or not. I have the code, but when I run it all it actually does is report odd numbers as prime and even numbers as not prime.

#include <iostream>
using namespace std;
//declaring variables//
int i;
int num;

[Code] ....

View 7 Replies View Related

C/C++ :: User Input - Number Prime Or Composite?

Mar 8, 2014

User will input a number in the range of 2 and 2^63-1 and the program will decide whether it's prime or not by a simple 'YES' or 'NO'.

I figured out a code:

#include <stdio.h>
#include <string.h>
main() {
unsigned long long n,i=3; int temp=0;
int t;
scanf("%d",&t); //Number of test cases

[Code] ....

Now this code works for normal inputs but whenever I try it with large primes in tat range, like 987654321987654329 or 9223372036854775783 it does not work />.

I tried to print the values of i to check the limit to which it runs.. and I found it 23697, whose square is 561547809 - nowhere near about the input primes.

View 10 Replies View Related

C/C++ :: Find Prime Factors Of Number Input By User?

Apr 21, 2014

find the prime factors of the number input by the user. This time, we are going to have the user input a number, and the program will find all prime numbers from 2 to that number and prime factors of the number input by the user

#include <iostream>
#include <cmath>
using namespace std;

[Code]......

View 5 Replies View Related

C++ :: Write Program Which Tells User If Number They Input Is Prime Or Not?

May 2, 2014

So I need to write a program which tells the user if the number they input is prime or not. I have done so and included the code below. My problem is that I need to allow the user to input if they want to enter another number after the first one using y or n and I am having trouble figure it out.

#include <cstdlib>
#include <iostream>
using namespace std;

[Code].....

View 5 Replies View Related

C :: Write A Code To Find All The Prime Numbers Before A User Entered Number

Apr 9, 2014

I am trying to write code to find all the prime numbers before a user entered number. I started with a working program and when I tried to create a function, it got all messed up.

Code:

#include <stdio.h>
int is_prime( int num );
int get_positive_integer(void);
int main( ) {
int upper; /* upper limit to check */
int num; /* current number to check */
int isprime;
/* used to flag if number is prime or not */

[Code]...

It says the error is on line 23

View 6 Replies View Related

C :: Find Largest And Second Largest Number In Array With Their Position

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

C++ :: Find Largest / Second Largest Number And Average

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

C/C++ :: Program To Allow User Input And Output Perfect Number

Mar 11, 2014

The problem that I am having is that , the program outputs numbers that are perfect numbers and im not sure where i can add a statement to make it so that if it isn't a perfect number it doesn't output...

#include<iostream>// allows user input/output
#include<conio.h>
#include<fstream>//data file / result file
#include<iomanip>
#include<cmath> // math function
#define in_file "data.txt"
#define out_file "result.txt"

[Code] ....

View 3 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++ :: Input Decimal Number And Output To Be Number Scientific Notation

Apr 26, 2013

I need to write a code in c++ , to input decimal number and the output to be number in Scientific notation with 32 bits .

View 1 Replies View Related

C :: How To Print Largest Number From 5 Rows

Sep 26, 2013

i want to print Largest number from any 5 rows.Th number printed should be any one of the largest in the five rows of 2d arrays.I have created code for largest number in each row but how to pick and print them randomly?.

Code:

#include<conio.h>
main( )
{
int a,b,c,d,e,x;
int arr[] = {a,b,c,d,e};
int Matrix[5][5] ={ /*Initializing array*/
2,4,3,5,9,
6,8,2,2,10,

[Code]...

View 9 Replies View Related

C :: Find Largest Number Of 3X3 Matrix

Jul 26, 2013

Its a code to find the largest number of a 3X3 matrix.The logic seems to be right........

Code:

#include<stdio.h>
main() {
int matrix[3][3],i,k,j,*a;
a=&matrix[0][0];
printf("Enter the elements");
for(i=0;i<=2;i++) {

[Code]....

View 7 Replies View Related

C :: Largest Negative Number In 2 Complement

Mar 3, 2014

In this exercise:The C Programming Language Exercise 3-4..It states the following: "In a two's complement number representation, our version of itoa does not handle the largest negative number, that is, the value of n equal to -(2 to the power (wordsize - 1)) ."

A char is one byte (255 bits). The range of an 8 bit variable using a two's complement representation is -128 to 127. Therefore -128 is the largest negative value. The statement in book suggests that the itoa function will not output -128 if we pass -128 as a parameter, because in itoa when we try to convert -128 to positive -128, the inverse of -128 is -128. However, I just ran this code in my computer and it successfully outputted -128.

Code:

#include <stdio.h>
#include <string.h>
#define SIZE 10
void reverse(char s[])
{
int c, i, j;
}

[code].....

View 2 Replies View Related

C/C++ :: Finding Third Largest Number Without Using Array

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

C/C++ :: Finding Largest Number In Loop

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

C/C++ :: How To Find Smallest And Largest Number

Oct 18, 2014

Write a program that will find the smallest, largest and average of the values in a collection of N positive integer numbers.

View 12 Replies View Related

C :: Program To Find Largest Number From 5 Row By 5 Column Matrix

Feb 27, 2013

I made the code that stores and prints 5 row by 5 column values but how to find the largest number from them.What should I do to find the largest number?If I use if-else then it would be very tedious but I think there is way out with for loop but still I can't frame the logic. Here is the code

Code:
#include<stdio.h>
main() {
int lnum[5][5];
int i,j;
for(i=0;i<=4;i++) {

[Code] .....

View 7 Replies View Related

C++ :: Finding Smallest / Largest And Average Number - Do While Loop

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







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