0% found this document useful (0 votes)
19 views2 pages

Programming Examples On Pointers

Title

Uploaded by

tunknown359
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views2 pages

Programming Examples On Pointers

Title

Uploaded by

tunknown359
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Programming Examples on Pointers

1. C program to find sum and average of numbers from m to n using pointers


#include<stdio.h>
void main()
{
int num,*pnum=&num,range;
int m,*pm=&m;
int n,*pn=&n;
int sum=0,*psum=&sum;
float avg,*pavg=&avg;
printf("Enter the starting and ending limit\n");
scanf("%d%d",&m,&n);
range=n-m;
while(*pm<=*pn)
{
*psum=*psum+*pm;
*pm++;
}
printf("Sum of numbers=%d\n",*psum);
*pavg=*psum/range;
printf("Average=%f\n",*pavg);
}

2. C Program to add two floating point numbers using pointers


#include<stdio.h>
void main()
{
float n1,n2,sum=0.0;
float *pnum1=&n1,*pnum2=&n2,*psum=&sum;
printf("Enter the two numbers\n");
scanf("%f%f",&n1,&n2);
*psum=*pnum1+*pnum2;
printf("%f + %f is %f\n",*pnum1,*pnum2,*psum);
}

Vision: The Department of Computer Science and Engineering shall create professionally competent
and socially responsible engineers capable of working in global environment
3. C Program to perform swapping of two numbers using pass by reference (address by
reference)
#include<stdio.h>
void swap(int *p,int *q)
{
int temp;
temp=*p;
*p=*q;
*q=temp;
}
void main()
{
int a=20,b=40;
printf("Before swapping a=%d and b=%d\n",a,b);
swap(&a,&b);
printf("After swapping a=%d and b=%d\n",a,b);
}

4. C Program to read and display an array of n integers using pointers


#include<stdio.h>
void main()
{
int i,n;
int a[10],*parr=a;
printf("Enter the number of elements\n");
scanf("%d",&n);
printf("Enter the array elements\n");
for(i=0;i<n;i++)
{
scanf("%d",parr+i);
}
printf("The array elements are\n");
for(i=0;i<n;i++)
{
printf("%d\t",*(parr+i));
}
}

Vision: The Department of Computer Science and Engineering shall create professionally competent
and socially responsible engineers capable of working in global environment

You might also like