Subtraction of 2 Numbers: C

In this video tutorial you can learn the procedure followed in C programming to subtract two numbers.

Related Read:

 
#include < stdio.h >

int main()
{
    int a, b, c;

    printf("Enter value of a and b\n");
    scanf("%d%d", &a, &b);

    c = a - b;
    
    printf("Subtraction of %d and %d is %d\n", a, b, c);

    return 0;
}

Output:
Enter value of a and b
30
20
Subtraction of 30 and 20 is 10

You can write same program without using third variable to calculate subtraction of 2 numbers, as below:

 
#include < stdio.h >

int main()
{
    int a, b;

    printf("Enter value of a and b\n");
    scanf("%d%d", &a, &b);

    printf("Subtraction of %d and %d is %d\n", a, b, (a+b));

    return 0;
}

Output:
Enter value of a and b
30
20
Subtraction of 30 and 20 is 10

Note: Instead of int you can take float variables too. That would help in taking both integer as well as real values from the user as input.

Scanf(): For user input

In above c program we are asking user to enter the values for variable a and b. You can know more about scanf() method/function in this video tutorial: Using Scanf in C Program

Subtraction of Two Numbers: C Programming


[youtube https://www.youtube.com/watch?v=2cAMGtToJNE]

YouTube Link: https://www.youtube.com/watch?v=2cAMGtToJNE [Watch the Video In Full Screen.]


This video was produced as building block for our simple calculator application.

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Swap 2 numbers using Addition and Subtraction: C

In this video tutorial we shall learn how to swap two integer numbers without using a temporary variable and by simply making use of addition and subtraction.

Related Read:
Basic Arithmetic Operations In C
Swap 2 Numbers Using a Temporary Variable: C
Swap 2 Numbers Without Using a Temporary Variable: C

 
#include < stdio.h >

int main()
{
    int a, b;

    printf("Enter 2 integer numbers for, a and b\n");
    scanf("%d %d", &a, &b);

    printf("You entered a = %d and b = %d\n", a ,b);

    a = a + b;
    b = a - b;
    a = a - b;

    printf("After swapping a = %d, b = %d\n", a, b);

    return 0;
}

Output:
Enter 2 integer numbers, for a and b
30
20
You entered a = 30 and b = 20
After swapping a = 20, b = 30

Scanf(): For user input

In above c program we are asking user to enter the values for variable a and b. You can know more about scanf() method/function in this video tutorial: Using Scanf in C Program

Swap 2 numbers using only Addition and Subtraction: C


[youtube https://www.youtube.com/watch?v=mm4XJ2DEVS4]

YouTube Link: https://www.youtube.com/watch?v=mm4XJ2DEVS4 [Watch the Video In Full Screen.]


Swapping 2 Numbers In C using only addition and subtraction: Logic

In our above program we are asking user to enter integer value for a and b.
If the user enters 30 and 20 for a and b respectively. Then our program executes below logic to swap the values of variable a and b.

Step 1: Add values of a and b and store it in variable a.
i.e.,
a = a + b;
50 = 30 + 20;
So a = 50;

Step 2: Now subtract value of b from value of a and store it in variable b.
i.e.,
b = a – b;
30= 50 – 20; (value of a = 50 according to Step 1)
So value of variable b is now 30.

Step 3: Now subtract value of b from value of a and store it in variable a.
i.e.,
a = a – b;
20= 50 – 30; (Value of a = 50 from Step 1, and Value of b = 30 from Step 2)

Finally value of a = 20(from step 3) and value of b = 30(from step 2).

This is how we swap the value of variable a and b by just making use of addition and subtraction in C programming language.

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Basic Arithmetic Operations In C

Today lets learn about basic arithmetic operations in C programming language.

What we learn in this video tutorial?

We shall learn, addition, subtraction, multiplication, division and modular division in this video tutorial. And we’ll also learn how to use pow() method present in math.h library file.

 
#include < stdio.h >
#include < math.h >

int main()
{
    int   a = 10, b = 2;
    char  m = 'A', n = 'a';

    printf("\nAddition of a and b is %d\n", (a+b));
    printf("\nSubtraction of a and b is %d\n", (a-b));
    printf("\nMultiplication of a and b is %d\n", (a*b));
    printf("\nDivision of a and b is %d\n", (a/b));
    printf("\nModular Division of a and b is %d\n", (a%b));
    printf("\n3 to the power of 2 is %f\n", pow(3,2));

    printf("\nASIIC value of A and a is %d and %d\n\n", m, n);

    return 0;
}

Output:

Addition of a and b is 12
Subtraction of a and b is 8
Multiplication of a and b is 20
Division of a and b is 5
Modular Division of a and b is 0
3 to the power of 2 is 9.000000
ASIIC value of A and a is 65 and 97

Basic Arithmetic Operations In C


[youtube https://www.youtube.com/watch?v=4mjiL9uwPCg]

YouTube Link: https://www.youtube.com/watch?v=4mjiL9uwPCg [Watch the Video In Full Screen.]


Precedence and associativity of arithmetic operators

1st priority: * / %
2nd priority: + –
3rd priority: =

When there is tie between same priority operators then we check the associativity. For example, for * and /, associativity is same. i.e., left to right.

So in the expression: c = a * b + a / b;
a * b gets executed first.

ASCII values

When you try to apply Arithmetic Operation on characters(alphabets, character digits, special characters) its correspondent ASCII value gets operated on. ASCII value of A is 65 and ASCII value of a is 97. This is the reason why capital A and small letter a are treated differently in C programming language. Under the hood, they have different ASCII values.

Note 1: All the variables and constants are called operands. All the arithmetic symbols(+, -, /, *, %) are called operators. = is called assignment operator.

Note 2: Only one variable is allowed on the left hand side of the equation or expression.
Ex:
c = a + b is valid.
a + b = c is invalid.

Note 3: You can’t store a decimal value in a integer variable. So if the result of evaluation of an expression has decimal value and you’re assigning it to an integer variable, then only the integer part gets stored. Decimal part will be discarded. So be careful while choosing the data type of variables while performing arithmetic operation.

Note 4: We need to always, explicitly mention the arithematic operators.
Ex:
c = (a + b)(a – b) throws error.
c = (a + b) * (a – b); is the correct way in C programming.

Note 5: An integer value divided by an integer value gives back an integer value. A float value divided by a float value gives back a floating point value.

Note 6: Modular division gives reminder of division.
Ex:
c = 10 / 2; gives c = 5;
c = 10 % 2; give c = 0;

Also,
c = -3 % 2; gives c = -1
c = 3 % -2; gives c = 1

Modular division doesn’t take float values as its operands.
Ex: c = 3.0 % 2.0; gives error

Note 7: math.h library file has other mathematical operations related methods like: pow(), sin(), cos(), tan(), abs(), sqrt() etc.

We’ll look more arithmetic operators as and when we encounter them in programs.

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Addition/Subtraction of Complex Number: Java

Video Tutorial and Free source code: Addition and Subtraction of Complex numbers in Java. It also explains about Do Nothing Constructor. Complex numbers are of the form x+iy. Where x is the real part and iy is the imaginary part. In this program we are using parameterized constructor to copy the real and imaginary values … Continue reading “Addition/Subtraction of Complex Number: Java”

Video Tutorial and Free source code: Addition and Subtraction of Complex numbers in Java.
It also explains about Do Nothing Constructor.

Complex numbers are of the form x+iy. Where x is the real part and iy is the imaginary part.

In this program we are using parameterized constructor to copy the real and imaginary values of the objects created, into the local variables of the class calc.(float real, img;)

calc(float r, float i)
{
real = r;
img = i;
}

Passing values to parameterized constructor:

calc c1 = new calc(12.5F, 2.5F);
calc c2 = new calc(09.5F, 0.5F);

But since we are also creating a third object c3, which doesn’t pass any parameter values, we need to write a default constructor even though it doesn’t contain any definition in it. Such a constructor is called Do Nothing Constructor.

calc c3 = new calc(); //Not Passing any value, 
                      //hence it starts searching for default constructor.

This is Do Nothing Constructor:

calc() { }

Now the actual addition of complex number:
The return type is calc, as it has to return value to be stored in object c3(which is of type calc).
Here, we add the real part of c1 to the real part of c2. Imaginary part of c1 to imaginary part of c2, and return the value and display the result.

calc add(calc c2)
{
calc res = new calc();
 
res.real = real + c2.real;
res.img = img + c2.img;
 
return(res);
}

Since c1 object called the add method, we need not again write c1.real and c1.img Instead we can directly write real and img.

c3 = c1.add(c2);

Similarly, subtraction of complex numbers:
Here, we subtract the real part of c1 to the real part of c2. Imaginary part of c1 to imaginary part of c2, and return the value and display the result.

calc sub(calc c2)
{
calc res = new calc();
 
res.real = real - c2.real;
res.img  = img - c2.img;
 
return(res);
}

Call to Subtraction method:

c3 = c1.sub(c2);

Video Tutorial: Addition and Subtraction of Complex Number: Java


[youtube https://www.youtube.com/watch?v=tfiTMjwDqKM]

YouTube Link: https://www.youtube.com/watch?v=tfiTMjwDqKM [Watch the Video In Full Screen.]



Full Source Code:(Complex.java)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import java.lang.*;
 
class calc
{
float real, img;
 
calc() {}        // Do Nothing Constructor
 
calc(float r, float i)
{
real = r;
img = i;
}
 
void display()
{
System.out.println(real+" + i "+img);
}
 
calc add(calc c2)
{
calc res = new calc();
 
res.real = real + c2.real;
res.img = img + c2.img;
 
return(res);
}
 
calc sub(calc c2)
{
calc res = new calc();
 
res.real = real - c2.real;
res.img = img - c2.img;
 
return(res);
}
 
}
 
class Complex
{
public static void main(String args[])
{
calc c1 = new calc(12.5F, 2.5F);
calc c2 = new calc(09.5F, 0.5F);
 
System.out.println("C1 is: ");
c1.display();
System.out.println("C2 is: ");
c2.display();
 
calc c3 = new calc();
 
System.out.println("Addition of C1 and C2 is: ");
c3 = c1.add(c2);
c3.display();
 
System.out.println("Subtraction of C1 and C2 is: ");
c3 = c1.sub(c2);
c3.display();
}
}

Output:
C1 is:
12.5 + i 2.5

C2 is:
9.5 + i 0.5

Addition of C1 and C2 is:
22.0 + i 3.0

Subtraction of C1 and C2 is:
3.0 + i 2.0