C C++ program to swap values of two variable using pointer

C C++ program to swap values of two variable using pointer

Pointer और इसके कार्य करने के तरीके को आधिक जानने के लिए देखें यह पोस्ट—Pointers in C

C Language में उपलब्ध विभिन्न प्रकार के Pointers और उनके उपयोग को जानने के लिए देखें—Types of Pointer in C

C Language के बारे में अधिक जानने के लिए देखें—Brief Introduction of C

C program to swap values of two variable using pointer

#include<stdio.h>
int main()
{
    int n1,n2,*p1,*p2,temp;
    printf("Enter number1: ");
    scanf("%d",&n1);
    printf("Enter number2: ");
    scanf("%d",&n2);
    p1=&n1;
    p2=&n2;
    temp=*p1;
    *p1=*p2;
    *p2=temp;
    printf("Number1 = %d\n",n1);
    printf("Number2 = %d\n",n2);
    return 0;
}

C++ program to swap values of two variable using pointer

#include<iostream>
using namespace std;
int main()
{
    int n1,n2,*p1,*p2,temp;
    cout<<"Enter number1: ";
    cin>>n1;
    cout<<"Enter number2: ";
    cin>>n2;
    p1=&n1;
    p2=&n2;
    temp=*p1;
    *p1=*p2;
    *p2=temp;
    cout<<"Number1 = "<<n1<<endl;
    cout<<"Number2 = "<<n2<<endl;
    return 0;
}

Output of C C++ program to swap values of two variable using pointer

C C++ program to swap values of two variable using pointer

List of important pointer example programs in C C++

  1. Declaration and assignment of pointer
  2. Pointer to pointer
  3. Sum of two numbers using pointer
  4. Average of three numbers using pointer
  5. Pointer Arithmetic
Share it to:

Published by

admin

I am a computer teacher, programmer and web developer