Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
In order to convert a given number to binary I wrote this code
//Binary conversion
int num,count=0;
int bi[15];
cout<<"Enter number";
cin>>num;
while(num>=1){
bi[count]=num%2;
num=num/2;
count++;
}
for(int i=0;i<=count;i++){
cout<<bi[count-i];
}
But the answer is wrong.It gives a -85993460 at the front.
If I want to convert 10 the result would be -859934601010.
Can someone please point out what's wrong with this code
When i is zero, the expression count-i is one position after the last entry of the array; this is undefined behavior, so an arbitrary number, such as -85993460, can be printed, or the program could crash.
To print your array backwards, use bi[count-1-i] instead, and end the loop upon reaching count:
for(int i=0 ; i != count ; i++) {
cout<<bi[count-1-i];
}
Your loop limits are off-by-one - the loop should be
for(int i=1;i<=count;i++){
cout<<bi[count-i];
}
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 months ago.
Improve this question
#include <iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int i = 2;
while(i<n){
if(n%i==0){
cout<<"not Prime";
break;
}else{
cout<<"Prime";
break;
}
i++;
}
return 0;
}
This code is written for showing prime or composite/notPrime numbers but 2 is prime & why it is not showing in output?
I write this code for getting for identifying that given number is prime or not. It can work on any number/digit but it can't show about "2" . So why is it?
Because 2 is special number it's even prime and you have to add special case for 2.
for more info: https://mathworld.wolfram.com/EvenPrime.html#:~:text=The%20unique%20even%20prime%20number,%22oddest%22%20prime%20of%20all.
Your program will not work for 2 because the condition goes false and it will never enter to the loop!
Update
Look here for primality test
https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have to find all prime numbers between two given numbers(given in ascending order i.e small, large) I made logic such that my program starts from the given least number till the given most numbers and find factors for each number in between, if factors count are 2 i.e 1 and itself(which is a condition for a prime number), hence it is printed as prime. However I am unable to print my desired output.. can't track why(P.S I am 19 years old newbie in Programming)
#include <iostream>
using namespace std;
int main(){
int start,end;
cin>>start,end;
for(int i=start+1;i<end;++i){
int count;
for(int j=1;j<=i;++j){
if(i%j==0 || i/2==0)count++;
}
if(count==2) cout<<i<<endl;
}
return 0;
}
Input: 1 10
Expected Output:
2
3
5
7
9
Output: (nothing)
Your program has several issues.
cin>>start,end; is not going to read in 2 numbers. You need cin >> start >> end;
You are not initializing count to anything, so you invoke undefined behavior when you do count++. You need to do int count = 0;
Also, when checking if n is prime, you don't need to check for divisibility by 1 or n since this is always true.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am trying to get an float type average number from int inputs using the for loop.
But it doesn't show me a correct answer.
Could anyone tell me what i have done wrong?
I think probably I made mistakes in this part,
for( int i = 0; i < size; i++ )
{
sum =+ myArray[ i ];
}
return static_cast< float >( sum ) / size;
I have attached picture so you can see the whole code.
Thank you for any advices
This line:
sum =+ myArray[ i ];
will apply unary+ on right hand side value and then assign that to sum, which is not what you want.
You're looking for:
sum += myArray[ i ];
which will add the right hand side to sum.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I tried to write a simple code that gives all 3 chars combinations of alphabet letters, but it doesn't read the ASCII code (90) character which is 'Z' . Also why letters repeat in combinations in any 90 line?
Here is the code:
{
int i, j, k;
char v[90];
for(i=65; i<=90; ++i)
v[i]=(unsigned char)i;
for(i=65; i<=90; ++i)
{
for(j=65; j<=90; ++j)
if(i!=j)
{
for(k=65; k<=90; ++k)
if(j!=k && i!=k)
cout<< v[i] << v[j] << v[k] << ' ';
cout<<endl;
}
cout<<endl;
}
return 0;
}
The top element of a 90-element array v is v[89].
You can't use v[90]; it doesn't exist.
Either make it char v[91] instead, or change the way you use arrays (there's no need for so many elements here).
char v[90] makes an array with 90 elements, numbered 0 through 89. By accessing element 90 of the list, you are accessing bits of memory used by other things, and thus you can't expect what you put in there to stay there. Initializing the array as 91 elements long should fix both problems.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Couldn't find an answer on google because I didn't know how to phrase is.
I have a regular function as below and would like to update the variable number in the first if statement. I've tried all sorts of combos but nothing works.
int main()
{
int apple, number;
cout << "Enter you number"<< endl;
cin >> apple;
if (apple == 1){
number = 2;
}
else {
number = 3;
cout << number << endl;
}
How would I change the above so I get 2 to output to the screen?
Thanks in advance!
You need to use
if (apple == 1)
instead of
if (apple = 1)
== is used for comparison. Also to note that your code will always assign the value 2 to the variable apple as in your condition you are not comparing rather you are assigning. So in your case the output will always be 2.