Assignment Operators in C

In this video tutorial we show Compound Assignment Operators in C programming language.

Related Read:
Simple Calculator Application In C

Assignment Operators

If a and c are the operands.

c += a; is equal to writing c = c + a;
c -= a; is equal to writing c = c – a;
c *= a; is equal to writing c = c * a;
c /= a; is equal to writing c = c / a;
c %= a; is equal to writing c = c % a;

Assignment Operator +=

  1.    
  2. #include < stdio.h >  
  3.   
  4. int main()  
  5. {  
  6.     int a = 20, c = 30;  
  7.   
  8.     c += a ; // c = c + a;  
  9.   
  10.     printf("%d\n\n", c);  
  11.   
  12.     return 0;  
  13. }  

Output:
50

Assignment Operator -=

  1.    
  2. #include < stdio.h >  
  3.   
  4. int main()  
  5. {  
  6.     int a = 20, c = 30;  
  7.   
  8.     c -= a ; // c = c - a;  
  9.   
  10.     printf("%d\n\n", c);  
  11.   
  12.     return 0;  
  13. }  

Output:
10

Assignment Operator *=

  1.    
  2. #include < stdio.h >  
  3.   
  4. int main()  
  5. {  
  6.     int a = 20, c = 30;  
  7.   
  8.     c *= a ; // c = c * a;  
  9.   
  10.     printf("%d\n\n", c);  
  11.   
  12.     return 0;  
  13. }  

Output:
600

Assignment Operator /=

  1.    
  2. #include < stdio.h >  
  3.   
  4. int main()  
  5. {  
  6.     int a = 2, c = 20;  
  7.   
  8.     c /= a ; // c = c / a;  
  9.   
  10.     printf("%d\n\n", c);  
  11.   
  12.     return 0;  
  13. }  

Output:
10

Assignment Operator %=

  1.    
  2. #include < stdio.h >  
  3.   
  4. int main()  
  5. {  
  6.     int a = 2, c = 20;  
  7.   
  8.     c %= a ; // c = c % a;  
  9.   
  10.     printf("%d\n\n", c);  
  11.   
  12.     return 0;  
  13. }  

Output:
0

Compound Assignment Operators in C Programming Language



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


You can also learn about Logical and Relational Operators in C:
Relational Operators In C
Logical Operators In C

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

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

Simple Calculator Program using Switch Case: C

In this video tutorial we shall perform Addition, Subtraction, Multiplication and Division of numbers based on user input, using switch case statement(decision control statement).

Related Read:
Basic Arithmetic Operations In C
Addition of 2 Numbers: C
Subtraction of 2 Numbers: C
Multiplication of 2 Numbers: C
Division of 2 Numbers: C
else if statement in C

We had written same calculator program using else-if clause. Same program has been modified to use Switch case in this program.
Simple Calculator Application In C

Simple Calculator using Switch Case: C Program



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


Working of Calculator Program
We display list of operation one can perform. That is,

1. Addition
2. Subtraction
3. Multiplication
4. Division

We ask the user to input his / her choice for arithmetic operation. If the user selects 1, then we ask the user to enter 2 integer numbers to perform addition operation. Once the user enters 2 integer numbers we add and display the result on the screen.

If the user enters wrong choice, we ask the user to enter proper choice.

Calculator Program using Switch Case: C Program

  1. #include < stdio.h >  
  2.   
  3. int main()  
  4. {  
  5.     int a, b;  
  6.     char choice;  
  7.   
  8.     printf("Enter your choice\n");  
  9.     printf("a. Addition\nb. Subtraction\nc. Multiplication\nd. Division\n");  
  10.     scanf("%c", &choice);  
  11.   
  12.   
  13.    printf("Enter 2 integer numbers\n");  
  14.    scanf("%d %d", &a, &b);  
  15.   
  16.   
  17.     switch(choice)  
  18.     {  
  19.         case 'a': printf("%d + %d = %d\n", a, b, (a+b));  
  20.                 break;  
  21.   
  22.         case 'b': printf("%d - %d = %d\n", a, b, (a-b));  
  23.                 break;  
  24.   
  25.         case 'c': printf("%d x %d = %d\n", a, b, (a*b));  
  26.                 break;  
  27.   
  28.         case 'd'if( b != 0)  
  29.                     printf("%d / %d = %d\n", a, b, (a/b));  
  30.                 else  
  31.                     printf("Number can't be divided by 0\n");  
  32.                 break;  
  33.   
  34.         default: printf("You entered wrong choice\n");  
  35.                  break;  
  36.     }  
  37.   
  38.     return 0;  
  39. }  

Output
Enter your choice
a. Addition
b. Subtraction
c. Multiplication
d. Division
c
Enter 2 integer numbers
5
10
5 x 10 = 50

In above program we are asking user to enter character a or b or c or d to perform addition, subtraction, multiplication and division operations respectively.

  1. #include < stdio.h >  
  2.   
  3. int main()  
  4. {  
  5.     int a, b, choice;  
  6.   
  7.     printf("Enter your choice\n");  
  8.     printf("1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n");  
  9.     scanf("%d", &choice);  
  10.   
  11.     if( choice > 4 )  
  12.     {  
  13.         printf("Select with in the range!\n");  
  14.     }  
  15.     else  
  16.     {  
  17.         printf("Enter 2 integer numbers\n");  
  18.         scanf("%d %d", &a, &b);  
  19.     }  
  20.   
  21.   
  22.     switch(choice)  
  23.     {  
  24.         case 1: printf("%d + %d = %d\n", a, b, (a+b));  
  25.                 break;  
  26.   
  27.         case 2: printf("%d - %d = %d\n", a, b, (a-b));  
  28.                 break;  
  29.   
  30.         case 3: printf("%d x %d = %d\n", a, b, (a*b));  
  31.                 break;  
  32.   
  33.         case 4: if( b != 0)  
  34.                     printf("%d / %d = %d\n", a, b, (a/b));  
  35.                 else  
  36.                     printf("Number can't be divided by 0\n");  
  37.                 break;  
  38.   
  39.         default: printf("You entered wrong choice\n");  
  40.                  break;  
  41.     }  
  42.   
  43.     return 0;  
  44. }  

Output:
Enter your choice
1. Addition
2. Subtraction
3. Multiplication
4. Division
4
Enter 2 integer numbers
10
2
10 / 2 = 5

Here we are asking user to enter 1 or 2 or 3 or 4 to choose the arithmetic operation.

  1. #include<stdio.h>  
  2.   
  3. int main()  
  4. {  
  5.     int a, b;  
  6.     char choice;  
  7.   
  8.     printf("Enter your choice\n");  
  9.     printf("1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n");  
  10.     scanf("%c", &choice);  
  11.   
  12.     printf("Enter 2 integer numbers. Format: a + b\n");  
  13.     scanf("%d %c %d", &a, &choice, &b);  
  14.   
  15.   
  16.   
  17.     switch(choice)  
  18.     {  
  19.         case '+': printf("%d + %d = %d\n", a, b, (a+b));  
  20.                 break;  
  21.   
  22.         case '-': printf("%d - %d = %d\n", a, b, (a-b));  
  23.                 break;  
  24.   
  25.         case '*': printf("%d x %d = %d\n", a, b, (a*b));  
  26.                 break;  
  27.   
  28.         case '/'if( b != 0)  
  29.                     printf("%d / %d = %d\n", a, b, (a/b));  
  30.                 else  
  31.                     printf("Number can't be divided by 0\n");  
  32.                 break;  
  33.   
  34.         default: printf("You entered wrong choice\n");  
  35.                  break;  
  36.     }  
  37.   
  38.     return 0;  
  39. }  
  40. </stdio.h>  

Output:
Enter your choice
1. Addition
2. Subtraction
3. Multiplication
4. Division
1
Enter 2 integer numbers. Format: a + b
50 + 60
50 + 60 = 110

Here we ask the user to enter values as well as the operation to be performed. Operands and operator.

Note 1: There is no need of curly braces inside case.
Note 2: If a case doesn’t end with break statement, then the execution continues and the block of code present inside next case will also get executed.
Note 3: default case is optional. And the code inside it executes only when non of the cases match.
Note 4: Here switch, case, break, default are all keywords / reserve words.

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

Simple Calculator Application In C

In this video tutorial we shall perform Addition, Subtraction, Multiplication and Division of numbers based on user input.

Related Read:
Basic Arithmetic Operations In C
Addition of 2 Numbers: C
Subtraction of 2 Numbers: C
Multiplication of 2 Numbers: C
Division of 2 Numbers: C
else if statement in C

Simple Calculator Program In C



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


Working of Calculator Program
We display list of operation one can perform. That is,

1. Addition
2. Subtraction
3. Multiplication
4. Division

We ask the user to input his / her choice for arithmetic operation. If the user selects 1, then we ask the user to enter 2 integer numbers to perform addition operation. Once the user enters 2 integer numbers we add and display the result on the screen.

If the user enters wrong choice, we ask the user to enter proper choice.

Student Grade Calculation using else-if clause

  1.    
  2. #include < stdio.h >  
  3.   
  4. int main()  
  5. {  
  6.     int a, b, choice;  
  7.   
  8.     printf("Enter your choice\n");  
  9.     printf("1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n\n");  
  10.     scanf("%d", &choice);  
  11.   
  12.     if(choice > 4)  
  13.     {  
  14.         printf("You entered wrong choice\n");  
  15.     }  
  16.     else  
  17.     {  
  18.         printf("Enter 2 integer numbers\n");  
  19.         scanf("%d %d", &a, &b);  
  20.     }  
  21.   
  22.   
  23.     if(choice == 1)  
  24.     {  
  25.         printf("Addition of %d and %d is %d\n", a, b, (a+b));  
  26.     }  
  27.     else if(choice == 2)  
  28.     {  
  29.         printf("Subtraction of %d and %d is %d\n", a, b, (a-b));  
  30.     }  
  31.     else if(choice == 3)  
  32.     {  
  33.         printf("Multiplication of %d and %d is %d\n", a, b, (a*b));  
  34.     }  
  35.     else if(choice == 4)  
  36.     {  
  37.         if(b != 0)  
  38.             printf("Division of %d and %d is %d\n", a, b, (a/b));  
  39.         else  
  40.             printf("Number can not be divided by 0\n");  
  41.     }  
  42.     else  
  43.     {  
  44.         printf("Please enter any of these no 1, 2, 3, 4\n");  
  45.     }  
  46.   
  47.   
  48.     return 0;  
  49. }  

Output 1:
Enter your choice
1. Addition
2. Subtraction
3. Multiplication
4. Division

1
Enter 2 integer numbers
3
2
Addition of 3 and 2 is 5

Output 2:
Enter your choice
1. Addition
2. Subtraction
3. Multiplication
4. Division

2
Enter 2 integer numbers
8
3
Subtraction of 8 and 3 is 5

Output 3:
Enter your choice
1. Addition
2. Subtraction
3. Multiplication
4. Division

3
Enter 2 integer numbers
5
5
Multiplication of 5 and 5 is 25

Output 4:
Enter your choice
1. Addition
2. Subtraction
3. Multiplication
4. Division

4
Enter 2 integer numbers
10
2
Division of 10 and 2 is 5

Output 5:
Enter your choice
1. Addition
2. Subtraction
3. Multiplication
4. Division

4
Enter 2 integer numbers
0
12
Division of 0 and 12 is 0

Output 6:
Enter your choice
1. Addition
2. Subtraction
3. Multiplication
4. Division

4
Enter 2 integer numbers
10
0
Number can not be divided by 0

Output 7:
Enter your choice
1. Addition
2. Subtraction
3. Multiplication
4. Division

50
You entered wrong choice
Please enter any of these no 1, 2, 3, 4

Note: In division section(that is, choice 4) you can see nesting of if else statement. You can know more about nested if-else statements in this program: Nested if else Statement In C

Note: We can write the same program within while loop and ask the user if he wants to continue working inside calculator application. This avoids the need to exit and re-run the program. We’ll publish the code and video tutorial for the same in coming days. Stay subscribed to our YouTube Channel and blog.

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

Multiplication of 2 Numbers: C

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

Related Read:

  1.    
  2. #include < stdio.h >  
  3.   
  4. int main()  
  5. {  
  6.     int a, b, c;  
  7.   
  8.     printf("Enter 2 numbers for multiplication\n");  
  9.     scanf("%d %d", &a, &b);  
  10.   
  11.     c = a * b;  
  12.   
  13.     printf("Multiplication of %d and %d is %d\n", a, b, c);  
  14.   
  15.     return 0;  
  16. }  

Output:
Enter 2 numbers for multiplication
25
5
Multiplication of 25 and 5 is 125

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

  1.    
  2. #include < stdio.h >  
  3.   
  4. int main()  
  5. {  
  6.     int a, b;  
  7.   
  8.     printf("Enter 2 numbers for multiplication\n");  
  9.     scanf("%d %d", &a, &b);  
  10.   
  11.     printf("Multiplication of %d and %d is %d\n", a, b, (a*b));  
  12.   
  13.     return 0;  
  14. }  

Output:
Enter 2 numbers for multiplication
25
5
Multiplication of 25 and 5 is 125

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

Multiplication of Two Numbers: C Programming



YouTube Link: https://www.youtube.com/watch?v=AFbtnOAH7eE [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 Multiplication and Division: 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 multiplication and division.

Related Read:
Basic Arithmetic Operations In C
Swap 2 Numbers Using a Temporary Variable: C
Swap 2 Numbers Without Using a Temporary Variable: C
Swap 2 numbers using Addition and Subtraction: C

  1.    
  2. #include < stdio.h >  
  3.   
  4. int main()  
  5. {  
  6.     int a, b;  
  7.   
  8.     printf("Enter 2 integer numbers\n");  
  9.     scanf("%d %d", &a, &b);  
  10.   
  11.     printf("You entered a = %d and b = %d\n", a, b);  
  12.   
  13.     a = a * b;  
  14.     b = a / b;  
  15.     a = a / b;  
  16.   
  17.     printf("After swapping a = %d and b = %d\n", a ,b);  
  18.   
  19.     return 0;  
  20. }  

Output:
Enter 2 integer numbers
3
5
You entered a = 3 and b = 5
After swapping a = 5 and b = 3

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 Multiplication and Division: C



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


Swapping 2 Numbers In C using only Multiplication and Division: Logic

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

Step 1: Multiply values of a and b and store it in variable a.
i.e.,
a = a * b;
50= 10 * 5;
So a = 50;

Step 2: Now divide value of b from value of a and store it in variable b.
i.e.,
b = a / b;
10= 50 / 5; (value of a = 50 according to Step 1)
So value of variable b is now 10.

Step 3: Now divide value of b from value of a and store it in variable a.
i.e.,
a = a / b;
5= 50 / 10; (Value of a = 50 from Step 1, and Value of b = 10 from Step 2)
Now the value of a = 5;

Finally value of a = 5(from step 3) and value of b = 10(from step 2).

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

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