All articles
c

A program to find maximum from an array using pointers

Share this article

Share on LinkedIn Share on X (formerly Twitter)

C Programming Language 101

  • Write a program to find maximum from an array using pointers

Program

#include <stdio.h>
 
void main()
{
    int a[30], n, i, *p, max = 0;
 
    printf("Enter how many numbers you want to input: ");
    scanf("%d", &n);
 
    p = a;
    printf("\nEnter numbers:\n");
    for (i = 0; i < n; i++)
    {
        scanf("%d", p + i);
    }
    for (i = 0; i < n; i++)
    {
        if (max < (*(p + i)))
        {
            max = (*(p + i));
        }
    }
 
    printf("Maximum = %d", max);
    printf("\n");
}

Result

Enter how many numbers you want to input: 3

Enter numbers:
1 2 3
Maximum = 3

Comments