1.
WAP to perform arithmetic calculations (sum, difference,
multiplication and division) of two numbers using pointers.
#include<stdio.h>
void main(){
int a,b,s,d,m,*x,*y;
float div;
printf("Enter first numbers: ");
scanf("%d",&a);
printf("\nEnter second numbers: ");
scanf("%d",&b);
x=&a;
y=&b;
s=*x+*y;
printf("\nSum of two number is:%d",s);
d=*x-*y;
printf("\nDifference of two number is:%d",d);
m=*x**y;
printf("\nMultiplication of two number is:%d",m);
div = (float)(*x) / (*y);
printf("\nDivision of two numbers is: %f", div);
return 0;
}
Output:
2. WAP to find odd or even number using pointers.
#include<stdio.h>
void main(){
int a,*x;
printf("Enter a number: ");
scanf("%d",&a);
x=&a;
if(a%2==0){
printf("\nThe given number is even");
}
else{
printf("\nThe given number is odd");
}
return 0;
}
Output:
3. Write C program to find sum and average of n natural numbers
using pointer.
#include <stdio.h>
int main(){
int n,i,num,sum=0;
int *p;
float avg;
printf("Enter how many natural numbers: ");
scanf("%d", &n);
p=#
for(i=1;i<=n;i++){
printf("Enter number %d: ", i);
scanf("%d", p);
sum=sum+*p;
}
avg=(float)sum / n;
printf("\nSum = %d", sum);
printf("\nAverage = %.f", avg);
return 0;
}
Output:
4. WAP to Use array as a pointer to input 5 elements and print them.
#include <stdio.h>
int main(){
int a[5], i;
int *p;
p=a;
printf("Enter five elements: ");
for(i=0;i<5;i++){
scanf("%d",(p+i));
}
printf("\nElements are:\n");
for (i=0;i<5;i++){
printf("%d\n",*(p+i));
}
return 0;
}
Output:
5. Write C program to swap any two integers using call by value and
call by reference.
Call by value:
#include<stdio.h>
void swap(int a, int b);
int main(){
int a=10;
int b=20;
swap(a,b);
printf("After swapping a=%d b=%d",a,b);
}
void swap(int a, int b){
int t;
t = a;
a = b;
b = t;
}
Output:
Call by reference:
#include<stdio.h>
void swap(int *a, int *b);
void main(){
int a=10;
int b=20;
swap(&a,&b);
printf("After swapping a=%d b=%d",a,b);
}
void swap(int *a, int *b){
int t;
t=*a;
*a=*b;
*b=t;
}
Output:
Conclusion:
We conclude that pointers in C allow us to access and manipulate data directly through memory
addresses. By using pointers, we can perform arithmetic operations, check odd or even
numbers, find the sum and average of natural numbers, and work efficiently with arrays. The
programs also show how pointers help in swapping values using call by reference, which is not
possible with call by value. Overall, pointers improve program efficiency, enable better
memory management, and are very useful for writing effective and flexible C programs.