0% found this document useful (0 votes)
15 views

Working With Pointers

Uploaded by

go away
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)
15 views

Working With Pointers

Uploaded by

go away
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/ 16

ST.

XAVIER’S COLLEGE
MAITIGHAR, KATHMANDU, NEPAL
Phone: 01-5321365, 01-5344636
Email: [email protected]

LAB ASSIGNMENT NUMBER:4

“C PROGRAMMING -
WORKING WITH POINTERS”

Submitted By Submitted To Signature


Name: Piyush Khadka Mr. Mrigendra Pradhan
Roll No: 431 Department of
Class:12 Computer Science,
Section: H St. Xavier’s College

Submission Date: 4th September, 2023


TABLE OF CONTENT

1. Write a C program to calculate the area of a circle using Pointer. ................. 3


2. Write a C program to find the maximum value between two numbers using
Pointer. .................................................................................................................. 3
3. Write a C program to print all the alphabets using Pointer. ........................... 4
4. Write a C program to swap two numbers using call by reference. ................ 5
5. Write a C program to check whether the input number is odd or even using
call by reference. ................................................................................................... 6
6. Write a C program to check whether the input number is prime or not using
Pointer and Function. ............................................................................................ 6
7. Write a C program to find the factorial of given number using call by
reference. ............................................................................................................... 7
8. Write a C program to print the array of 5 numbers in ascending order using
Pointer. .................................................................................................................. 9
9. Write a C program to calculate the length of string using Pointer and
Function............................................................................................................... 10
10. Write a C program to find reverse of a string using call by reference ...... 11
11. Write a C program to search an element in array using pointers .............. 13
12. Write a C program to return multiple value from function using pointers14
1. Write a C program to calculate the area of a circle using Pointer.
#include<stdio.h>
#define PI 3.1415
int main(){
int r,*pr;
float Area;
pr=&r;
printf("Enter the Radius of the circle");
scanf("%d",pr);
Area=PI* *pr * *pr;
printf("The area is %f",Area);
return 0;
}

OUTPUT:

2. Write a C program to find the maximum value between two


numbers using Pointer.

#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:

3. Write a C program to print all the alphabets using Pointer.


#include<stdio.h>
int main(){
int x,*px;
px=&x;
for(x=65;x<=90;x++){
printf("%c \t",*px);
}
return 0;
}
OUTPUT:
4. Write a C program to swap two numbers using call by reference.
#include<stdio.h>
int swapVariables( int *x, int *y);
int main(){
int a,*pa,b,*pb;
pa=&a;
pb=&b;
printf("Enter two numbers \n");
scanf("%d %d",pa,pb);
printf("Without swap= %d and %d \n",*pa,*pb);
swapVariables(pa,pb);
return 0;
}
int swapVariables( int *x, int *y){
int temp;
temp=*x;
*x=*y;
*y=temp;
printf("Swapped variables= %d and %d",*x,*y);
return 0;
}

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:

6. Write a C program to check whether the input number is prime or


not using Pointer and Function.
#include<stdio.h>
int prime(int *x);
int main(){
int a,*pa;
pa=&a;
printf("Enter the Number");
scanf("%d",pa);
prime(pa);\
}
int prime(int *x){
int s=0,i;
for(i=1;i<=*x;i++){
if(*x%i==0){
s=s+1;
}
}
(s==2)? printf("Prime"): printf("Consonant");
return 0;
}
OUTPUT:

7. Write a C program to find the factorial of given number using call


by reference.
#include<stdio.h>
int factorial(int *x);
int main(){
int n,*pn;
pn=&n;
printf("Enter the number");
scanf("%d",pn);

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(){

int i,j, temp, a[5], *pa[5];

printf("Enter the numbers:\t");


for(i = 0; i<5; i++){
pa[i] = &a[i];
scanf("%d", pa[i]);
}

for(i = 0; i<5; i++){


for(j = i+1; j<5; j++){
if(*pa[i] > *pa[j]){
temp = *pa[i];
*pa[i] = *pa[j];
*pa[j] = temp;
}
}
}

printf("Ascending Order \n");


for(i= 0; i<5; i++){
printf("%d\n", *pa[i]);
}
return 0;
}
OUTPUT:

9. Write a C program to calculate the length of string using Pointer


and Function.

#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:

10. Write a C program to find reverse of a string using call by


reference

#include <stdio.h>

int reverse(char *x) {

char *first, *last,temp;

first = x;

last = x;
while (*last != '\0')

last++;

last--;

while (first < last)

temp = *first;

*first = *last;

*last = temp;

first++;

last--;

printf("The reverse of the string is: %s\n", x);

return 0;

int main()

char a[50], *pa;

pa = &a;

printf("Enter a string: ");

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);

int *ptr = arr;


int found = 0;

for (i = 0; i < size; i++) {


if (*ptr == key) {
found = 1;
break;
}
ptr++;
}

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:

12. Write a C program to return multiple value from function using


pointers
#include <stdio.h>
int getValues(int *a, int *b, int *c) {
*a = 10;
*b = 20;
*c = 30;
}

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:

You might also like