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.
Related
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);
}
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.
I wrote this credit card validation program where the user can enter as many didigts as long as it can be stored in type long, with the following algorithm:
Take every other digit in the number starting with the rightmost and add them together, num1
Then take the rest of the digits that were not added and double them, then split the digit and add them up. ex. if we had as one of the digits 9 we would double it to 18, then split it to 1 and 8 and add them 1+8=9 etc., num2
Then we take num1 and num2 and add them together, and if the resulting number ends in a zero, say 50, then the card is valid, if it ends in anything other then a zero its is invalid
My program works with a number with 1-9 digits but as soon as I try a 10 digit number the algorithm is off, I'm not sure where I'm wrong, can somebody help? Also in the if(ccnum <= 4294967295), if I enter a number greater then 4294967295 my program does not execute the else if, what is wrong?
#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
unsigned long ccnum;
int num1, num2, even, odd, divisorodd, divisoreven, first, split, check;
double digits;
cout << "Enter your credit card number: ";
cin >> ccnum;
digits = ceil(log10((double)ccnum + 1));
if (ccnum <= 4294967295){
num1 = 0;
num2 = 0;
divisorodd = 100;
divisoreven = 10;
first = ccnum % 10;
for (int i = 0; i <= digits; i++){
odd = ((ccnum / divisorodd) % 10);
num1 = num1 + odd;
divisorodd = divisorodd * 100;
}
num1 = num1 + first;
for (int i = 0; i <= digits; i++){
even = ((ccnum / divisoreven) % 10) * 2;
split = (even % 10) + ((even / 10) % 10);
num2 = num2 + split;
divisoreven = divisoreven * 100;
}
}
else if (ccnum > 4294967295){
cout << "\nIncorrect credit card number.\n";
}
cout << "\nNum1 is: " << num1 << "\n";
cout << "\nNum2 is: " << num2 << "\n";
check = num1 + num2;
cout << "\nThe check number is: " << check << "\n";
if (check % 10 == 0){
cout << "\nThe credit card number is valid! Thank you.\n" << endl;
}
else if (check % 10 != 0){
cout << "\nThe credit card number is invalid! Sorry.\n" << endl;
}
}
Declaring the variables as unsigned long long fixed both problems
unsigned long long ccnum, num1, num2, even, odd, divisorodd, divisoreven, first, split, check;
double digits;
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;
}
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;
}