C Program To Add Two Numbers using Pointers

Lets write a C program to add 2 numbers using pointer and function.

In this video tutorial we will show both ways of adding 2 numbers using pointers: 1. Using function 2. Without using function.

Related Read:
Basics of Pointers In C Programming Language
Function / Methods In C Programming Language

Video Tutorial: C Program To Add Two Numbers using Pointers


[youtube https://www.youtube.com/watch?v=0wBPwxsr6-U]

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


Source Code: C Program To Add Two Numbers using Pointers and Without using Function

#include<stdio.h>

int main()
{
    int a, b, c, *ptr1, *ptr2;

    ptr1 = &a;
    ptr2 = &b;

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

    c = *ptr1 + *ptr2;

    printf("Using *ptr1 + *ptr2 : %d + %d = %d\n", a, b, c);

    c = *(&a) + *(&b);

    printf("Using  *(&a) + *(&b) : %d + %d = %d\n", a, b, c);

    return 0;
}

Output:
Enter 2 numbers
25
25
Using *ptr1 + *ptr2 : 25 + 25 = 50
Using (&a) + *(&b) : 25 + 25 = 50

Logic To Add Two Numbers using Pointers

Here we are storing the address of variable a in pointer variable ptr1 and address of variable b in pointer variable ptr2.

We know that any address preceded by * would fetch the value present at that address. So we use following code to get the addition of 2 numbers result using pointers.

c = *ptr1 + *ptr2;

Source Code: C Program To Add Two Numbers using Pointer and Function

#include<stdio.h>

void addition(int, int, int*);

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

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

    addition(a, b, &c);

    printf("%d + %d = %d\n", a, b, c);

    return 0;
}

void addition(int x, int y, int *z)
{
    *z = x + y;
}

Output:
Enter 2 numbers
10
40
10 + 40 = 50

Logic To Add Two Numbers using Pointers and Function

We ask the user to enter 2 numbers and store it inside address of variables a and b. Next we pass the values of a and b and address of variable c to a function called addition(). And then print the result in main method itself.

The motive is to change the value present at address of variable c. That way we can print the result directly inside main method itself, without our function addition() returning any value back to calling function(main()).

Inside addition() method, we add the values passed in first 2 arguments and then store the result back in the address location sent to addition() function as third argument. Function addition() doesn’t return any value, so it has void as its return type.

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

C Program To Find Sum of Series 1/1! + 2/2! + 3/3! + …. + n/n!

Lets write a C program to add first seven terms of the following series 1 / 1! + 2 / 2! + 3 / 3! + ….. + 7 / 7!

We’ve also written the C program to ask the user to enter the number of terms of the series that has to be added. You can find code to both of it below.

Related Read:
For Loop In C Programming Language
while loop in C programming
C Program To Find Factorial of a Number using For Loop

C Program To Find Sum of Series 1/1! + 2/2! + 3/3! + …. + n/n!


[youtube https://www.youtube.com/watch?v=Nok-OqhKpDY]

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


Source Code: C Program To Find Sum of Series 1/1! + 2/2! + 3/3! + …. + 7/7!

 

int main()
{
    int num = 1, count;
    float sum = 0.0, fact;

    while(num <= 7)
    {
        fact = 1;
        for(count = 1; count <= num; count++)
        {
            fact = fact * count;
        }

        sum = sum + (num / fact);

        num++;
    }

    printf("Sum of series is %f\n", sum);

    return 0;
}

Output:
Sum of series is 2.718056

In above code, while loop iterates from 1 to 7. For each iteration for loop calculates the factorial of the selected number – which is present in variable num. Outside for loop, we add the individual series elements. At the end of while loop execution, variable sum will have sum of 7 terms of the series.

Source Code: C Program To Find Sum of Series 1/1! + 2/2! + 3/3! + …. + n/n!

 

int main()
{
    int num = 1, count, limit;
    float sum = 0.0, fact;

    printf("Enter the number of terms\n");
    scanf("%d", &limit);

    while(num <= limit)
    {
        fact = 1;
        for(count = 1; count <= num; count++)
        {
            fact = fact * count;
        }

        sum = sum + (num / fact);

        num++;
    }

    printf("Sum of %d terms of series is %f\n", limit, sum);

    return 0;
}

Output 1:
Enter the number of terms
7
Sum of 7 terms of series is 2.718056

Output 2:
Enter the number of terms
8
Sum of 8 terms of series is 2.718254

In above code, we ask the user to enter the number of terms in the series that needs to be added. While loop iterates until the user entered number of times. Inside for loop we calculate the factorial of the selected number(number is selected by while loop and it’ll be present in variable num). Outside the for loop we add the individual series element. At the end of execution of while loop we’ll have sum of the series until user entered number of terms in the series.

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

C Program To Add Two Numbers without using Plus Operator

Lets write a C program to perform addition of 2 numbers without using plus symbol or the addition operator(+).

In this video tutorial we are using ~(tilde symbol) bitwise complement operator to perform the operation to get to the anticipated result.

Example: If user enters 2 numbers. a = 8 and b = 7. To add 8 and 7 we use result = a + b. And the result will be 15. But in this C program we are not using plus(+) operation, but still we must get the same result 15(for above input).

Logic To Add Two Numbers without using Plus Operator

If num = 7;
result = ~num; will give 1s complement of 7, that is -8.
result = -(~num); will give 8.
result = -(~num)-1; will give 7.

Now using this logic, we ask the user to enter 2 numbers. If user enters a = 8 and b = 7. We use the below logic to perform addition, without using plus symbol:

a = 8, b = 7;
add = a – ~b – 1;
add = 8 – (-8) – 1;
add = 8 + 8 – 1;
add = 8 + 7;
add = 15;

This would give proper required result.

Source Code: C Program To Add Two Numbers without using Plus Operator

#include < stdio.h >

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

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

    add = a-~b-1; 

    printf("Addition of %d and %d is %d\n", a, b, add);

    return 0;
}

Output 1:
Enter 2 numbers for addition
1
2
Addition of 1 and 2 is 3

Output 2:
Enter 2 numbers for addition
-1
2
Addition of -1 and 2 is 1

Output 3:
Enter 2 numbers for addition
-1
-2
Addition of -1 and -2 is -3

Output 4:
Enter 2 numbers for addition
-5
5
Addition of -5 and 5 is 0

Output 5:
Enter 2 numbers for addition
5
-10
Addition of 5 and -10 is -5

Video Tutorial: C Program To Add Two Numbers without using Plus Operator


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

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

Note: These kind of tricky C programming questions can be asked in your viva or company interviews or competitive exams or even in your academic exams. So be prepared.

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

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 +=

 
#include < stdio.h >

int main()
{
    int a = 20, c = 30;

    c += a ; // c = c + a;

    printf("%d\n\n", c);

    return 0;
}

Output:
50

Assignment Operator -=

 
#include < stdio.h >

int main()
{
    int a = 20, c = 30;

    c -= a ; // c = c - a;

    printf("%d\n\n", c);

    return 0;
}

Output:
10

Assignment Operator *=

 
#include < stdio.h >

int main()
{
    int a = 20, c = 30;

    c *= a ; // c = c * a;

    printf("%d\n\n", c);

    return 0;
}

Output:
600

Assignment Operator /=

 
#include < stdio.h >

int main()
{
    int a = 2, c = 20;

    c /= a ; // c = c / a;

    printf("%d\n\n", c);

    return 0;
}

Output:
10

Assignment Operator %=

 
#include < stdio.h >

int main()
{
    int a = 2, c = 20;

    c %= a ; // c = c % a;

    printf("%d\n\n", c);

    return 0;
}

Output:
0

Compound Assignment Operators in C Programming Language


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

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 https://www.youtube.com/watch?v=HHuaYKKoMR4]

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

#include < stdio.h >

int main()
{
    int a, b;
    char choice;

    printf("Enter your choice\n");
    printf("a. Addition\nb. Subtraction\nc. Multiplication\nd. Division\n");
    scanf("%c", &choice);


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


    switch(choice)
    {
        case 'a': printf("%d + %d = %d\n", a, b, (a+b));
                break;

        case 'b': printf("%d - %d = %d\n", a, b, (a-b));
                break;

        case 'c': printf("%d x %d = %d\n", a, b, (a*b));
                break;

        case 'd': if( b != 0)
                    printf("%d / %d = %d\n", a, b, (a/b));
                else
                    printf("Number can't be divided by 0\n");
                break;

        default: printf("You entered wrong choice\n");
                 break;
    }

    return 0;
}

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.

#include < stdio.h >

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

    printf("Enter your choice\n");
    printf("1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n");
    scanf("%d", &choice);

    if( choice > 4 )
    {
        printf("Select with in the range!\n");
    }
    else
    {
        printf("Enter 2 integer numbers\n");
        scanf("%d %d", &a, &b);
    }


    switch(choice)
    {
        case 1: printf("%d + %d = %d\n", a, b, (a+b));
                break;

        case 2: printf("%d - %d = %d\n", a, b, (a-b));
                break;

        case 3: printf("%d x %d = %d\n", a, b, (a*b));
                break;

        case 4: if( b != 0)
                    printf("%d / %d = %d\n", a, b, (a/b));
                else
                    printf("Number can't be divided by 0\n");
                break;

        default: printf("You entered wrong choice\n");
                 break;
    }

    return 0;
}

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.

#include

int main()
{
    int a, b;
    char choice;

    printf("Enter your choice\n");
    printf("1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n");
    scanf("%c", &choice);

    printf("Enter 2 integer numbers. Format: a + b\n");
    scanf("%d %c %d", &a, &choice, &b);



    switch(choice)
    {
        case '+': printf("%d + %d = %d\n", a, b, (a+b));
                break;

        case '-': printf("%d - %d = %d\n", a, b, (a-b));
                break;

        case '*': printf("%d x %d = %d\n", a, b, (a*b));
                break;

        case '/': if( b != 0)
                    printf("%d / %d = %d\n", a, b, (a/b));
                else
                    printf("Number can't be divided by 0\n");
                break;

        default: printf("You entered wrong choice\n");
                 break;
    }

    return 0;
}

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