Question

In: Computer Science

Which codes add 1 to integer n? 1) n=n+1; 2) n++; 3)++n; 4) n+=1; 5) n=--n+2

Which codes add 1 to integer n?

1) n=n+1;

2) n++;

3)++n;

4) n+=1;

5) n=--n+2

Solutions

Expert Solution

Which codes add 1 to integer n:

Answer: All of the above

(1) n=n+1; (2)  n++; (3) ++n; (4) n+=1; (5)  n=--n+2;

Description:

(1) n = n + 1;

• The above statement Simply adds 1 to integer n as "n + 1".

• Example: Let's take integer n = 6 and apply n = n + 1 in the code, this will give the result n = 7.

Code:

Output:

(2) n++;

• The above statement is postfix or post-increment operator that adds 1 to integer n as "n++". This operator follows a rule 'use-then-change' means that n will be incremented first and then assigned to variable.

• Example: Let's take integer n = 6 and apply n++ in the code, this will give the result n = 7.

Code:

Output:

(3) ++n;

• The above statement is prefix or pre-increment operator that adds 1 to integer n as "++n". This operator follows a rule 'change-then-use' means that n will be assigned first to variable and then gets incremented.

• Example: Let's take integer n = 6 and apply ++n in the code, this will give the result n = 7.

Code:

Output:

(4) n+=1;

• The above statement is arithmetic assignment operator that adds 1 to integer n as "n+=1", which is simply n = n + 1.

• Example: Let's take integer n = 6 and apply n+=1 in the code, this will give result n = 7.

Code:

Output:

(5) n = --n+2;

• The above statement uses pre-decrement operator (--n) that will first decrement the n value by 1 and then adds 2 to it as "n = --n+2".

• Example: Let's take integer n = 6 and apply n= --n+2 in the code, this will give the result n = 7.

Code:

Output:


Related Solutions

Prove true or false. For each natural number n, ((n5/5)+(n^4/2)+(n^3/3)-(n/30)) is an integer
  Prove true or false. For each natural number n, ((n5/5)+(n^4/2)+(n^3/3)-(n/30)) is an integer
1. Prove or Disprove: If n is a nonnegative integer, then 5 | (2*4n + 3*9n)
1. Prove or Disprove: If n is a nonnegative integer, then 5 | (2*4n + 3*9n)
prove or disprove .if n is a non negative integer, then 5 divides 2 ⋅ 4^n...
prove or disprove .if n is a non negative integer, then 5 divides 2 ⋅ 4^n + 3⋅9^n.
What is the radius of convergence for (( 2x-3)^n) / (n+4)^2 * 5^n+1
What is the radius of convergence for (( 2x-3)^n) / (n+4)^2 * 5^n+1
Identify all allowable combinations of quantum numbers for an electron. n=3,n=3, ?=2,?=2, m?=2,m?=2, ms=−12ms=−12 n=5,n=5, ?=4,?=4,...
Identify all allowable combinations of quantum numbers for an electron. n=3,n=3, ?=2,?=2, m?=2,m?=2, ms=−12ms=−12 n=5,n=5, ?=4,?=4, m?=−1,m?=−1, ms=−12ms=−12 n=3,n=3, ?=−2,?=−2, m?=2,m?=2, ms=+12ms=+12 n=6,n=6, ?=6,?=6, m?=1,m?=1, ms=+12ms=+12 n=4,n=4, ?=3,?=3, m?=4,m?=4, ms=+12ms=+12 n=2,n=2, ?=0,?=0, m?=0,m?=0, ms=−1
4. (a) Suppose that τσ=(1 5 2 3)(4) and στ=(1 2 4 5)(3) in S5. If...
4. (a) Suppose that τσ=(1 5 2 3)(4) and στ=(1 2 4 5)(3) in S5. If σ1 = 2, find σ and τ. (b) In Sn, show that σ = τ if and only if σ(τ)^(−1) = ε. ε is the identity permutation. Must be written as a proof. (c) Let σ=(1 2 3) and τ=(1 2) in S3. Show that S3={ε,σ,σ^2,τ,τσ,τ(σ)^2} and that σ^3=ε=τ^2 and στ=τ(σ)^2, then fill out the multiplication table for S3.
Find the distances: A) Between ?1=〈2+2?,−1+?,−3?〉and ?2=〈4,−5−3?,1+4?〉 . B) Between the planes 2?−?+5?=0 and 2?−?+5?=5 ....
Find the distances: A) Between ?1=〈2+2?,−1+?,−3?〉and ?2=〈4,−5−3?,1+4?〉 . B) Between the planes 2?−?+5?=0 and 2?−?+5?=5 . C) From the point (1,2,3) to the line ?=〈−?,4−?,1+4?〉 .
5 + (3 * 4)^2 - 2 = 147             (5 + 3) * 4^2 -...
5 + (3 * 4)^2 - 2 = 147             (5 + 3) * 4^2 - 2 = 126             (5 + 3 )* (4^2 – 2) = 112             5 + (3 * 4^2) - 2 = 51 Which is the correct answer to using the "order of precedence" and why?
X 1 3 5 3 4 4 Y 2 5 4 3 4 6 A: Plot...
X 1 3 5 3 4 4 Y 2 5 4 3 4 6 A: Plot the date B: find the line of best fit C: determine ŷ AT x=3 D: Find r and r^2 E: explain r and r^2
1. is sin(pi/4) causal? 2. is sin(pi/4) stable? 3. is delta(n+1) causal? 4.  = ? 5. If...
1. is sin(pi/4) causal? 2. is sin(pi/4) stable? 3. is delta(n+1) causal? 4.  = ? 5. If function w [ n ] is convolved with , what will the result be? 6. if a system with signal length 4 is convolved with its own system response, what will the length of that signal be? 7. In an LTI system, x[n] * h[n]= y[n]. What is x[n-3] * h[n-2] =?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT