Working With Pointers
Working With Pointers
XAVIER’S COLLEGE
MAITIGHAR, KATHMANDU, NEPAL
Phone: 01-5321365, 01-5344636
Email: [email protected]
“C PROGRAMMING -
WORKING WITH POINTERS”
OUTPUT:
#include<stdio.h>
int main(){
int a,*pa,b,*pb;
pa=&a;
pb=&b;
printf("Enter two number");
scanf(" %d %d",pa,pb);
if(*pa==*pb){
printf("They are Equal");
}
else if(*pa>*pb){
printf("%d is greater",*pa);
}
else{
printf("%d is greater",*pb);
}
return 0;
}
OUTPUT:
OUTPUT:
5. Write a C program to check whether the input number is odd or
even using call by reference.
#include<stdio.h>
int oddOreven(int *x);
int main(){
int a,*pa;
pa=&a;
printf(" Enter the number \t");
scanf("%d",pa);
oddOreven(pa);
}
int oddOreven(int *x){
(*x %2==0)? printf("Even"): printf("Odd");
return 0;
}
OUTPUT:
factorial(pn);
}
int factorial(int *x){
int i,fact=1;
if (*x==0 || *x==1) {
printf("The factorial is 1");
}
if(*x>1){
for(i=1;i<=*x;i++){
fact *= i;
}
}
printf("The factorial is %d",fact);
return 0;
}
OUTPUT:
8. Write a C program to print the array of 5 numbers in ascending
order using Pointer.
#include <stdio.h>
int main(){
#include <stdio.h>
int lengthOfstr(char *x)
{
int l=0,*pl,i;
pl=&l;
while ((*x+*pl) != '\0')
{
*pl=*pl+1;
}
return *pl;
}
int main()
{
char n[50],*pn;
pn=&n;
printf("Enter a string \t ");
scanf("%s", pn);
printf("Length of the string: %d\n", lengthOfstr(pn));
return 0;
}
OUTPUT:
#include <stdio.h>
first = x;
last = x;
while (*last != '\0')
last++;
last--;
temp = *first;
*first = *last;
*last = temp;
first++;
last--;
return 0;
int main()
pa = &a;
scanf("%s", pa);
reverse(pa);
return 0;
OUTPUT:
11. Write a C program to search an element in array using pointers
#include <stdio.h>
int main() {
int size, i, key, *psize;
psize= &size;
printf("Enter the size of the array \t");
scanf("%d", psize);
int arr[size] ;
printf("Enter the elements of the array:\n");
for (i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the element to search: ");
scanf("%d", &key);
if (found) {
printf("Element %d found at index %d\n", key, i);
} else {
printf("Element %d not found in the array\n", key);
}
return 0;
}
OUTPUT:
int main() {
int x, y, z;
getValues(&x, &y, &z);
printf("Value of x: %d\n", x);
printf("Value of y: %d\n", y);
printf("Value of z: %d\n", z);
return 0;
}
OUTPUT: