Arithmetic/Integer: Difference between revisions

added division truncation & modulus sign info to task
(added division truncation & modulus sign info to task)
Line 1:
{{Task|Basic language learning}}[[Category:Arithmetic operations]]
{{basic data operation}}
Get two integers from the user, and then output the sum, difference, product, integer quotient and remainder of those numbers. Don't include error handling. For quotient, indicate how it truncates (e.g. towards 0, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
 
=={{header|Ada}}==
Line 75:
printf("a-b = %d\n", a-b);
printf("a*b = %d\n", a*b);
printf("a/b = %d\n", a/b); /* truncates towards 0 (in C99) */
printf("a%%b = %d\n", a%b); /* same sign as first operand (in C99) */
return 0;
}</lang>
Line 245:
putStrLn $ "a - b = " ++ show (a - b)
putStrLn $ "a * b = " ++ show (a * b)
putStrLn $ "a /`div` b = " ++ show (a `div` b) -- truncates towards negative infinity
putStrLn $ "a %`mod` b = " ++ show (a `mod` b) -- same sign as second operand
putStrLn $ "a `quot` b = " ++ show (a `quot` b) -- truncates towards 0
putStrLn $ "a `rem` b = " ++ show (a `rem` b) -- same sign as first operand
 
=={{header|J}}==
Line 267 ⟶ 269:
System.out.println("a - b = " + (a - b));
System.out.println("a * b = " + (a * b));
System.out.println("quotient of a / b = " + (a / b)); //integer division truncates decimaltowards places0
System.out.println("remainder of a / b = " + (a % b)); // same sign as first operand
}</lang>
 
Line 372 ⟶ 374:
Printf.printf "a - b = %d\n" (a - b);
Printf.printf "a * b = %d\n" (a * b);
Printf.printf "a / b = %d\n" (a / b); (* truncates towards 0 *)
Printf.printf "a mod b = %d\n" (a mod b) (* same sign as first operand *)</lang>
 
=={{header|Pascal}}==
Line 425 ⟶ 427:
print "Product: %d" % (x * y)
print "Quotient: %d" % (x / y) # or x // y for newer python versions.
# truncates towards negative infinity
print "Remainder: %d" % (x % y) # same sign as second operand
 
## Only used to keep the display up when the program ends
Line 497 ⟶ 500:
"Difference: #{x-y}",
"Product: #{x*y}",
"Quotient: #{x/y}", # truncates towards negative infinity
"Remainder: #{x%y}" # same sign as second operand</lang>
 
=={{header|Scheme}}==
Line 511 ⟶ 514:
(arithmetic 8 12)</lang>
quotient - truncates towards 0
remainder - same sign as first operand
modulo - same sign as second operand
prints this:
Line 547 ⟶ 553:
print ("a - b = " ^ Int.toString (a - b) ^ "\n");
print ("a * b = " ^ Int.toString (a * b) ^ "\n");
print ("a div b = " ^ Int.toString (a div b) ^ "\n"); (* truncates towards negative infinity *)
print ("a mod b = " ^ Int.toString (a mod b) ^ "\n"); (* same sign as second operand *)
print ("~a quot b = " ^ Int.toString (~Int.quot (a, b)) ^ "\n") ;(* unary negation, unusual notation compared totruncates othertowards languages0 *)
print ("a rem b = " ^ Int.toString (Int.rem (a, b)) ^ "\n"); (* same sign as first operand *)
print ("~a = " ^ Int.toString (~a) ^ "\n") (* unary negation, unusual notation compared to other languages *)
end</lang>
 
Line 656 ⟶ 664:
echo "a-b = " `expr $a - $b`
echo "a*b = " `expr $a \* $b`
echo "a/b mod b = " `expr $a %/ $b` # truncates towards 0
echo "a mod b = " `expr $a % $b` # same sign as first operand
 
(Notes: Using the ` (backtick operators, also available in most Bourne shells via the ''$(...)'' syntax) allows us to keep the results on their labels in the most efficient and portable way. The spaces around the operators in the ''expr'' command line arguments are required and the shell requires us to quote or escape the ''*'' character has shown, to prevent any possible "globbing" --- filename expansion of the ''*'' as a wildcard character.
Line 671 ⟶ 680:
echo "a-b = $(($a-$b))"
echo "a*b = $(($a*$b))"
echo "a/b mod b = $(($a%/$b))" # truncates towards 0
echo "a mod b = $(($a%$b))" # same sign as first operand
 
(Note: spaces inside the ''$((...))'' are optional and not required; the ''$((...))'' expressions can be inside the double quotes, but the `...` expressions could also have been enclosed in the double quotes in the previous example).
Anonymous user