This question already has answers here:
Why is my power operator (^) not working?
(11 answers)
What is the C++ function to raise a number to a power?
(17 answers)
Closed 3 years ago.
Print a sequence of n^n for 1 to 15. This code only prints out 0. What am I doing wrong?
int main()
{
int i, n;
i = 0;
n=1;
do {
n = n^n;
cout << n;
n++;
i++;
}while(i<15);
return 0;
Related
This question already has answers here:
Why does a C-Array have a wrong sizeof() value when it's passed to a function? [duplicate]
(6 answers)
Passing Arrays to Function in C++
(5 answers)
Closed 4 months ago.
The community reviewed whether to reopen this question 4 months ago and left it closed:
Original close reason(s) were not resolved
I am trying to recursively count down from an array. I have done this previously in python, but I am having trouble finding the size of array. My sizeof() function is only returning a length of 2, which is odd since the array size should be 20 bytes/4bytes.
string cdown(int param[])
{
int length = sizeof(param)/sizeof(param[0]);
cout<<"length: "<< length << endl;
string blastoff = "blastoff";
for (int i=0;i<length;i++)
{
if (param[i]==0){return blastoff;}
else
{
cout<<param[i]<< " ";
param[i] = param[i+1];
param[length-1] = 0;
cdown(param);
}
}
}
int main(void)
{
int countdown[] = {5,4,3,2,1,};
cdown(countdown);
return 0;
}
Result
length: 2
5
length: 2
4
length: 2
This question already has answers here:
vector.size() - 2 leads to infinite loop - C++ [duplicate]
(2 answers)
How do unsigned integers work
(3 answers)
How compare vector size with an integer? [closed]
(3 answers)
Closed 9 months ago.
The value for i keeps decreasing forever. Technically it should stop after first iteration because ans.size()-k = 0, but it doesn't stop. If I put 0 manually over there then it works completely fine.
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector<int> ans(1);
int k = 1;
for (int i = ans.size() - 1; i >= ans.size() - k; --i)
{
cout << i << endl;
}
}
This question already has answers here:
Value of C define changes unexpectedly
(7 answers)
C++ int with preceding 0 changes entire value
(4 answers)
Closed 1 year ago.
Here I am trying to convert h = 010 to h = 001 (via right shift operator) and then using the 'and' operator with 1, I wanna print yes, but I don't know why it is printing NO.
int h = 010;
h = h>>1;
if(h & 1){
cout<< "YES";
}
else{
cout << "NO";
}
This question already has answers here:
Is floating point math broken?
(31 answers)
Why does pow(n,2) return 24 when n=5, with my compiler and OS?
(4 answers)
Closed 2 years ago.
using namespace std;
#include<iostream>
#include<cmath>
int main()
{
int n;
cin>>n;
int temp;
int m=0;
for(int i=0;i<n;i++)
{
cin>>temp;
m += pow(10,i)*temp;
cout<<"\n"<<m<<"\n";
}
return 0;
}
OUTPUT
5
1
1
2
21
3
320
4
4320
5
54319
I wanted to get 54321 but instead got this for some reason a 1 is getting removed in the 3rd and last loop.
This question already has answers here:
How to convert a single char into an int [duplicate]
(11 answers)
Closed 7 years ago.
This code Find the sum of all digits that occur in a string.
Example
sumUpNumbers("2 apples, 12 oranges") = 5 //2+1+2
Can anyone explain the need for use int('0') in this code!?
int sumUpDigits(std::string inputString) {
int answer = 0;
for (int i = 0; i < inputString.size(); i++) {
if ('1' <= inputString[i] && inputString[i] <= '9') {
answer += int(inputString[i]) - int('0');
}
}
return answer;
}
It converts char into ASCII code to make number out of string
int('9') - int('0') = 9