C C++ program to find sum of two numbers using pointer

Coding in C

#include<stdio.h>
int main()
{
    int n1,n2,*p1,*p2,sum;
    printf("Enter two numbers: ");
    scanf("%d%d",&n1,&n2);
    p1=&n1;
    p2=&n2;
    sum=*p1+*p2;
    printf("Sum = %d",sum);
    return 0;
}

Coding in C++

#include<iostream>
using namespace std;
int main()
{
    int n1,n2,*p1,*p2,sum;
    cout<<"Enter two numbers: ";
    cin>>n1>>n2;
    p1=&n1;
    p2=&n2;
    sum=*p1+*p2;
    cout<<"Sum = "<<sum;
    return 0;
}

Output

C C++ program to find sum of two numbers using pointer

Published by

admin

I am a computer teacher, programmer and web developer