C C++ program to find average of three numbers using pointer

Coding in C

#include<stdio.h>
int main()
{
    int n1,n2,n3,*p1,*p2,*p3;
    float avg;
    printf("Enter three numbers: ");
    scanf("%d%d%d",&n1,&n2,&n3);
    p1=&n1;
    p2=&n2;
    p3=&n3;
    avg=(*p1+*p2+*p3)/3.0;
    printf("Average = %f",avg);
    return 0;
}

Coding in C++

#include<iostream>
using namespace std;
int main()
{
    int n1,n2,n3,*p1,*p2,*p3;
    float avg;
    cout<<"Enter three numbers: ";
    cin>>n1>>n2>>n3;
    p1=&n1;
    p2=&n2;
    p3=&n3;
    avg=(*p1+*p2+*p3)/3.0;
    cout<<"Average = "<<avg;
    return 0;
}

Output

C C++ program to find average of three numbers using pointer

Published by

admin

I am a computer teacher, programmer and web developer