C Program to Find Largest Number Using Dynamic Memory Allocation

Largest Number Using Dynamic Memory Allocation


Write a C Program to Find Largest Number Using Dynamic Memory Allocation. Here’s simple Program to Find Largest Number using pointers in C Programming Language.


What are Pointers?


A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address.

The general form of a pointer variable declaration is

  • type *var-name;

Here, type is the pointer’s base type; it must be a valid C data type and var-name is the name of the pointer variable.

The asterisk * used to declare a pointer is the same asterisk used for multiplication. However, in this statement the asterisk is being used to designate a variable as a pointer.

The unary or monadic operator & gives the “address of a variable'”.

The indirection or dereference operator * gives the “contents of an object pointed to by a pointer”.


Below is the source code for C Program to Find Largest Number Using Dynamic Memory Allocation which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/*  C Program to Find Largest Number Using Dynamic Memory Allocation  */

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i, num;
    float *data;

    printf("How many elements u want to enter :: ");
    scanf("%d", &num);

    // Allocates the memory for 'num' elements.
    data = (float*) calloc(num, sizeof(float));

    if(data == NULL)
    {
        printf("\nError!!! memory not allocated.");
        exit(0);
    }

    // Stores the number entered by the user.
    for(i = 0; i < num; ++i)
    {
       printf("\nEnter Number %d :: ", i + 1);
       scanf("%f", data + i);
    }

    // Loop to store largest number at address data
    for(i = 1; i < num; ++i)
    {
       // Change < to > if you want to find the smallest number
       if(*data < *(data + i))
           *data = *(data + i);
    }

    printf("\nLargest element = %.2f\n", *data);

    return 0;
}

Output : :


/*  C Program to Find Largest Number Using Dynamic Memory Allocation  */

How many elements u want to enter :: 6

Enter Number 1 :: 3

Enter Number 2 :: 1

Enter Number 3 :: 5

Enter Number 4 :: 7

Enter Number 5 :: 3

Enter Number 6 :: 8

Largest element = 8.00

Process returned 0

Above is the source code for C Program to Find Largest Number Using Dynamic Memory Allocation which is successfully compiled and run on Windows System.The Output of the program is shown above .

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

Leave a Reply