What does the remainder operation (x % y) do when x < y? [closed] - c

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
This program correctly prints whether a number is even or odd ...
#include <stdio.h>
int main(void)
{
int n;
printf("Please enter a number:");
scanf("%d", &n);
if(n % 2 == 0)
printf("%d is even", n);
else
printf("%d is odd",n);
return 0;
}
I don't understand how n % 2 can give a meaningful result when n is less than two. % is the remainder operation, right? If n is less than two, how can you divide it by two at all?

I am unable to understand the logic of n%2==0. If user input a value less than 2. Then how it give us correct answer?
The operator % performs the modulus (or remainder) operation. The remainder of dividing a number by 2 (when that number is less than 2) is the number itself (with the quotient being 0). For example, one divided by two has a quotient of 0 and a remainder of 1 so 1%2 = 1.

Related

complex hex numbers in C [closed]

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 5 years ago.
Improve this question
Is there an option to store a hex complex number in c?
The remainder of a number divided by 3 equals to the sum of its digits modulo 3.
Once you calculate the remainders for the two numbers (not need to represent each number's value), sum those. If result modulo 3 is zero, the sum of the number is a multiplication of 3.
Well I guess you are not getting the problem. Rather getting the input is easier but processing it is not.
So no type would be big enough to accurately hold the value - these are large. Why not store it as string?
You can store it as a char array and use fgets for that (this is only if you want to print the number otherwise not needed). You can use getchar() also and do the sum as shown in the proof here.
After doing it, just do one thing - check each digit-char and then calculate it's sum mod 3. That way you will get the value of the result and keep it adding. (The resultant mod sum tells you about the divisibility). That is what you want exactly.
What I meant is?
(A + B) mod 3
= ( A(n)A(n-1)A(n-2)...A(1)A(0)
+ B(m)B(m-1)B(m-2)...B(1)B(0) ) mod 3
= ( [ A(n) + A(n-1) + A(n-2) + ... + A(1) + A(0) ] mod 3
+ [ B(m) + B(m-1) + B(m-2) + ... + B(1) + B(0) ] mod 3 ) mod 3
Rules:
if a≡b (mod m) and c≡d (mod m) then
a+c ≡ b+d (mod m) and
ac ≡ bd (mod m)
Example code
#include <stdio.h>
int main(void){
int c,sum = 0;
while(isdigit(c = getchar()))
sum+=(c-'0'),sum%=3;
while(isdigit(c = getchar()))
sum+=(c-'0'),sum%=3;
printf("%s\n", sum?"Non-divisible":"divisible");
return 0;
}

C - turn 3.00 into 3 [closed]

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 6 years ago.
Improve this question
I've got multiple float numbers to print:
{3.00, 3.20, 2.23, 5.00, 3.40 }
They all have 2 decimal places, however if there are two zero's after a decimal, it needs to be displayed as a whole number.
How can I convert 3.00 into 3 and 5.00 into 5?
it is conversion not printing
There is no way to directly (i.e. using only the format specifier) print either two decimals or zero for a number ending in e.g. 3.20.
You need to check if the number ends with .00 beforehand; for example:
if((int)round(num * 100) % 100 == 0) // if decimals are `.00`
printf("%.0f", num);
else // not `.00`
printf("%.2f", num);
If 3.2 is okay for 3.20, then the %g specifier might also be of some use.
Just assign them to an int array or just type cast them in int for your purpose.
int i = 3.00; //lhs is int and rhs is double i will store 3 only
(int)3.00 //cast
printing them with %.f will also work
I think you cannot do this with a general format specifier or similar. You might need to treat each number separately and adjust the printf accordingly.
First you need to detect if you have digits that are 0.
You could do it like this
if (fabs(number - round(number)) < 0.01)
printf("%.2f", number);
else
printf("%d", (int) number);
This is just meant as a hint in the right direction. You might improve it a bit on your own.
Edit:
I assume that you want two digits for 3.20 and not 3.2 only.
Casting to integer type will truncate the positive integers down to integer part:
float f = 2.00;
printf("f: [%f], [%d]", f, (int) f);
f: [2.000000], [2]
If you want to leave 2.50 as 2.5 then checking for .00 is necessary:
if( (int) round(num * 100) % 100 == 0) // if decimals are .00
printf("%.0f", num); // truncate all
else // not .00
printf("%.2f", num); // truncate to 2 decimal places

How can i reduce the complexity of this code(given below)? [closed]

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
Problem statement:
Find the sum of all the multiples of 3 or 5 below N.
Input Format:
First line contains T that denotes the number of test cases. This is followed by T lines, each containing an integer,N.
Constraints :
1 <= T <= 10^5
1 <= N <= 10^9
Output Format:
For each test case, print an integer that denotes the sum of all the multiples of 3 or 5 below N.
This is my code :
#include <stdio.h>
int main() {
long t,i,x;
scanf("%ld",&t);
long y[t];
for(i=0; i<t; i++) {
scanf("%ld",&x);
long j,k,sum= 0;
if(x<=3)
sum= 0;
else if(x<=5)
sum= 3;
else {
for(j=3; j<x; j+=3)
sum= sum + j;
for(j=5; j<x; j+=5)
if(j%3!=0)
sum = sum + j;
}
y[i] = sum;
}
for(i=0; i<t; i++) {
printf("%ld\n",y[i]);
}
return 0;
}
There is a solution with a time complexity of O(T):
Use the formula for sum of integers 1+2+3+...+n = n*(n+1)/2.
Also note that 3+6+9+...+(3*n) = 3*(1+2+3+...+n) = 3*n*(n+1)/2.
Figure out how many numbers divisible by 3 there are. Calculate their sum.
Figure out how many numbers divisible by 5 there are. Calculate their sum.
Figure out how many numbers divisible by 15 (=3*5) there are. Calculate their sum.
Total sum is sum3 + sum5 - sum15. The numbers divisible by both 3 and 5 (hence by 15) are both in sum3 and in sum5, so unless we subtract sum15 they would be counted twice.
Note that the sum will overflow a 32 bit integer, so make sure you use a 64 bit integer type.
You can achieve constant complexity if you employ the Ancient Power of Mathematics.
First figure out how many numbers divisible by three there are.
Compute their sum.
(You do not need any loops for this, you only need elementary arithmetic.)
Repeat for five and fifteen.
The total is a simple expression involving those three sums.
The details left as an exercise for the reader.

Multiplying two large numbers [closed]

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 7 years ago.
Improve this question
I have this question that requires to multiply two large numbers . I thought of adding the first number A , B times ( B is the second number) . I made the algorithm for adding two large numbers . so I thought this would work . the question is , would it take a long time to do this algorithm ? , adding a number to itself a lot of times ?
the question is , would it take a long time to do this algorithm ? , adding a number to itself a lot of times ?
Yes. That's a very slow method of multiplying numbers as you need to to a additions when you add b to itself a times. For better performance and still a reasonably simple algorithm, consider a shift-and-add procedure like this (multiplying a and b, putting the result in q):
q ← 0, i ← 0
if 2i > a then return q
if a & (1 &ll; i) then q ← q + (b &ll; i)
i ← i + 1
goto 2
Fast algorithms for this kind of problem are Karatsuba multiplication, Toom-Cook multiplication and Schönhage-Strassen multiplication.
Check this algorithm:
long long multiply(long long a, long long b)
{
if(a < b)
swap(a, b);
long long c = 0;
for(int i = 0; (1ll << i) <= b; ++i)
{
if(((b >> i) & 1ll) == 1ll)
{
c += a << i;
}
}
return c;
}
It works in logarithmic speed of min(a, b).
If your numbers are really large, Fast Fourier Transform (https://en.wikipedia.org/wiki/Fast_Fourier_transform) and Karatsuba algorithm (https://en.wikipedia.org/wiki/Karatsuba_algorithm) might help you.

Appending any digit in integer value using bitwise operation [closed]

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 want to append any static single digit in given number using bitwise operation.
Let the static number is 1 ,
If the number is
2 => 12
31 => 131
24 => 124
11 => 111
Is it possible to do..?
Here why I am strict with bitwise means , I want to maintain as integer values.
If your dbms supports basic math functions (Oracle for instance), you can use:
SELECT NUMBER + Power(10, Floor(Log(10, NUMBER)) + 1)
FROM TABLE;
If not you could get away with a string trick like:
SELECT TO_NUMBER('1' || TO_CHAR(NUMBER))
FROM TABLE;
(using Oracle)
Bitwise operators won't work that well on decimal numbers since powers of two and powers of ten don't mesh well (I have no idea what the database tag is for here, this seems to be totally unrelated).
If you want a function to add a 1 to the left of an arbitrary number as you seem to indicate, you can use the following algorithm:
def prefixWithOne (n):
if n == 0:
return 10
addNum = 1
testNum = n
while testNum > 0:
testNum = int (testNum / 10)
addNum = addNum * 10
return addNum + n
For example, the following C code will do that:
unsigned int prefixWithOne (unsigned int n) {
unsigned int testNum = n;
unsigned int addNum = 1;
if (n == 0) return 10;
while (testNum > 0) {
testNum /= 10;
addNum *= 10;
}
return addNum + n;
}
(with the usual caveats about watching out for overflow).

Resources