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.
Question 1 Refer to the operations below: Add (10 + 5) Add (4+8) Add (7*2) Add...
Question 1 Refer to the operations below: Add (10 + 5) Add (4+8) Add (7*2) Add (90 – 3) Print list Print peek Remove an item from the list Print list 1.1 Implement the operations above into a Queue structure called q1. 1.2 Implement the operations above into a Stack structure called s1. Name your program Question1_1 for the queue structure and Question1_2 for the stack structure JAVA Language to be used. Please give step by step explanation on how...
coffee tea juice 3 4 5 5 4 3 4 4 4 5 1 2 4...
coffee tea juice 3 4 5 5 4 3 4 4 4 5 1 2 4 2 2 Do a One-way ANOVA by hand (at least once in your life!) …Is there a difference in attention for those who drink coffee, tea, or juice during an 8 a.m. class? Utilize the five steps of hypothesis testing to analyze the following data (p<.01). Attention Ratings (1=no attention- 5=full attention)
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?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT