# Introduction of Pointer

Pointers are an essential concept in C programming that allow you to store the address of a variable or the location of the memory. They provide a powerful way to manipulate data in your program and can help you optimize your code's performance. In this article, we will explore the basics of pointers, including declaration, storage, and accessing values using pointers. Additionally, we will discover how to find pairs of amicable numbers within a given range using recursion and pointers.

## Understanding Pointers

A pointer is a variable that stores the memory address of another variable. To declare a pointer, you need to use an asterisk (*) before the variable name. For example, to declare a pointer that points to an integer variable, you would use the following syntax: int *ptr;

Once you have declared a pointer, you can store the memory address of a variable by using the ampersand (&) symbol before the variable name. For example, to store the address of an integer variable "a" in the pointer "ptr," you would use the following syntax: ptr = &a;

To access the value stored at the memory address pointed to by a pointer, you need to use the asterisk (*) symbol before the pointer name. For example, to print the value of the integer variable "a" using the pointer "ptr," you would use the following syntax: printf("%d", *ptr);

Example Code: Pointer Basics Let's look at an example to illustrate the basics of pointers in C programming:

copy

```
#include <stdio.h>
int main() {
int *ptr;
int a = 5;
ptr = &a;
printf("%d\n", ptr); // print address of a by the pointer
printf("%d\n", &a); // print address of a
printf("%d\n", *ptr); // print value of a
*ptr = 10; // changing value of a by the address
printf("%d\n", a);
return 0;
}
```

In this example, we declared a pointer "ptr" that points to an integer variable. We stored the memory address of the integer variable "a" in the pointer "ptr" using the ampersand (&) symbol. We then printed the address of "a" using the pointer and the ampersand symbol, as well as the value of "a" using the pointer and the asterisk (*) symbol. Finally, we changed the value of "a" using the pointer and printed the updated value.

## Q

Finding amicable numbers within a range can be an interesting problem to solve using programming. In this post, we will see how to solve this problem using recursion and pointers in C language.

Two numbers are called amicable numbers if the sum of proper divisors of each is equal to the other number. For example, 220 and 284 are amicable numbers since the sum of proper divisors of 220 is 284, and the sum of proper divisors of 284 is 220.

The problem requires us to find pairs of amicable numbers within a given range. To solve this problem, we will write a C program that will take a range as input and print pairs of amicable numbers within that range.

Our approach to solving this problem will use recursion and pointers to find the sum of proper divisors of a given number. We will start by writing a function that will find the sum of proper divisors of a given number using recursion and pointers.

Here is the code for the function that finds the sum of proper divisors of a given number:

copy

```
int rec(int *ls, int a, int *sum, int *temp, int *j)
{
int k = 1;
if (*j == 1 && ls[a + 1] == 0)
{
(*j)++;
return 1;
}
else if (ls[a] == 0)
return 1;
for (a; ls[a]; a++)
{
if (k == 1)
{
*temp *= ls[a];
*sum += *temp * rec(ls, a + 1, sum, temp, j);
*temp /= ls[a];
k++;
}
else if (ls[a] != ls[a - 1])
{
*temp *= ls[a];
*sum += *temp * rec(ls, a + 1, sum, temp, j);
*temp /= ls[a];
}
}
return 1;
}
```

The function takes an array of integers, ls, which contains the prime factors of the number whose sum of proper divisors we want to find. It also takes an integer, a, which is the index of the current prime factor in the array. The function uses recursion to find the sum of proper divisors of the number.

The function also takes three pointers, sum, temp, and j. The sum pointer is used to keep track of the sum of proper divisors of the number, the temp pointer is used to keep track of the product of the prime factors of the number, and the j pointer is used to keep track of whether the function has already been called recursively or not.

copy

```
int mul_add(int num)
{
int sum = 0, temp = 1, ls[30], j = 0;
for (int i = 2; num > 1 || num < -1; i++)
{
if (num % i == 0)
{
ls[j] = i;
num /= i;
i--;
j++;
}
if (i > num / 2)
i = num - 1;
}
ls[j] = 0;
j = 1;
rec(ls, 0, &sum, &temp, &j); // passing all important value and start recursion
return sum + 1;
}
int amicable(int i)
{
int sum1, sum2, j;
sum1 = mul_add(i); // passing i and get Sum of all proper divisors
// if both amicable number are same return 0
// ex:- 6 = 3 + 2 + 1 = 6
if (i == sum1)
return 0;
sum2 = mul_add(sum1);
// if match value return second number of amicable pair
if (i == sum2)
return sum1;
return 0;
}
void main()
{
int range, num, i, j = 0;
printf("Enter a range() for searching Amicable number\n");
scanf("%d %d", &i, &range); // input range
system("cls"); // clear screen using system command
// this iteration will alternately pass all the numbers to the function amicable().
for (i; i <= range; i++)
{
// this condition accept negative and positive both value
if (i > 1 || i < -1) // not accept -1, 0 and 1
{
// if the paasing value is part of amicable number return second number otherwise return 0
if ((num = amicable(i)) != 0)
{
printf("(%d, %d)\t", i, num); // print pair of amicable number
j++;
// print new line after every five pair of amicable number are printed
if (j % 5 == 0)
{
printf("\n");
}
}
}
}
if (j == 0)
printf("\n Any amicable number not found! in this range\n");
printf("\nEnter any key for exit");
getch();
}
```

The program first asks the user to input a range of numbers to search for amicable pairs. It then iterates through the range and passes each number to the `amicable` function to check if it is part of an amicable pair. The `amicable` function uses the `mul_add` function to calculate the sum of the proper divisors of the input number. It then uses `mul_add` again to calculate the sum of the proper divisors of the first sum. If the second sum is equal to the original input number, then the pair is amicable and the function returns the second number in the pair. Otherwise, it returns 0. If the `amicable` function returns a non-zero value, indicating that the input number is part of an amicable pair, the program prints the pair of numbers in the format "(a, b)". It also prints a new line after every five pairs of numbers are printed. If no amicable pairs are found in the range, the program prints a message saying so. The output of the program will depend on the input range and the number of amicable pairs found in that range.

This problem can also be solved in a simple way.