What is Addition?

Adding two numbers in programming is essentially the same as adding two numbers in general arithmetic. A significant difference is that in programming, you need to pay attention to the data type of the variable that will hold the sum of two numbers.

In most programming languages, the plus sign (+) is the arithmetic operator that performs addition.

In the context of adding two numbers, the most common operations you will encounter are:

  • Adding two numbers and assigning the result to a variable
  • Setting the value of a variable to the sum of a number and a variable
  • Setting the value of a variable to the sum of two other variables

Adding Two Numbers

The following C code snippet adds 2 and 3, and displays the result:

#include <stdio.h>

int main() {

int a;

a = 2 + 3; // a becomes equal to the sum of 2 and 3

printf(“The result of the addition is %d.n”, a);

return 0;

}

Output: The result of the addition is 5.

Adding a Number and a Variable

Let’s add 3 to a variable “a” whose value is 2, and display the result:

#include <stdio.h>

int main() {

int a = 2;

int b = a + 3; // b becomes equal to the sum of a and 3

printf(“The result of the addition is %d.n”, b);

return 0;

}

Output: The result of the addition is 5.

Adding Two Variables

Finally, let’s add two variables whose values are 2 and 3 respectively—and display the result:

#include <stdio.h>

int main() {

int a = 2, b = 3;

int c = a + b; // c becomes equal to the sum of a and b

printf(“The result of the addition is %d.n”, c);

return 0;

}

Output: The result of the addition is 5.

Incrementing a Variable

Incrementing by 1

A special case of adding two numbers is adding 1 to a number—that is, incrementing a number. Incrementing is such a common and atomic operation that C—like many other programming languages—has two operators specifically for the purpose.

  • The post-increment operator ( ++) increments a variable after it is used in an expression.
  • The pre-increment operator (++ ) increments a variable before it is used in an expression.

Both operators are signified by “++”; the difference is in the location of the plus signs—as in the following example:

#include <stdio.h>

int main() {

int a = 2;

int b = a++; // a is incremented, but only after it is used

printf(“When we use the post-increment operator, b is assigned the value of a before it is incremented—which is %d. The incremented value of a is %d.n”, b, a);

a = 2;

b = ++a; // a is incremented before it is used

printf(“When we use the pre-increment operator, the value of b becomes %d, which is equal to the incremented value of a.n”, b);

return 0;

}

Output:

When we use the post-increment operator, b is assigned the value of a before it is incremented—which is 2. The incremented value of a is 3.

When we use the pre-increment operator, the value of b becomes 3—which is equal to the incremented value of a.

Incrementing by a Specific Value

In C and Java, the “+=” construct increments a value by a specified amount.

This statement:

a+=7;

means the same as this one:

a = a + 7

Similarly, the following two statements:

b=3; a+=b;

have the same effect as:

a = a + 3.

Data Type Considerations

As mentioned earlier, you need to pay attention to the data type of the variable that will hold the sum of two numbers. Let us use the example of the integer data type and the “float” floating-point data type to illustrate this.

Some languages allow you to use a variable of a floating-point type to hold an integer, even though you do not need to.

In a C program, when we say “float a = 7” and display “a,” you’ll see “7.000000”.

In Java, “double a = 7” makes “a” show up as “7.0.”

Some languages don’t, and some do, allow you to use a variable of the integer data type to hold a number with a decimal point such as 7.45.

In a C program, “int a = 7.45” assigns “a” a truncated value—that is, “a” will be assigned the value 7.

In Java, “int a = 7.45” brings up an error. For an integer value to be assigned a floating point number, you need to cast the floating point number thus: “int a = (int) 7.45.” The variable a then becomes equal to 7.

The same holds true for the sum of two variables. The variable that holds the sum should be of the appropriate data type. To summarize in the case of Java:

The sum of two integers can be stored in a floating-point type variable.

The sum of two numbers with decimal points, or of one integer and one number with a decimal point, cannot be stored in an integer data type unless the sum is cast as an integer.

The sum of two floating point numbers can, of course, be stored in a floating-point type.

The following code illustrates this using a Java program:

public class Main {

public static void main(String[] args) {

int a = 7; int b = 2; // use of integer data types to hold integers

double c = 2.34; double d = 3.21;

double e;

e = a + b; //sum of two integers stored in a floating-point variable

System.out.println ("When the sum of two integers is stored in a floating-point variable, it is displayed with a zero: " + e + ".");

// “a = b + c” and “a = c + d” would both bring up errors

a = (int) (c + d); //sum of two floating-point numbers cast as an integer

System.out.println ("Here’s the sum of c and d, two floating-point numbers, cast as an integer: " + a + ". Note that the actual sum, 5.55, is truncated.");

e = c + d; //sum of two floating-point numbers stored as a floating point number

System.out.println ("…And here’s the sum of the same two floating-point numbers displayed as a floating-point number: " + e + ".");

}

}

Output:

When the sum of two integers is stored in a floating-point variable, it is displayed with a zero: 9.0.

Here’s the sum of c and d, two floating point numbers, cast as an integer: 5. Note that the actual sum, 5.55, is truncated.

…And here’s the sum of the same two floating-point numbers displayed as a floating-point number: 5.55.

Let’s look at C program that illustrates this.

#include <stdio.h>

int main() {

int a = 7.45; // use of an integer data type to hold a decimal number

printf(“When 7.45 is stored in a variable as an integer, it becomes %d.nn”, a);

int b = 4; int c = 5;

float d = b + c; // use of the “float” data type to hold an integer

printf(“The sum of 4 and 5 is displayed as a floating point number in this way: %fnn”, d);

float e = 4.51; float f = 7.87;

float g = e + f; // use of the “float” data type to hold a floating point number

printf(“The floating-point sum of 4.51 and 7.87 is displayed with trailing zeroes: %fnn”, g);

float h = 4.51; float i = 7.87;

int j = h + i; // use of the integer data type to hold a floating point number

printf(“When the sum of 4.51 and 7.87 is stored as an integer, the digits after the decimal point are discarded: %dnn”, j);

int k = 4; float l = 3.27;

int m = k + l; // use of the integer data type to hold the sum of an integer and a floating point number

float n = k + l; // use of the “float” data type to hold the sum of an integer and a floating point number

printf(“The sum of 4—an integer—and 3.27, displayed as an integer, has the digits after the decimal point discarded: %dnThe same sum as a floating point number is displayed this way: %f”, m, n);

return 0;

}

Output:

When 7.45 is stored in a variable as an integer, it becomes 7.

The sum of 4 and 5 is displayed as a floating point number in this way: 9.000000                              

The floating-point sum of 4.51 and 7.87 is displayed with trailing zeroes: 12.380000              

When the sum of 4.51 and 7.87 is stored as an integer, the digits after the decimal point are discarded: 12   

The sum of 4—an integer—and 3.27, displayed as an integer, has the digits after the decimal point discarded: 7

The same sum as a floating point number is displayed this way: 7.270000

Common Mistakes

Here’s a short list of potential coding errors to be aware of in the context of adding numbers, taking inputs for numbers to be added, and producing the results of addition as outputs.

1. Remember variable type in printf and scanf statements in C.
Say the result of an addition is a variable “a,” which is an integer, and “a” is specified as a floating-point number in a “printf” statement as in the following:

printf("The result of the addition is %f.n", a);

The output might—with some compilers—be 0.000000. The other way round, say a floating point number is specified as an integer in a “printf” statement as in the following:

printf("The result of the addition is %d.n", a);

The output will vary from compiler to compiler—and will usually not make sense. In some cases, the compiler might just generate an error and no output.

It’s a similar case with scanf. When you specify that scanf should receive an integer input and it receives a floating-point number—or vice-versa—the result could be 0 as output, an unexpected number as output, a compiler warning followed by some output, or only a compiler error.

2. Remember what the “+=” operator does.

You’ve seen that a+= 1 means the same as a = a + 1, that is, the number or variable that comes after the “+=” represents the increment.

A common error among beginners is to write a+= a+1, which—logically—means the same as a = a + a + 1, not a + 1.

3. Remember the relationship between casting, truncating, and rounding.

As you’ve seen, a floating-point number cast as an integer gets truncated (and not rounded). Similarly, the sum of two floating-point numbers cast as an integer gives the truncated sum. Say “a” equals 2.9 and “b” equals 5.8. Their sum is 8.7, which rounds to 9. You might mistakenly expect that this sum, cast as an integer in the following code, will result in 9—or perhaps 8:

float num1=2.9; float num2=5.8;

int sum = (int) num1+num2;

printf("The sum is %d.n", sum);

Instead, it results in 7. This is because during the casting operation on the sum, each number is cast independently—so 2.9 becomes 2, 5.8 becomes 5, and you get their (integer) as 7.

Context and Applications

This topic is significant in the professional exams for both undergraduate and graduate courses, especially for

  • Bachelors in Technology (Computer Science)
  • Masters in Technology (Computer Science)
  • Bachelors in Science (Computer Science)
  • Masters in Science (Computer Science)

Want more help with your computer science homework?

We've got you covered with step-by-step solutions to millions of textbook problems, subject matter experts on standby 24/7 when you're stumped, and more.
Check out a sample computer science Q&A solution here!

*Response times may vary by subject and question complexity. Median response time is 34 minutes for paid subscribers and may be longer for promotional offers.

Search. Solve. Succeed!

Study smarter access to millions of step-by step textbook solutions, our Q&A library, and AI powered Math Solver. Plus, you get 30 questions to ask an expert each month.

Tagged in
EngineeringComputer Science

Programming

Introduction to Coding

Addition of Two Numbers Homework Questions from Fellow Students

Browse our recently answered Addition of Two Numbers homework questions.

Search. Solve. Succeed!

Study smarter access to millions of step-by step textbook solutions, our Q&A library, and AI powered Math Solver. Plus, you get 30 questions to ask an expert each month.

Tagged in
EngineeringComputer Science

Programming

Introduction to Coding