Programming in C Lab Manual (1) - Pages-97-105
Programming in C Lab Manual (1) - Pages-97-105
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:
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;
}
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
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
FLOWCHART:
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;
}
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.