Wrong output when searching for prime numbers in given interval - c++

I am trying to find to find prime numbers between num1 and num2, but this code is yields only 1,2,3 as prime numbers if my input is 1,10
#include <iostream>
using namespace std;
void prime(int num1, int num2)
{
bool prime = 1; // prime=1 means the number is prime
for (num1; num1 <= num2; num1++)
{
for (int i = 2; i < num1; i++)
{
if (num1 % i == 0)
{
prime = 0;
break;
}
}
if (prime == 0)
{
cout << num1 << " Is not Prime" << endl;
}
else
{
cout << num1 << " Is prime" << endl;
}
}
}
int main()
{
int num1, num2;
cout << "Enter 2 numbers to check the prime numbers between them:";
cin >> num1 >> num2;
prime(num1, num2);
}

You're setting prime=1 only once, before you start looping over candidate prime numbers. So as soon as you hit a number that isn't prime (in your case, 4) you get prime=0 and after that it never gets set to 1 again.

Also second for should go from 3 to (int)sqrt((double)num2), with a step i+=2.

You are forgetting to reset bool prime to true.
Try this....
#include <iostream>
using namespace std;
void prime(int num1, int num2)
{
for (num1; num1 <= num2; num1++)
{
bool prime = 1; // prime=1 means the number is prime
for (int i = 2; i < num1; i++)
{
if (num1 % i == 0)
{
prime = 0;
break;
}
}
if (prime == 0)
cout << num1 << " Is not Prime" << endl;
else
cout << num1 << " Is prime" << endl;
}
}
int main()
{
int num1, num2;
cout << "Enter 2 numbers to check the prime numbers between them:";
cin >> num1 >> num2;
prime(num1, num2);
}

Related

Getting Run-time error in c++ for calculating LCM of two Number

I have make my program to calculate LCM of two number..It has no syntax error
but after entering number it has stopped working.. Please help to remove this Error .. Very Very Thanks for help in Advance....
#include <iostream>
using namespace std;
int main()
{
cout << "Enter Your Two Number : ";
int num1, num2, big, i, lcm = 1;
cin >> num1 >> num2;
big = num1;
if(num2 > big)
big = num2;
for(i = 1; i <= big/2; ++i)
{
if(num1 % i == 0)
if(num2 % i == 0)
lcm *= i;
}
cout << "LCM of " << num1 << " and " << num2 << " is " << lcm;
return 0;
}
If the loop starts with zero (as you say it does in a comment), then you will have e.g. num1 % 0 which is division by zero, something which is not mathematically possible and will cause a crash in your program.

Having trouble with a simple loop when trying to print two numbers to the screen at once

I need to make ask the user for how many numbers of the Fibonacci sequence displayed to the screen. I have everything done pretty much, but since i am printing two numbers to the screen at a time, it is printing double of what the user enters. I would just divide the number the user enters by two, but the wont work for odd numbers. If anyone can think of a solution for this, thank you. http://en.wikipedia.org/wiki/Fibonacci_number <-- the fibonacci number if you dont know it
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << "How many numbers do you want generated in the Fibonacci sequence.\n";
int num;
cin >> num;
int num1 = 0, num2 = 1;
int sum;
int f = 1;
while (f <= num)
{
cout << num1 << setw(5) << num2 << setw(5);
sum = num1 + num2;
num1 = sum;
num2 = sum + num2;
++f;
}
}
Loop something like:
while ( f <= num )
{
cout << num1 << setw(5);
if ( ++f <= num )
{
cout << num2 << setw(5);
/// rest of calcs
++f;
}
}
Since you're generating two numbers at a time, you need to increment your counter 2 at a time:
int f = 0;
for (; f < num; f += 2) {
// body
}
And then if num is even, print the last one
if (f == num) {
// print num1
}
In a Fibonacci series, you need a minimun of 2, anything less has no meaning. More than 2 you just have to print the sum.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << "How many numbers do you want generated in the Fibonacci sequence.\n";
int num;
cin >> num;
int num1 = 0, num2 = 1;
int sum;
cout << num1 << setw(5) << num2 << setw(5);
int f = 3;
while (f <= num)
{
sum = num1 + num2;
cout << sum << setw(5);
num1 = num2;
num2 = sum;
++f;
}
cout << std::endl;
}

cant figure out how to always subtract smaller num from larger

i have the whole program except for the part where it always subtracts the smaller number from the larger number. I've been trying to find it but i couldn't find anything to help me with it. that's the only part i need help with.
Here is the code.
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
//function prototype
int getRandomNumber(int lower, int upper);
int main()
{
//declare variables
int smallest = 0;
int largest = 0;
int num1 = 0;
int num2 = 0;
int correctAnswer = 0;
int userAnswer = 0;
//initialize rand function
srand(static_cast<int>(time(0)));
cout << "Smallest Integer: ";
cin >> smallest;
cout << "Largest Integer: ";
cin >> largest;
cout << endl;
for (int x = 1; x < 6; x += 1)
{
//generate two random integers
//from smallest through largest, then
//calculate the sum
num1 = getRandomNumber(smallest, largest);
num2 = getRandomNumber(smallest, largest);
correctAnswer = num1 - num2;
//display addition problem and get user's answer
cout << "what is the difference of " << num1 <<" - " << num2 << " ? ";
cin >> userAnswer;
//determine wether user's answer is correct
if (userAnswer == correctAnswer)
cout << "Correct";
else
cout << " Sorry, the correct answer is " << correctAnswer << ".";
//end if
cout << endl << endl;
} //end for
return 0;
} //end of main function
//*****function definitions*****
int getRandomNumber(int lower, int upper)
{
int randInteger = 0;
//generate random integer from lower through upper
randInteger = lower + rand() % (upper - lower + 1);
return randInteger;
} //end of getRandomNumber function
need help!
If you're only after the difference between the two numbers, why not use abs(num1 - num2)?
Then you wouldn't have to care about the order.
http://www.cplusplus.com/reference/clibrary/cmath/abs/
It seems very straightforward.
if (num1 < num2)
correctAnswer = num2 - num1;
else
correctAnswer = num1 - num2;
just put the logic into a function, and do a simple bool check to decide which result to return.
int SubstractSmallerNumber(int num1, int num2){
if(num1 < num2) return num2 - num1;
else return num1 - num2;
}
This is a brute force method, and you can find smarter ways to solve the problem.
Think about Absolute Value. If you take the absolute value of the subtraction, you will get the same net effect.

C++ - my loop keeps on adding up to 0

so far here's my code
#include <iostream>
using namespace std;
int main ()
{
int num1 = 0;
int num2 = 0;
int sum = 0;
for(num2 = num1; num1 <= num2; num1 +=2) sum += num1;
num1 = num1 / 2 == 0? num1 : num1 + 1;
num2 = num2 / 2 == 0? num2 : num2 - 1;
cout << "Enter the First Number:" << endl;
cin >> num1;
cout << "Enter the Second Number:" << endl;
cin >> num2;
cout << "Total Sum: " << sum << endl;
} //end for
but the sum keeps on adding up to 0 :/
here's the problem.
Create a program that displays the sum of the even numbers between and including two numbers entered by the user. In other words, if the user enters an even number, that number should be included in the sum. For example, if the user enters the integers 2 and 7, the sum is 12 (2 + 4 + 6). If the user enters the integers 2 and 8, the sum is 20 (2 + 4 + 6 + 8 ). Display an error message if the first integer entered by the user is greater than the second integer.
The code is executed sequentially and the for loop initialization will make you lose the boundaries of the loop consider this code instead.
#include <iostream>
using namespace std;
int main ()
{
int num1 = 0;
int num2 = 0;
int sum = 0;
cout << "Enter the First Number:" << endl;
cin >> num1;
cout << "Enter the Second Number:" << endl;
cin >> num2;
if (num1 > num2) // swap the numbers and do not print error message
{
int temp = num1;
num1 = num2;
num2 = temp;
}
//make sure to start from even number
num1 = num1 % 2 ? num1+1 : num1;
for(; num1 <= num2; num1 +=2)
sum += num1;
cout << "Total Sum: " << sum << endl;
} //en
You need to calculate the sum after getting the input!
But your whole calculation and loop usage is wrong. Here it's fixed:
#include <iostream>
using namespace std;
int main ()
{
int num1 = 0;
int num2 = 0;
int sum = 0;
cout << "Enter the First Number:" << endl;
cin >> num1;
cout << "Enter the Second Number:" << endl;
cin >> num2;
if (num1 % 2 == 1) num1 += 1;
if (num2 % 2 == 1) num2 -= 1;
while (num1 <= num2) {
sum += num1;
num1 += 2;
}
cout << "Total Sum: " << sum << endl;
}
Note the following:
% returns modulus - num1 % 2 ==1 implies that num1 is odd. I took out your ternary ?: operators not because they're bad, but just because if is easier to read and in this case you're not doing anything if num1 is even.
You were setting num2 at the start of your for loop. A while loop makes more sense in this situation, or a for loop without initialization for (;num1<=num2; num1+=2) {.
While the homework (I assume) should be solved by you, here are a few hints to help you out:
1) Your for loop needs curly brackets around the code that is supposed to loop:
for(num2 = num1; num1 <= num2; num1 +=2)
{
sum += num1;
num1 = num1 / 2 == 0? num1 : num1 + 1;
num2 = num2 / 2 == 0? num2 : num2 - 1;
}
2) Your loop is above your cout and cin statements, so it runs before the user ever enters any numbers. You need to move the loop to after (below) the numbers have been given to the program by the user.
3)
The logic of the loop is probably not what you want. Once curly brackets are added, this is what it is doing (in "pseudo-code"):
Let num2 equal num1 // Both are set to zero so this doesn't do anything
While num1 is less than or equal to num2:
{
Add the current value of num1 to sum.
if num1 /2 (ignoring remainder) is 0, then set num1 equal to itself. Otherwise, add 1 to it.
// num1 already equals itself, so this doesn't do anything when num1 / 2 is zero.
//
if num2 /2 (ignoring remainder) is 0, then set num1 equal to itself. Otherwise, subtract 1 from it.
Add 2 to num1.
}
Unless the assignment says otherwise, it's probably best not to use the ternary (? and :) syntax because it's pretty confusing when you're just starting to program (at least, I thought so).
C++ is a challenging language to learn, but hang in there!
1.)Get numbers
2.)Determine max and min
3.)Sum evens between
#include <iostream>
using namespace std;
void main()
{
int num1 = 0;
int num2 = 0;
int sum = 0;
int temp = 0;
int i;
//Get your input values
cout << "Enter the First Number:" << endl;
cin >> num1;
cout << "Enter the Second Number:" << endl;
cin >> num2;
cout << endl;
//just to reorganize and make num1 the smallest of the two
if ( num2 << num1 )
{
temp = num1;
num1 = num2;
num2 = temp;
}
//loop through and add even values
for(i = num1; i < num2; i++)
{
if(i%2 == 0)
{
sum = sum + i;
}
}
cout << "Sum: " << sum << endl;
}

LCM program in C++

#include <iostream>
using std::cout;
using std::cin;
int main()
{
int num1 = 0;
int num2 = 0;
cout << "Please enter two numbers (remember to put
a space between them):\n";
cin >> num1 >> num2;
const int num_limit = num1 * num2;
for (int i = 1; i <= num_limit; i++)
{
int product = num1 * i;
int product2 = num2 * i;
// test for when multiples are equal
if (product == product2)
{
cout << "The LCM of " << num1 << " and " <<
num2 << " is: ";
}
}
}
I'm trying to get the LCM from two integers which the user enters. The if statement within the for loop isn't doing what I intended it to do, as nothing prints when product and product2 are equal. What is the solution to this little problem?
int gcd(int a,int b)
{
if(b==0)
return a;
else
return gcd(b,a%b);
}
int lcm(int a,int b)
{
//Efficient Solution
// a*b=gcd(a,b)*lcm(a,b)
return (a*b)/gcd(a,b);
}
int main()
{
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int a,b;
cin>>a>>b;
cout<<lcm(a,b);
return 0;
}
Output
15 12
60
Try this out
//#include "stdafx.h"
#include <iostream>
#include <conio.h>
using std::cout;
using std::cin;
int main()
{
int num1 = 0;
int num2 = 0;
cout << "Please enter two numbers (remember to put a space between them):\n";
cin >> num1 >> num2;
int num_limit = num1 * num2,LCM;
for (int i = 1; i <= num_limit; i++)
{
LCM = num1 * i;
// test for when multiples are equal
if (LCM%num2==0) break;
}
cout << "The LCM of " << num1 << " and " << num2 << " is: " << LCM;
_getch();// replace with getch(); if didn't work
}