C++ :: Find Factors And Primes For A Range Of Numbers

Oct 9, 2014

I'm trying to write a program that will find all the factors and primes for a range of numbers. I have the inner loop working but I am having trouble writing the outer loop that will output the range of numbers instead of just finding the factors for one number.

int n1 = 0;
int n2;
int factor = 0;
cout << "Enter a starting number: ";
cin >> n1;
cout << "Enter a ending number: ";

[Code] ....

View 7 Replies


ADVERTISEMENT

C++ :: Random Numbers Are Not In Specified Range

Nov 9, 2014

My program behaves weird... I wanted to generate 10 random numbers from 1 to 100 each of them bigger than previous, using the while loop and function that returns a random number in specified range.

When I run the program, I get numbers much bigger than 100, even negative number, and numbers are same every time I run the program.

Code:
#include <ctime>#include <cstdlib>
#include <iostream>
using namespace std;
int range(int low, int high);

[Code] .....

View 2 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++ :: Finding A Range Between 2 Numbers?

Sep 23, 2013

Ok so I'm reading the Programming: Principles and Practice using C++ and Im stuck in Drill 4 part 5. It says:

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/10000000

I'm using an If statement for it... I just need know what the formula is to check 2 numbers that were entered by person if they land within the range specified above. so then I can cout << "numbers are almost equal" << endl;

View 4 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++ :: How To Make Array With A Range Of Numbers From 1 - 9 Only

Jan 29, 2013

I want to make an array with a range of number from 1-9 only. So if the user entered more than or less than the number indicated it will be error and ask the user to reinput the data. So far my code can be used to do so that if the user enter the number one by one... But if the user entered all the number in one shot the reentered value will be prompt to the back automatically for some reason...Let say 10,2,3,4 which was suppose to be 1,2,3,4 became 2,3,4,1 instead... here is the code:

#include<stdio.h>
void main() {
int num[4][4];
int row,col,x,y;
for(row=0;row<4;row++)

[Code] .....

View 3 Replies View Related

C++ :: Find All Prime Numbers In A Range

Oct 17, 2013

I am trying to create a program that lists all prime numbers within a range of two number. Why the below program isn't working?

#include <iostream>
using namespace std;
int main(){
int high_range;
int low_range;
int w;

[Code] .....

View 3 Replies View Related

C/C++ :: Finding Happy Numbers For A Given Range

Oct 5, 2014

I'm currently working on an assignment that finds all happy numbers for a given range. Such as 1- 10. My current code just isn't cutting it. I have to use a user defined function and return a bool value to main in order to output if the number is happy or if it sad. For some reason, I cannot get it to loop properly. the first number will be displayed correctly, but the second and third are just squares of the first number, instead of being squared by their single digits. I'm dividing by 10 to get the first number of the digit entered, then I'm using mod to get the remainder. One thing I did notice was that the num2 was putting a remainder of 30 for 130, and I'm not too sure what I need to do to change that, without affecting the whole program.

#include <iostream>
#include <iomanip>
#include <cmath>
#include <cctype>
#include <string>
#include <cstdlib>
using namespace std;
Program calculates the bill for a user

[Code] ....

View 8 Replies View Related

C/C++ :: Get Double Random Numbers In Range From 0 To 1?

Apr 19, 2012

How to get double random numbers in the range from 0 to 1?

View 2 Replies View Related

C :: GSL And OpenMP - Getting Random Numbers That Are Out Of Expected Range

May 2, 2013

I am trying to parallelize some of my code with OpenMP. When I switch to using multiple threads I start getting random numbers that are out of the expected range.

Here is a small example:

Code:
#include <stdio.h>
#include </usr/local/include/gsl/gsl_rng.h>
#include </usr/local/include/gsl/gsl_randist.h>
int main() {
int mySeed=0;
const gsl_rng_type *T;
T = gsl_rng_ranlxs2;
gsl_rng *r_1 ;

[Code] .....

gsl_rng_uniform should only output number in the range [0,1) but with multiple threads it outputs larger number as well.

View 2 Replies View Related

C :: Loading Array With Random Numbers Within A Range

Oct 5, 2013

How would i go about loading an array with random numbers with in a range. For example, loading an array of 500 elements with random numbers in the range of 50-100.

View 9 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++ :: Generate Random Numbers Within Specific Range?

Jun 5, 2014

So, there has got to be an easier way to generate random numbers within a specific range. Here is the code that I have been using, for a range of 1-6:

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

[Code]....

View 10 Replies View Related

C++ :: How To Randomly Select Uniform Value From Range Of Numbers

Dec 28, 2013

How do you randomly select a uniform value from a range of numbers?

View 1 Replies View Related

C# :: Generating Unique Random Numbers In The Range

Apr 28, 2014

We had to generate random, unique numbers in the range [1,15]. But running the program for several times showed a bug: It wouldn't always generate a new number for every repeated number. I can't figure out the problem, especially since it works half the time and I can't figure out what's making it work some times and not others.

bool flag1 = true, flag2 = true, flag3 = true;
int i, j = 1;
int[] A = new int[11];
Random rnd = new Random();
A[0] = rnd.Next(1, 15);
Console.WriteLine("1. = " + A[0]);

[Code] .....

View 14 Replies View Related

Visual C++ :: Generating Lottery Numbers In Specified Range

Dec 4, 2012

Write a function named generateLotteryNumbers. The function is passed an int array of size 5. The function should generate 5 different lottery numbers in the range 1 to 50 inclusive and place the numbers in the array. The declaration is as follows:

void generateLotteryNumbers (int lotteryNumbers []);

Note that no data is passed in to the function. The array is used to return the function results. Thus the parameter is an OUT parameter. Do not display the result. Return the result.

Do not seed the random number generator inside the function. If you seed the random number generator inside the function and the function is called many times in the same second, your function will return the same results each time it is called.

I know how to generate the numbers in the specified range but I do not know how to test for duplicates. Here is the code I have so far:

Code:
//This program will test the "generateLotteryNumbers" function
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void generateLotteryNumbers (int lotteryNumbers[]);

[Code] ....

When I try to compile this, my compiler tells me that lines 41 and 46 require an array or pointer type.

View 8 Replies View Related

C :: Program That Multiply Very Large Numbers (out Of Range Of Long Int)

Jan 10, 2014

I have to write a program, that multplicates very large numbers (out of range of long int). It's said that i need to use arrays and read the numbers as strings. My problem is to end function called "mnoz:, because i don't know how to sum the multiplicated values of arrays a and b.

Code:
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include "winbgi2.h"
#include<string.h>
#define roz 10000
char lan(char a[])

[Code] ......

View 1 Replies View Related

C++ :: Read Some Numbers From Input File - Vector Subscript Out Of Range

Nov 3, 2013

My project's goal is to read some numbers from input file,then create a grid.my input file is:

2 3
3 4 5
1 2 4

first line is rows and colums. second and third line the grid location values.My code is

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include<vector>
using namespace std;

[code].....

View 7 Replies View Related

C :: Find All Primes Between 0 And 100

Aug 20, 2013

I'm a beginner in C and system programming. I need to use multiple process and POSIX shared memory to find all primes between 0 and 100. My code compiles, but the result is not correct, it shows all the multiples of 3 as primes.My instructor also mentioned that the multi-process portion will fork() the appropriate number of child processes. The parent process will create a POSIX shared memory object to which the sub-processes will attach. I am confused about the things he said about parent process.why I'm not getting the right primes?

Code:

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/mman.h>

[Code]....

View 10 Replies View Related

C :: Print Primes In Limit

Jul 13, 2013

What's actually wrong with the program....I'm trying to print the primes in limit of L1 and L2 ;L1<L2.... */

Code:

#include<stdio.h>
#include<math.h>
void main(){
int L1,L2,n=0
printf("Enter Limits by a space:
}

[code]....

View 3 Replies View Related

C++ :: Find The Sum Of Primes Below Limit

Apr 15, 2013

I have to find the sum of primes below limit. My program works fine when I try to find primes less than 10. However, when I run it for the sum of primes less than 100, I get 166337 when I am supposed to get 1060. My program works on modular arithmetic, where any prime greater than 3 can be expressed as 1 or 5 mod 6.

Here is my code:

#include <iostream>
using namespace std;
int main(){
unsigned long long prime, sum;
int limit = 100;

[Code] ....

OUTPUT:
SUM: 166337

View 4 Replies View Related

C/C++ :: Making A Program That Gives All Primes Below A Given Number

Oct 8, 2014

I'm trying to understand why this won't work, the output i get is a list of even numbers. I'm trying to get all prime numbers below the number thats scanned in.

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

[Code]....

View 5 Replies View Related

C/C++ :: Number Of Twin Primes Between Interval?

Feb 11, 2015

I'm having some trouble finishing my code, it's meant to give the number of twin primes between an interval e.g. 1 to 1000000 and the answer should be 8169 but all I can get it to is 8168

#include <stdio.h>
int prime (int num) {
int div;
if (num == 2) return 1;
if (num % 2 == 0) return 0;
div = 3;
while (div*div <= num) {

[code]....

View 6 Replies View Related

C++ :: Print Out Number Of Twin Primes Program

Jul 27, 2014

This program prints out the total number of up to 100,000. How to do is to print out the number of twin primes up to 100,000. Is it as simple as adding an if statement "if(primesList[j] - i == 2)"?

#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main() {
int primesCount = 0;

[Code] ......

View 4 Replies View Related

C/C++ :: How To Find Sum Of Even Factors

Nov 4, 2013

How to find the sum of even factors ????

View 1 Replies View Related

C++ :: Prime Factors Of A Number?

Oct 22, 2013

I have to create a program where I need to find the prime factors of a number and express it using exponents.

For example, for the number 1368 should output:

1368 = 2^3 * 3^2 * 19^1

This is my code:

void primeFact (int n){
int i;
int score = 0;

[Code].....

View 3 Replies View Related







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