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;
Related
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 12 months ago.
Improve this question
How can I change line 6 to handle the integer overflow so that this code will never terminate (while still increasing i in each loop iteration)?
1 int main() {
2 unsigned int i;
3 i = 1;
4 while (i > 0) {
5 printf("%u \n", i);
6 i *= 2;
7 }
8 printf("%u \n", i);
9 }
Because i is unsigned, it is never less than zero, but it may at some point be zero.
I might try to guarantee it is always at least 1 with something like this:
i = i*2? i*2 : 1;
That is:
If i*2 is non-zero, then that is the new value of i.
Otherwise, i*2 would be zero, so instead set i = 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 4 years ago.
Improve this question
I watched a tutorial which explain int:
but I just get 1:
Enter a:999999999999999999999999999999999999999999
Enter b: 9999999999999999999999999999999999999999999999999999999
a * b = 1
$ ./a.out
Enter a:87999999999999999999999999999999999999999999999999999999999
Enter b: 89999999999998798774334999999999994378969869869869458639534934578365
product = 1
What's the problem?
The reason for you getting 1 instead of -129542144 is that you multiply
999999999999999999999999999999999999999999 and 89999999999998798774334999999999994378969869869869458639534934578365
instead of 300000 and 200000.
Try the following short program you will get the result in the example, -129542144.
#include <stdio.h>
int main()
{
int a = 300000;
int b = 200000;
printf("%d\n", a*b);
return 0;
}
This is assuming that the datatype is int and that it is at least 32 bits.
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);
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I just wonder how to find mean of two numbers without using division.
do not use these conditions :
int mean = (a + b) >> 1;
four fundamental arithmetic operations
I think this may be helpful -->
int a,b,i,j;
if (a>b)
{
int temp = a;
a = b;
b = temp;
}
for(i=a,j=b;i<j;i++,j--)
continue;
if(i==j)printf("%d\n", i);
else printf("%lf\n", (double)(i)-0.5);
Add them then multiply by 0.5 , no division involved.
If they're both integers, you can use a right shift:
int median = (a + b) >> 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 8 years ago.
Improve this question
I am using an array,i want to call 4 element from the array at the same time so i can use them in an equation ,any one knows how.
example
int a[10]={1,2,3,4,5,6,7,8};
I want 1234+15
how?
using C#:
1234 = 1*1000 + 2*100 + 3*10 + 4*1
double result = 0;
int ex = 3; // Math.Pow(10,3) = 1000.00
for(int i = 0; i < 4 0; i++)
{
result += a[i] * Math.Pow(10, ex);
ex--;
}
result += 15;