#include<iostream>
using namespace std;
double hey(int x){
if(x==2)
return 1;
int i=0;
if (x%2==0){
while(i<=2){
cout<<(x/2)<<endl;
i++;
break;
}
}
if (x%2==1){
while(i<=2){
i=3*x+1;
cout<<i<<endl;
}
return 1;
}
}
int main()
{
int n;
cout<<"insert a number to check how many steps to reduce down to 2"<<endl;
cin>>n;
cout<<hey(n)<<endl;
return 0;
}
I couldn't
The code will be
#include<iostream>
using namespace std;
double hey(int x) {
int count=0;
while(x!=2){
if(x%2==0)x=x/2;
else x=3*x+1;
count++;
}
return count;
}
int main()
{
int n;
cout << "insert a number to check how many steps to reduce
down to 2" << endl;
cin >> n;
cout << hey(n) << endl;
return 0;
}
The output is

In case there are doubts leave a comment.
#include<iostream> using namespace std; double hey(int x){ if(x==2) return 1; int i=0; if (x%2==0){ while(i<=2){ cout<<(x/2)<<endl;...
#include <iostream> #include <chrono> using namespace std; double improvedPow(double x, int y) { // To be implemented by you } int main() { cout << "To calculate x^y ..." << endl; double x; int y; cout << "Please enter x: "; cin >> x; cout << "Please enter y: "; cin >> y; if(x == 0) { if (y > 0) cout << 0 << endl; else cout << "x^y is not defined" <<endl; } else { cout << improvedPow(x,y)...
what is the output for the following code? explain the steps. /*#include <iostream> using namespace std; int f(int &i) { i = 10; return(5 * i); } int main() { int n = 5; f(n); cout << n << "\n"; return 0; } #include <iostream> using namespace std; int sub1(int n) { n--; return n; } int main() { int m = 10; for(int j = 0; j < 10; j++) m -= sub1(j); cout << m << "\n"; return...
How do can I update this code (Code A): Code (A) #include using namespace std; int fibonacci(int n) { int a = 0, b = 1, c; if (n <= 1) return n; for (int i = 2; i <= n; i++) { c = a + b; a = b; b = c; } return b; } int fibonacciRecursive(int n) { if (n <= 1) { return n; } return fibonacciRecursive(n-1) + fibonacciRecursive(n-2); } int main() { int n;...
#include <iostream> #include <string> using namespace std; int main() { int number; int sum = 0; while(true) { cout << "Please enter a number between 1 and 11: "; cin >> number; if (number >= 1 && number <= 11) { cout << number << endl; sum = sum + number; //only add the sum when number is in range: 1-11, so add wthin this if case } else { cout << number << endl; cout << "Out of range;...
#include <iostream> uisng namespace std; int main() { cout<<"Enter your three test scores and I will "; <<" average them: "; int score1, score2, score3, cin>>score1>>score2>>score3; double average; average = (score1 + score2 + score3) / 3.0; if (average = 100); perfectScore = true; // set the flag variable cout<<"Your average is "<<average <<endl; bool perfectScore; if (perfectScore); { cout<<"Congratulations!\n"; cout<<"That's a perfect score.\n"; ...
#include <iostream> using namespace std; int main() { int i,n; int counter=0; cin >> n; for (i = 1; i <= n; i++) { if (n % i == 0 ) { counter++; } /*else { counter++; i++; }*/ } if (counter == 2) cout << n <<...
#include<iostream> using namespace std; int main() { float num,avg,sum=0.0; int count=0; bool moreNumbers=true; cout<<"Enter grades:"<<endl; while(moreNumbers) { cin>>num; if(num < 0) { moreNumbers=false; } else { count = count+1; sum=sum+num; } } avg=sum/count; cout<<"Average grade:"<<avg<<endl; cout<<"How many grades were entered:"<<count<<endl; return 0; } Question: We need to add another loop to check input. Just like the input loop we already have, this one should use a boolean variable as a control. So, the logic could be something like this: Loop...
Add comments on this Fibonacci C++ program in detail. #include <iostream> using namespace std; int fib(int n){ int a = 0,b = 1,c; if(n == 0 || n == 1){ return n; } while(n>2){ c = b + a; a = b; b = c; n--; } return c; } int main(){ int n; cout<<"Enter n: "; cin>>n; int result = fib(n); cout<<"Fib("<<n<<") = "<<result<<endl; return 0; }
Write following program using Switch statement. #include <iostream> using namespace std; int main() int number; cout << "Enter an integer cin >> number; if (number > B) cout << You entered a positive integer: " << number << endl; else if (number (8) cout<<"You entered a negative integer: " << number << endl; cout << "You entered e." << endl; cout << "This line is always printed." return 0;
1 #include<iostream> 2 using namespace std; int main() int number; 7 cout < "Enter a number to p cin number; cout << "start:" rint the square numbers in reverse order from numb 10 ut << 1. N was: number return e; 12 13