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

Programming in C Lab Manual (1) - Pages-97-105

This document contains examples of C programs that use pointers. It includes a program to add two numbers using pointers that takes the addresses of two variables, dereferences the pointers to perform the addition, and prints the result. It also includes a program to calculate the factorial of a given number using pointers by passing the number and address of a variable to a function, initializing the variable to 1, and using a for loop to multiply it by integers from 1 to the given number. Screenshots show the programs executing and printing the correct results. Practice exercises are provided to write additional programs using pointers.

Uploaded by

SACHIN
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Programming in C Lab Manual (1) - Pages-97-105

This document contains examples of C programs that use pointers. It includes a program to add two numbers using pointers that takes the addresses of two variables, dereferences the pointers to perform the addition, and prints the result. It also includes a program to calculate the factorial of a given number using pointers by passing the number and address of a variable to a function, initializing the variable to 1, and using a for loop to multiply it by integers from 1 to the given number. Screenshots show the programs executing and printing the correct results. Practice exercises are provided to write additional programs using pointers.

Uploaded by

SACHIN
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

U20ESP202– Programming in C Laboratory Regulation: 2020

Ex No 8. Develop program to illustrate pointers.

a)Write a C program to add two numbers using pointers.

AIM:
To write a C program to add two numbers using pointers.

ALGORITHM:
Step1: Start
Step2: declare variables a, b
,*p,*q,sum Step3: read two integers a
and b
Step4: store the address of a and b to p and qp = &a q = &b
Step5: perform sum = *p + *q
Step6: print sum
Step7: stop

FLOW CHART:

Department of Information Technology, 1


SMVEC
U20ESP202– Programming in C Laboratory Regulation: 2020

SOURCE CODE:

#include <stdio.h>
int main()
{
int a, b, *p, *q, sum;
printf("Enter two integers to add\n");
scanf("%d%d", &a, &b);
p = &a; q = &b;
sum = *p + *q;
printf("Sum of entered numbers = %d\n",sum);
return 0;
}

Department of Information Technology, 2


SMVEC
U20ESP202– Programming in C Laboratory Regulation: 2020

SCREENSHOT:

RESULT:
Thus the program to add the two integers using pointers has been executed andverified
successfully.

PRACTICE EXERCISE:
1.Write a C program to change the value of the variable using pointe

Department of Information Technology, 3


SMVEC
U20ESP202– Programming in C Laboratory Regulation: 2020

8 b) Write a C program to find the factorial of the given number using pointers.

AIM:
To write a C program to find the factorial of the given number using pointers.

ALGORITHM:

Step 1: Start
Step 2: declare variable num and
fact. Step 3: Read the input num.
Call the function find(num,&fact)
Step 4: print factorial=fact
Step 5: In function find(int n, int *f)Initialize *f ← 1, i← 1
Step 6:for(i = 1; i<= n; i++) do
* f = *f * i;
Step 7:
Stop

Department of Information Technology, 4


SMVEC
U20ESP202– Programming in C Laboratory Regulation: 2020

FLOWCHART:

Department of Information Technology, 5


SMVEC
U20ESP202– Programming in C Laboratory Regulation: 2020

SOURCE CODE:

#include <stdio.h>
void find(int,int*);
int main()
{
int fact; int num;
printf(" Enter a number : ");
scanf("%d",&num);
find(num,&fact);
printf(" Factorial of %d is : %d \n\n",num,fact);
return 0;
}
void find(int n,int *f)
{
int i;
*f =1; for(i=1;i<=n;i++)
*f=*f*i;
}

Department of Information Technology, 6


SMVEC
U20ESP202– Programming in C Laboratory Regulation: 2020

SCREENSHOT:

RESULT:
Thus the program to find the factorial of a given number using pointers has beenexecuted
and verified successfully.

PRACTICE EXERCISE:
1.Write a C program to print the string using pointers.

Department of Information Technology, 7


SMVEC

You might also like