Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
How to get sum of carryover on adding two 4 digit numbers?
An example would be:
Carryovers 111
First 4 digit 9999
2nd 4 digit 7777
Answer 17776
Here I want to calculate the sum of the carries (answer of summing carries = 3), what should I do?
This is a rather simple task. You may not know it sice you are new here, but SO is not a coding plateform, so you won't get answers unless you show us your code first.
But, since I wanted to try myself on the algorithm, here's a simple answer. I warn you, it won't work on all examples (I won't tell you when this code won't work) and I'm not commenting the code on purpose. I'm smelling the assignment here, so the code is just to give you some pointers, not to make your homework.
Beware: It's just a skeleton and you won't get a good grade if you copy paste it as it is.
int main(int argc, char** argv)
{
int numberA = 9999;
int numberB = 7777;
int sum_of_carryovers = 0;
while (numberA > 9 && numberB > 9)
{
int digitA = numberA % 10;
int digitB = numberB % 10;
int sum = digitA + digitB;
if (sum > 9)
{
sum_of_carryovers += 1;
}
numberA /= 10;
numberB /= 10;
}
printf("The sum of carryovers = %d\n", sum_of_carryovers);
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
Im trying the powers of an integer. For example if my integers is 2 first 10 power is 2^1,2^2...2^10. Im using
while (expnt < 10)
{
preExpnt = expnt;
while (preExpnt)
{
preExpnt *= num;
printf("%lld\n", preExpnt);
}
expnt++;
}
but it doesn't work.`
Here is a way you could achieve your purpose.
int num = 2; // for example
int out = 1;
for (int exp = 1; exp <= 10; exp++)
{
out *= num;
printf("%d\n", out);
}
Remarks about your code:
Your inner while loop is infinite if num and expnt are both different from 0.
Assigning preExpnt to the value of expnt at each step and multiplying by num would display a something like: 1*n 2*n 3*n 4*n ... if expnt starts at 1.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Specifically I'm trying to simplify this particular line of code by removing any of its operators with the limitation that I'm only allowed to use ! ~ & ^ | + << >>:
int combine = ((sign << n) + ~sign + 1);
If you run this program a handful of times, with different values of n, there will be an OBVIOUS pattern:
#include <stdio.h>
int main(void) {
int n = 4;
for(int sign=1; sign<15; ++sign)
{
int combine = ((sign << n) + ~sign + 1);
printf("%d => %d\n", sign, combine);
}
return 0;
}
In each case, when:
n == 1, then combine == sign.
n == 2, then combine == 3*sign.
n == 3, then combine == 7*sign.
In general, for any N, combine == ((2n)-1) * sign
Now, can you find a fast way to express that relationship?
Are there any restrictions on values for n and sign that you haven't told us about?
Or does it need to be solved for all values of n and sign?
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a int variable in which I am storing the integer value. Now I want to get only the suffix. e.g:
int num = 12;
int num = 17;
etc
from the above num variables, I want to store the suffix like 2, 7 etc.
What is the efficient way to do that?
Thanks in advance...
The modulo (%) gives you the remainder when divided by a number which will be 10.
int num1 = 12;
int num2 = 17;
int suffix1 = num1 % 10;
int suffix2 = num2 % 10;
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
If i have length of 20 2d array
of length 5 integers represented as bits
int bitsval[6];
and the array looks like for example bitsval = {0,0,1,0,1,0}
and i want the decimal value so something like
(2^0 x 0) + ( 2^1 x 1) + (2^2 x 0)... and eventually return the final value as 10
can some one help write a function a function like this
This looks like a homework question, so I'll help, but not give you the code
What you need to do is have a 'total' variable, and a 'current bit worth' variable.
Start with the 'current bit worth' variable equal to 32 (2 ^ 5)
Then, have a loop going through the array, adding the bit worth on to the total if that bit is '1'
Each time you go to the next element in the array you need to half the 'current bit worth' value
[Edit]
OK - if it's not homework, try this:
total = 0;
bitvalue= 32;
for (i = 0; i < 6; i++) {
if (bitsval[i]) total += bitvalue;
bitvalue /= 2;
}
This should work I guess.
#include<math.h>
#include<stdio.h>
void main()
{
int a[]={1,0,0,1,0};
int sum=0;
for(int i=0; i<sizeof(a)/2; i++)
{
if(a[i]==1)
sum+=pow(2,(sizeof(a)/2)-i-1);
}
}
Without going into any kind of binary, and working of off of purely your explanation here is something that should work:
#include <stdio.h>
#include <math.h>
int main(){
int number[6] = {0,0,1,0,1,0}, result = 0;
for (int x = 5; x >= 0; --x){
result += (number[x] * (int) pow(2, 5-x));
}
printf("Final result: %d\n", result);
return 0;
}
You will off course have to massage it to fit
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I wanted to find out efficiently which drawers are Full. But the output has to be a number corresponding to the binary representation. For example if only the second Drawer is full (from left to right), then the output is 4:
8 4 2 1
0 1 0 0 (Drawer Two is Full)
So I used this approach.
int DrawersFull[4] = {0,0,0,0}; //Initially all are empty
for(i=0;i<4;i++)
{
if(IsDrawerFull) // the api was provided by the interviewer
DrawerFull[i]=1;
}
I am not sure how to generate the output. Any suggestions will be helpful. Thanks.
Interviewer gave me hint that it can be done using bitwise operators but I am not sure.
This is the same as converting binary number to decimal. Try this one:
int res = 0;
for (i = 4; i >= 1; i--) {
res = res * 2 + DrawerFull[i]; // Assuming DrawerFull will contain only 1 or 0.
}
char fullDrawers = 0;
for( char i = 0; i < 4; ++i )
{
if( IsDrawerFull )
fullDrawers &= 1;
fullDrawers <<= 1;
}