This is my assignment but I don't know where to start and I need your help... give explanation of given 2 lines sum=sum+(i*rem) ; i=i*10;
give explanation of given 2 lines: sum=sum+(i*rem); i=i*10;
Collapse
X
-
Code:sum=sum+(i*rem); i=i*10;
Code:int a=15; int b=3; int rem=a/b;//rem=3 int sum=0; sum=sum+(i*rem);//is same as sum+=i*rem for(int i=0;i<3;i++){ i=i*10;//same as i*=10 cout<<i;//prints 0 10 20} for(int i=0;i<3;i++){ sum+=i*rem; cout<<sum;//prints 0 3 9} }
Comment
-
I'm sorry - I don't understand what this program is trying to do. Can you tell us?
Line 5 of the larger snippet doesn't belong: it uses i before that variable has been declared or initialized.
I would have guessed that the variable name rem was short for remainder, but line 3 assigns the quotient of a/b to rem.
The loop in lines 6-8 has nothing to do with a, b, or rem. It just prints the values of 0*10, 1*10, and 2*10. The point of this escapes me.
Knowing that rem is 3, the loop in lines 9-12 prints the values of 0+(0*3), 0+(0*3)+(1*3), and 0+(0*3)+(1*3)+( 2*3). The point of this escapes me.Comment
-
i=i*10
Code:#include<stdio.h> int main(){ int i=2; printf("before any operation i = %d",i); // o/p i=2 i=i*10; printf("after above operation operation i = %d",i);// o/p i=20 }
Code:#include<stdio.h> int main(){ int i=2,sum=0,rem,a=6,b=5; printf("before any operation i = %d",i); i=i*10; printf("after above operation operation i = %d",i); rem = a%b; //a%b yields reminder sum = sum +(i*rem); // sum = 0+(20 * 1) printf("after above operation operation sum = %d",sum);}
Comment
-
This is just a wild guess. Perhaps the intent is to demonstrate how the integer division and multiplication instructions (/ and % and *) work in mathematics as well as in C. I'm not sure how to relate this to the original question.
Code:void divideDemo(int dividend, int division); int multiplyDemo(int multiplier, int multiplicand); void divideDemo(int dividend, int divisor) { int quotient, remainder, checkValue; quotient = dividend / divisor; remainder = dividend % divisor; printf("divide: %d divided by %d = %d remainder %d\n", dividend, divisor, quotient, remainder); checkValue = multiplyDemo(quotient,divisor) + remainder; printf("divide: ((%d * %d) + %d = %d) == %d\n", quotient, divisor, remainder, checkValue, dividend); } int multiplyDemo(int multiplier, int multiplicand) int i, polarity, product; polarity = 1; if (multiplier < 0) { polarity = -polarity; multiplier = -multiplier; } if (multiplicand < 0) { polarity = -polarity; multiplicand = -multiplicand; } product = 0; for(i=0; i<multiplicand, i++) product += multiplier; if (polarity < 0) product = -product; printf("multiply: %d * %d = %d\n", multiplier, multiplicand, product); return product; }
Comment
Comment