pointers

C program to add two numbers using pointer

C program to add two numbers using pointer variable

In this tutorial, we will discuss a concept of  C program to add two numbers using pointer variable

In the C programming  language, we can use the pointer variable to add two numbers

add two numbers using pointer

In this article, we are going to learn  how to Display the sum of two numbers using the pointer variable

Program 1

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num1,num2;      //1
    int *ptr1,*ptr2;    //2
    int sum;              //3

    printf("Please enter value for num1: ");    //4
    scanf("%d",&num1);                           //5
    printf("Please enter value for num2: ");      //6
    scanf("%d",&num2);                             //7

    ptr1=&num1;                                      //8
    ptr2=&num2;                                  //9

    sum=*ptr1+*ptr2;                                 //10

    printf("Sum of the two integer values is: %d",sum);  //11
    getch();
    return 0;
}

When the above code is executed, it produces the following results

Please Enter value for num1: 46
Please enter value for num2: 64
Sum of the two integer values is: 110

 

Methods

  1. Create two variables to store two numbers as input provided by the user: num1,num2;
  2. Create two pointer variables (*ptr1,*ptr2)to store the address of the numbers: num 1 and num2
  3. Create a variable to store the sum of these numbers: sum
  4. Request the user to enter first input for store variable num1
  5. Using scanf() function store the first input value in num1
  6. Request the user to enter second input for store variable num2
  7. Using scanf() function store the second input value in num2
  8. Assign the address of variable num1 to ptr1
  9. Assign the address of variable num2 to ptr2
  10. Find the sum of two number using their addresses – find the sum of num1,num2 using their addresses and add them using the pointer variables
  11. Finally, display result on the screen- the sum of both numbers num1,num2

 

Similar post

C code to add two numbers

C program find the sum of two numbers using the function

C program find the sum of two numbers using recursion

C program to find the product of two numbers using the pointer

 

 

Suggested for you

Input-output function in C language

Data type in C language

Variable in C language

Operator n C language

Pointer in C language

 

 

Pointer in c++ programming language
C++ program to add two numbers using pointer
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

1 day ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago