You create a program that displays the sum of even integers between and including two numbers entered by the user ..
ex) 2 and 7 = the sum of 12 (2+4+6)
this is what i have so far! butt if u can just put me in the right direction that would be helpful
//Advanced30.cpp - displays the sum of the even integers between and
//including two numbers entered by the user
//Created/revised by <your name> on <current date>
#include <iostream>
using namespace std;
int main()
{
// declare variables
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)
{
cout << "Invalid entry. Final number must be less than the first number. Please try again." << endl;
}
for ( int sum = (((num1 + 1)/2)*2); num1 <= (((num2 + 1)/2)*2) ; sum = 2 + (((num1 + 1)/2)*2) )
return 0;
} //end of main function
In your for loop it should be like this.
double sum = 0.0;
for(i = num1; i <= num2; i++){
if(i % 2 == 0){ // Is our number even
sum += i;
}
}
That's it and it print out sum.
I would simplify your for loop
for(int i = num1; i <= num2; i++) {
if(i % 2 == 0) sum += i;
}
This will look at twice as many numbers, but honestly that's not all that much more expensive.
You could also do it in O(1) time by taking advantage of the fact that the sum 1..n == n*(n+1)
Here's a very simple example in Java, translating it to C++ won't be too difficult I hope :) no C++ compiler on this machine :-X
import java.util.*;
class DoubleSum {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num1 = sc.nextInt();
int num2 = sc.nextInt();
int low = (num1 - 1)/ 2;
int high = num2 / 2;
int sumLow = (low*(low + 1));
int sumHigh = (high*(high + 1));
int sum = sumHigh - sumLow;
System.out.println(sum);
}
}
You are using the same variable to control the for loop and to the sum, this won't work. Try this:
int even1 = num1 % 2 == 0 ? num1 : num1+1;
int even2 = num2 % 2 == 0 ? num2 : num2-1;
for (int i = even1; i <= even2; i += 2) sum += i;
Note that you don't really need a for loop:
int even1 = num1 % 2 == 0 ? num1 : num1+1;
int even2 = num2 % 2 == 0 ? num2 : num2-1;
// how many numbers you will sum (remember they are even, so we need to divide by 2)
int count = 1 + (even2 - even1)/2;
sum = (even1 + even2) * (count/2);
if (count % 2 == 1) sum += (even1 + even2)/2;
for(int i = num1; i <= num2; i++)
{
if(!(i & 1))
sum += i;
}
Your code would end up in an infinite loop.
Look at the for() loop. You have the condition
num1 <= (((num2 + 1)/2)*2)
to determine whether your loop terminates. However, since num1 itself is never incremented, and num1 < num2 is guaranteed, this condition will always be true - which means your for loop would never end. I would also suggest using a separate looping variable.
Related
So I was trying to get the prime number between two numbers, it works fine but it also prints out odd numbers
int prime(int num1, int num2)
{
int sum{0};
while (num1 <= num2)
{
for (int i = 1; i <= num1; i++)
{
if (num1 % i == 0) //remainder needs to be zero
{
if (num1 % 2 == 0) continue; //continue the loop if the number is even
if (i * num1 == num1 && num1 / i == num1)
{//make sure it only prints prime numbers
cout << num1 << endl;
sum += num1;
}
}
}
num1++;
}
return sum;
}
Console log
I tried to make a for loop that iterates between 1 and N and if the remainder is zero it also checks if the number is even.. if it is then it continues the loop. and then i checked for prime numbers.. if (i * num1 == num1 && num1 / i == num1) i expected it to print out only prime numbers and last the sum of them but it also counts odd numbers
This if statement
if (i * num1 == num1 && num1 / i == num1)
{//make sure it only prints prime numbers
cout << num1 << endl;
sum += num1;
}
is always evaluated for any number when i is equal to 1.
So the code has a logical error.
At least there is no sense to start the for loop
for (int i = 1; i <= num1; i++)
with i equal to 1.
Pay attention to that prime numbers are defined for natural numbers that is for numbers of unsigned integer types.
I would write the function at least the following way
unsigned long long int sum_of_primes( unsigned int first, unsigned int last )
{
unsigned long long int sum = 0;
if (not ( last < first ))
{
do
{
bool prime = first % 2 == 0 ? first == 2 : first != 1;
for (unsigned int i = 3; prime && i <= first / i; i += 2)
{
prime = first % i != 0;
}
if (prime) sum += first;
} while (first++ != last);
}
return sum;
}
For example if in main to write this statement
std::cout << "sum_of_primes(0, 10) = " << sum_of_primes( 0, 10 ) << '\n';
then its output will be
sum_of_primes(0, 10) = 17
Indeed in this range [0, 10] prime numbers are 2, 3, 5, 7 and their sum is equal to 17.
I'm New to C++ and I want to know if there's a way to add two fractions by using the LCM, as i want to display the added fractions in reduced form. I also can't use functions or string or thing of that nature. I attempted to answer it using GCD but this isn't the assignment question so i could some help.
Such as 3/8 + 5/12 = 19/24, i know this it the answer, i just don't know how to get c++ to display it
int main()
int num1,denom1,num2,denom2,num3,denom3,GCD,i;
while(denom1!=0 || denom2!=0)
{
cout<<"Enter Numerator of fraction one ";
cin>>num1;
cout<<"Enter Denominator of fraction one ";
cin>>denom1;
cout<<"Enter Numerator of fraction two ";
cin>>num2;
cout<<"Enter Denominator of fraction two ";
cin>>denom2;
num3 = (num1 * denom2) + (denom1 * num2);
denom3 = denom1 * denom2;
cout<<"The answer is "<<num3<<"/"<<denom3;
}
if (denom3 == 0)
{
exit;
}
for (i = 1; i <= num3 && i <= denom3; ++i)
{
if (num3 % i == 0 && denom3 % i == 0)
GCD = i;
}
cout <<"\n The added fraction is " << num3/GCD << "/" << denom3/GCD;
cout << endl;
return 0;
}
You probably want to implement something like
class Fraction{
private:
int numerator;
int denominator;
public:
Fraction (int n, int dn);
void reduce ();
Fraction add (Fraction a, Fraction b);
Fraction subtract (Fraction a, Fraction b);
}
I got this code. I bet it can be simplified, but I think you will understand it.
#include <iostream>
using namespace std;
int main()
{
int tempNum , tempDenom;
int denom1=8 , denom2=12 , denom3, num1=3 , num2=5, num3;
for(int i = 1 ; ;i++){ // this loop will work untill you find
multiplier for the first fraction
if ( (denom1 * i) % denom2 == 0){
denom3 = denom1 * i;
num1 = num1 * i;
break; // when you find it, you will end the
} //loop with break
}
for(int i = 1 ; ;i++){ // this loop will work untill you find
multiplier for the second fraction
if ( (denom2 * i) % denom1 == 0){
num2 = num2 * i;
num3 = num1 + num2;
break; // when you find it, you will end the
} loop with break
}
for (int i = num3 ; i > 1 ; i--){
tempNum = num3 % i; //the % gives you the rest of the divison
tempDenom = denom3 % i;
if(tempNum == 0 && tempDenom == 0){
num3 = num3 / i;
denom3 = denom3 / i;
break; // you have simplified the fraction
}
}
cout << "The result is : " << num3 << "/" << denom3;
}
#include <iostream>
using namespace std;
int main() {
int n;
int reversedNumber = 0;
int remainder;
cout << "Enter an integer: ";
cin >> n;
while (n != 0) {
remainder = n % 10;
reversedNumber = (reversedNumber * 10) + remainder;
n /= 10;
}
if (reversedNumber == n)
cout << "YES";
else
cout << "NO";
return 0;
}
Hello, I want the compiler to show yes but when i enter 2356532 in my input shows No ,This program should show that the input is equal to the inverse number.`
You divide n /= 10 in your loop until you have 0 left so if (reversedNumber == n) will never be true for anything but 0 as input.
Save n before the loop and compare with the value you've saved after the loop.
Example:
int saved = n;
while (n != 0) {
remainder = n % 10;
reversedNumber = (reversedNumber * 10) + remainder;
n /= 10;
}
if (reversedNumber == saved) ...
Demo
You are clearly updating the value of n inside the while loop. At the end, its value will always be 0. Hence, your if statement will always return false.
I suggest you save the initial value of n in a new variable after getting user input and make the comparison between the reversedNumber and that value.
How can i remove all digits from number before finding specific digit.
Let's say i want to get all digits after digit 1 (1-included)
Example: 3543125 - i want to get digits 125
i started doing this:
int n, result;
cout << "Please enter number: ";
cin >> n;
while (n>0)
{
n = n % 10;
}
to get last digit, but i don't know how you can save it in variable and then add the second digit to it.
Can someone please explain, how can i solve this?
Here is an algorithm I didn't test it but it works {only with loops and if}
int num = 3543125;
int temp = 0;
do //get the result in reverse number
{
temp += num % 10;
temp *= 10;
num /= 10;
if (num % 10 == 1)
temp += 1;
} while (num % 10 != 1);
int result = 0;
while (temp > 0) //reverse the temp number to result
{
result = result * 10 + (temp % 10);
temp = temp / 10;
}
cout << result; // = 125
Try this one. (too simple :-) )
#include <math.h>
int n,a, result=0;
cout << "Please enter number: ";
cin >> n;
cout << "Please enter number of digits: ";
cin >> a;
int b=a;
while(b>0) {
result += n%10 * pow(10,a-b--);
n=n%10;
}
std::cout<<"result: "<<result;
I'm trying to find prime numbers and for that I do:
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int count;
cin >> count;
for(int i = 0; i < count; i++) {
int num, num2;
cin >> num;
num2 = num;
int res = 1;
while(num > 1) {
for(int j = 2; j < static_cast<int>(sqrt(num) + 0.5); j++) {
int a = num2 % j;
if(a == 0) {
res = res * j;
num2 = num2 / j;
//cout << j << endl;
}
}
}
cout << "Result is: " << res << endl;
}
return 0;
}
but I do not know why, when I enter 315 it prints me: 3 5 3, without 7. But when I paste 17 instead of static_cast<int>(sqrt(num) - 0.5) that gives me 17, too, it prints the 7, as well.
So, what is the problem, that when I type 17 it prints me 7, but when I calculate with sqrt - it doesn't?
Also, it does not print this one cout << "Result is: " << res << endl;.
I did not work with C++ for a long time, so may be I forgot something?
This program attempts to factor a number. Whenever the number hits 1, it has been fully factorized. So the loop should be while (num>1).
Furthermore, you fail to extract powers of primes. You will get 2 and 4 if num is 8. This is solvable if you replace the "int a=num%j;if(a==0)" lines with while (num % j == 0). You'd get the same prime repeatedly, sure [that can be fixed too if you wish].
Alright, I have actually introduced an important performance problem to gain correctness. But can we regain the performance?
You can add an additional condition in the for loop: if (j * j > num) {cout<<num; num=1;}
In the comments, the OP says he wants to obtain all prime factors and ... multiply them together? I'll write code that handles one number below:
int num, res = 1;
cin >> num;
for (int d = 2; d <= num; d++)
{
while(num % d == 0) //while also acts as if
{
cout << d << " ";
num = num / d; //or num /= d;
res = res * d; //or res *= d;
}
}
//here num is 1, res is the original number and the prime factors have been printed
static_cast<int>(sqrt(315) - 0.5) is equal to 17, but you are probably dividing num by some j on the previous iteration of the for loop, here:
num = num / j;