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


ADVERTISEMENT

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

C++ :: Printing The First N Prime Numbers (user Input)

Jan 15, 2014

//Finding prime numbers
#include <iostream>
using namespace std;

[Code]....

/*The program currently prints all the prime numbers up to n (For example, if 7 is entered, it prints out: 1, 2, 3, 5, 7. What I want it to do, is print out the first 7 numbers; 1, 2, 3, 5, 7, 11, 13.

View 1 Replies View Related

C/C++ :: Find Sum Of Prime Factors Of User Input Integer

Sep 16, 2014

As an assignment for school , I've to write a program to find the sum prime factors of a user input integer.

E.g. 20 = 2 x 2 x 5 , Sum = 2 + 2 + 5 = 9
E.g. 10 = 2 x 5 , Sum = 2 + 5 = 7

My method for finding the result is as follows :

- Divide the number by increasing values if int i , starting from i=2.

- Once I get a value of i that can divide the number without giving me a remainder , I add this value of i to int sum and divide the number by i.

- I will repeat this process until the user input value is equal to 1.

My code is as shown:

#include<stdio.h>
int primecheck(int n); // Function to check if i is prime
int primesum(int n); // Function to sum the values of i that are prime
int main(void) {
int n;
int sum;
printf("Enter a number (> 1): "); //Prompting and scanning user input ,n
scanf("%d",&n);
sum = primesum(n);

[Code] .....

But for some reason I keep getting an incorrect result, it's as if it is missing out the last factor for each case.

Eg. 20 = 2 x 2 x 5 , the result I get is 4 , which is 2+2
Eg. 40 = 2 x 2 x 2 x 5 , the result I get is 6 , which is 2+2+2;

I've looked through my code numerous times.

View 7 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 :: 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++ :: 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/C++ :: How To Mix Each Number Given By User Input

Jun 29, 2014

There's this program I'm trying to code where the user gives input of 2 values.

Sample :-

Value of X: 1234
Value of Y: 234567
Reversed value X: 4321
Reversed value of Y: 765432
New value as Z: 1223344567 (each number was taken in both X and Y)

Now the thing is I could do the reverse, and I've been trying to find out how.

Here's my code,

#include <iostream>
using namespace std;
int main(){
int x=0, y=0;
int reverse_x=0;
int reverse_y=0;
int z = 0;
int l;

[Code]....

What I thought about that area where I've put a comment on, I was wondering if I code a loop for the amount of times x gets divided by 10 until it becomes 0 and store it in the value, same thing for Y, and add the value together to get Z. But if that's how it is then how I could use that concept here.

View 5 Replies View Related

C/C++ :: How To Mix Each Number Given By The User Input

Jun 29, 2014

There's this program I'm trying to code where the user gives input of 2 values.

Sample :-
Value of X: 1234
Value of Y: 234567
Reversed value X: 4321
Reversed value of Y: 765432
New value as Z: 1223344567 (each number was taken in both X and Y)
Now the thing is I could do the reverse, and I've been trying to find out how.

Here's my code,

#include <iostream>
using namespace std;
int main(){
int x=0, y=0;
int reverse_x=0;
int reverse_y=0;
int z = 0;
int l;

[Code]...

What I thought about that area where I've put a comment on, I was wondering if I code a loop for the amount of times x gets divided by 10 until it becomes 0 and store it in the value, same thing for Y, and add the value together to get Z. But if that's how it is I can't seem to really get that idea how I could use that concept here. I'm not really good with programming, fairly new at this.

View 1 Replies View Related

C/C++ :: Testing To See If User Input Is A Number

Feb 10, 2013

I was wondering what function was the C++ equivalent for testing to see if a user input is numeric.

I have equivalents in Ruby, Java, Python, VB, VBA, php, but being a novice in C++, I have yet to find one - even with Google.

And I know C++ is such a wonderful language, that its extensive library must have one somewhere - considering it's already 2013!

View 2 Replies View Related

C :: User Input 6 - Array To Display Even Number From 0 - 6

Apr 5, 2013

If the user puts in a 6. I need the array to display the even number from 0 - 6. Right now what it does is it displays the first six even numbers. Okay as I'm writing this I'm starting to see where my problem might be..

Code:
void sumIntegers () {
int arr[50];
int i = 0;
int num = 0;
int sum = 0;

[Code] .....

View 1 Replies View Related

C :: Take User Input Between 1 And 10 And Print Out Number As A Word

Oct 2, 2014

I'm working my way through some C exercises to get some practice and have hit a wall. I need to use a function to take a number, such as 1, and print out One.Here's what I've got so far:

Code:
#include <stdio.h>
int name(int n);
char numberOne[] = "One";
int main(void)
{
int n;
}

[code]...

However, when I run the code and enter "1" the only thing that gets printed to the screen is "(lldb)". I've tried changing it to doing it without variables and just printing "One" but that gave the exact same result.

View 6 Replies View Related

C :: User Can Input Phone Number In Any Of Formats

Jun 18, 2013

I'm having a bit issue with ispunct in my function. The user can input a phone number in any of the formats below:

4147778888
(414)7778888
(414)777-8888

The program will only print out:

41447778888

What I am trying to do is accept any integer and parentheses. If the user inputs anything else, the program will display an error message. Right now, parentheses displays an error which is obvious but I'm wondering if there is a way I can allow a parentheses to be entered but not any other symbol. ex: . , : ; " '

Code:
void getPhoneNum(){
// Local Declarations
char string[14];
char* tokPtr;

[Code] ....

View 6 Replies View Related

C :: Generate Words From User Input (Phone Number)

Aug 13, 2013

The basic idea of this exercise is to generate words from a user-input, seven-digit number(a phone number). The code runs, but for some reason, I can't get it to print the numbers into the file.

Code:
/*Write a program that will generate a word based on a randomly generated, seven-digit number. there should be 2187 possible words. Avoid "phone numbers" that begin with 0 or 1*/

#include<stdio.h>
#define PHONE 7
void wordGenerator(int number[]);
void wordGenerator(int number[]) {
//loop counters for each digit in the number

[Code] ....

View 3 Replies View Related

C++ :: Writing Program That Asks User To Input Number?

Sep 11, 2013

im writing a program that asks the user to input a number 1-10 and will transform it into the roman numeral of that number but it seems that its not after the user inouts the number nothing will output

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

[Code]....

View 3 Replies View Related

C++ :: Generate Random Number Between Two Numbers (User Input)

Jul 26, 2014

I am trying to generate a random number between two numbers that the user gives me.

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void rand_int(const int &min, const int &max, int &val);

[Code] .....

View 3 Replies View Related

C++ :: Input From User A Positive Number N And Find Its Factorial

Oct 8, 2014

Write a C++ program that will input from the user a positive number n and find its factorial. Don’t forget to validate the input. The factorial of a positive integer n (denoted by n!) is defines as the product of the integers from 1 to n.

n! = 1* 2 * 3 * ... * (n - 1) * n

You should allow the user to continue working with your program for additional data sets.

Sample output:

Please enter a number: 5
5! = 120
Would you like to continue (Y/N)?Y
Please enter a number: 3
3! = 6

Would you like to continue (Y/N)?N
Good Bye!!

My code for what i think I'm doing is as follows:

#include <iostream>
using namespace std;
int main(){
int i=1;
int n;

[Code] ....

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/C++ :: How To Store The Number Of Times User Input Received

Mar 15, 2015

I'm trying to write a function in C that calculates the average test score given by the user an arbitrary number of times...

View 14 Replies View Related

C++ :: Create Program That Has User Input 5 Digit Number?

Mar 9, 2013

Im trying to create a program that has the user input a 5 digit number. If it's between 10000 & 99999, it will do one thing..(just saying 'yes' for now. Outside those numbers will prompt the user to input again. However, if the user inputs the exact digits 76087, it should display 'term'.

This current code is displaying 'term' whenever the user inputs the 5 digits.

Code:

#include <iostream>
using namespace std;
int main() {
int pin;
cout << "Welcome to Movie Food
Enter your 5-digit pin code: " ;

[code]....

View 14 Replies View Related

C :: Accept Input Number From User And To Convert It Into Array Of Integer

Oct 7, 2014

i was trying to solve a problem in SPOJ and what i wanted to do is to accept an input number from the user and to convert it into a array of integer.

Code:

#include<stdio.h>
int * toarray(int *num);
int main(void)
{
int testCases;
}

[code]....

But whenever i try to print the array in the main function i get only two value and the rest address

Code:

1//number of testCases
23456 //input number
6
2293452
4
2293700
1974439125

Process returned 0 (0x0) execution time : 4.152 s
Press any key to continue. However, if i tried to print the array from within the function, it prints the numbers just fine.

print the array elements from the main program, so that i would be able to go on with the rest of it

View 1 Replies View Related

C++ :: User Input Number And Then Computer Calculates It For Fibonacci Series

Mar 15, 2013

I was asked to write a code that has the user input a number and then the computer calculates it for the Fibonacci series. The output should be separated by commas and a period should follow the last number. Ex. 1,2,3,4,5. <---period

I can't seem to get the period at the end. I have the commas and everything else. Here is my code:

#include "stdafx.h"
#include <iostream>
using namespace std;
int main() {
double num;

cout << "How many Fibonacci numbers do you want to display?";

[Code] ....

View 3 Replies View Related







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