Recursion final number - c++

If my input number is 2...I understand that the function will take the previous answer and multiply by 2 until the number will be greater than 25
2 * 2 = 4
4 * 2 = 8
8 * 2 = 16
16 * 2 = 32...program stops since num is 32
but then my final number is 62 how am I getting that number>
#include <iostream>
using namespace std;
int myRecursiveFunction (int num)
{
if (num >= 25) // stopping condition
return num;
else
cout << "number is at " << num << endl;
return num = num + myRecursiveFunction (num * 2);
}
int main()
{
int num, num1;
cout << "Enter a number: ";
cin >> num;
num1 = myRecursiveFunction(num);
cout << "Final number is: " << num1;
return 0;
}

You want to do this:
2 * 2 = 4
4 * 2 = 8
8 * 2 = 16
16 * 2 = 32...program stops since num is 32
you are doing
return num = num + myRecursiveFunction (num * 2);
which returns 32+16+8+4+2 = 62

2 + 4 + 8 + 16 + 32 = 62.
The return values are getting added each time.

Related

Sum of all integers that are both divisible by 4 and 6 between the first and second number

I need to write a program that will get 2 integers. The program will then display the sum of all integers that are both divisible by 4 and 6 between the two numbers
I tried doing the code down below:
#include <iostream>
using namespace std;
int main()
{
int num1, num2, sum=0;
cout << "Input first number : ";
cin >> num1;
cout << "Input second number : ";
cin >> num2;
for(int i = num1 + 1;i<num2;i++)
{
if ( num1 % 4 == 0 && num2 % 6 == 0)
{
sum = sum + i;
}
}
cout<< "The sum of all integers that are both divisible by 4 and 6 between " << num1 << " and " << num2 << " is " << sum << endl;
system("pause");
return 0;
}
My expected result should be
Input first number : 4
Input second number : 12
The sum of all integers that are both divisible by 4 and 6 between 4 and 12 is 12
"since 12 is the only number that is both divisible by 4 and 6"
But the actual results are
Input first number : 4
Input second number : 12
The sum of all integers that are both divisible by 4 and 6 between 4 and 12 is 56
Here is the correct program:
#include <iostream>
using namespace std;
int main()
{
int num1, num2, sum=0;
cout << "Input first number : ";
cin >> num1;
cout << "Input second number : ";
cin >> num2;
for(int i = num1;i<=num2;i++) // if you want to include num1 and num2
//for(int i = num1+1;i<num2;i++) // if you do not want to include num1 and num2
{
if ( i % 4 == 0 && i % 6 == 0)
{
sum = sum + i;
}
}
cout<< "The sum of all integers that are both divisible by 4 and 6 between " << num1 << " and " << num2 << " is " << sum << endl;
system("pause");
return 0;
}
The condition in if( num1 % 4 == 0 && num2 % 6 == 0 ) is wrong.
You need to change it to:-
(i % 4 == 0 && i % 6 == 0)
There are two issues:
At first, as 4 and 12 used as bounds shall yield 12, not 0, you need to include the bounds as well in your loop:
for(int i = num1; i <= num2; i++)
// ^ (!)
// ^ - 1 dropped
Then your condition to select the summands is not correct:
if(num1 % 4 == 0 && num2 % 6 == 0)
Be aware that this is always true for num1 == 4 and num2 == 12, so you'd sum up all numbers in between... What you actually want is to check the variable running in between these two borders, which is i:
if(i % 4 == 0 && i % 6 == 0) // i will be 4, 5, ... , 11, 12 (with above fixed loop)
Additionally, we can have it a bit shorter: a number is divisible by both 4 and 6, if it is divisible by 12. So your check might simply look like:
if(i % 12 == 0)
As stated, you only need check for divisibility by 12 and you need to use i as the check in the conditional. My system does not have pause(), so you can just use another cin >> call to create the pause. Of course, then you have to provide a letter/number input. A space is not sufficient.
#include <iostream>
using namespace std;
int main()
{
int num1, num2, sum=0;
int wait_var;
cout << "Input first number : ";
cin >> num1;
cout << "Input second number : ";
cin >> num2;
for(int i = num1 + 1;i<num2;i++) // bounds are correct
{
if ( i % 12 == 0) // check for divisibility by 12 of i, not of the num1 and num2
{
sum = sum + i;
}
}
cout<< "The sum of all integers that are both divisible by 4 and 6 between " << num1 << " and " << num2 << " is " << sum << endl;
cin >> wait_var;
return 0;
}

IN, OUT, INOUT Parameters

Please explain the execution of the program such that why it will generate such specific outputs.
The output is
6 10 20
6 10 8
2 2 14
My guess would have to be that it is due to the IN, OUT, INOUT parameters but I am not really understanding it
#include <iostream>
using namespace std;
void sunny(int&, int);
void cloudy(int, int&);
int temp;
int main()
{
int num1 = 6;
int num2 = 10;
temp = 20;
cout << num1 << " " << num2 << " " << temp << endl;
sunny(num1, num2);
cout << num1 << " " << num2 << " " << temp << endl;
cloudy(num1, num2);
cout << num1 << " " << num2 << " " << temp << endl;
}
void sunny(int& a, int b)
{
int w;
temp = (a + b) / 2;
w = a / temp;
b = a + w;
a = temp - b;
}
void cloudy(int u, int& v)
{
temp = 2 * u + v;
v = u;
u = v - temp;
}
Actually the output is:
6 10 20
2 10 8
2 2 14
We can see the steps like this:
Sunny call:
temp = (6 + 10)/2 = 8
w = 6/8 = 0 //This is because w is declared int, and int(6/8) = 0
b = 6 + 0 = 10
a = 8 - 6 = 2
Therefore num1 is now 2, because of the reference argument, and temp is changed to 8 because its global
Cloudy call:
temp = 2 * 2 + 10 = 14
v = 2
u = 2 - 14 = -12 //This variable is doing nothing
Therefore num2 is now 2 for same reason as before and temp is 14

While Loops: simple division program outputting zero, don't understand why

DIRECTIONS:Write a while loop that prints userNum divided by 2 (integer division) until reaching 1. Follow each number by a space. Example output for userNum = 40:
20 10 5 2 1
#include <iostream>
using namespace std;
int main() {
int userNum;
cin >> userNum;
while (userNum >= 1) {
userNum = userNum / 2;
cout << userNum << " ";
}
cout << endl;
return 0;
}
OUTPUT: (userNum = 40; 20 10 5 2 1 0)
In this part:
userNum = userNum / 2;
cout << userNum << " ";
When userNum = 1 (The final iteration of the loop) - 1/2 = 0.
You are doing an integer division .
Hence 1/2 = 0.5 after truncation it will be 0
By reading your question , it seems like you don't want the division of 1/2 to be done.
If so, check the condition in your while Loop
Make it userNum > 1 instead of userNum >= 1

Calculate factorial of the largest numbers

i want to calculate the factorial of the numbers greater than 30 , when i want to calculate factorial the result is 0 .
#include <iostream>
using namespace std;
void fun1();
int main()
{
fun1();
}
void fun1(){
int x ;
int sum = 1 ;
cout << "Enter your number:";
cin >> x ;
if ( x >= 1 ){
for ( int i = x ; i > 1 ; i--){
sum *= i ;
}
}
cout << "Factorial of " << x << " is :" << sum ;
}
You can use larger data types (unsigned long long int if available).
You can cheat some digits off by checking whenever you have a 2 and 5 factors in the expression, removing them and remembering to output an extra 0 at the end:
6! = 6 * 5 * 4 * 3 * 2 = ( 6 * 4 * 3 ) * (5*2) = "72""0"
best of all you use a "large number" or "arbitrary precision" library

I need to write a C++ program that shows the sum of 1 to n for every n from 1 to 50

I used the below code, but my numbers are incorrect & my instructor would rather I use a for loop. It also needs to print out:
The sum of "n" through "n" is " " (The sum of 1 through 1 is 1)
The sum of "n" through "n" is " " (The sum of 1 through 2 is 3)
I've tried using for loops, but can't seem to get the code right to print out the above. I'm lost!
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
const int NUM_LOOPS = 50;
int count = 0;
while (count < NUM_LOOPS)
{
cout << "Sum of 1 through " << count << " is " << (count * (++count)) / 2 << endl;
}
system("pause.exe");
return 0;
}
With a for loop it looks like this:
for (int i = 1; i <= NUM_LOOPS; i++)
{
cout << "Sum of 1 through " << i << " is " << i*(i+1)/2 << endl;
}
You could write it in a while loop just as easily if you wished.
Your problem was that you were initialising count to 0 rather than 1. Presumably to deal with the fact that you modified count in the middle of the long cout. The evaluation of the << operators are unsequenced relative to each other and your code exhibits undefined behaviour, as has been discussed here so many times before.
The bottom line with this is that the pre and post increment operators are dangerous in anything beyond the most simple expressions. Use the sparingly.
for (int count = 1; count <= NUM_LOOPS; ++count)
{
cout << "Sum of 1 through " << count << " is "
<< (count * (count+1)) / 2 << endl;
}
Don't do mix funny increments with mathematical formulas. Your future life will be happier.
I don't think it's a good way to calculate every summary. You just need to maintain one summary currently, and each time just add new value
Since multiple operation will cost more time than a single add.
const int NUM_LOOPS = 50;
int count = 0, sum = 0;
while ( count < NUM_LOOPS )
cout << "Sum of 1 through " << count << " is " << (sum+=(++count)) << endl;
My example is from 1 to 10
int sum=0;
for (int i = 1; i < 11; i++) {
for (int j = 1; j <= i; j++) {
cout << j;
sum=sum+j;
if(j != i){
cout << " + ";
}
}
cout << " = " << sum;
sum=0;
cout <<"\n";
}
Output is:
1 = 1
1 + 2 = 3
1 + 2 + 3 = 6
1 + 2 + 3 + 4 = 10
1 + 2 + 3 + 4 + 5 = 15
1 + 2 + 3 + 4 + 5 + 6 = 21
1 + 2 + 3 + 4 + 5 + 6 + 7 = 28
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 = 36
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55