Let Us C (Chapter 8 : Arrays) : Program-7

(g) Find the smallest number in an array using pointers.

let us c chapter 8 arrays problem 7

// Let Us C (Chapter 8 : Arrays) : Program-7
/* Find the smallest number in an array using pointers */
#include<stdio.h>
#define ARRAY_SIZE 10
long int arr[ARRAY_SIZE],smallest,*ptr;
unsigned int i,N;
void main()
{
    clrscr();
    printf("Program to find smallest number in an array using pointers");
    printf("Enter no. of elements: ");
    scanf("%d",&N);
    for(i=0;i<N;i++)
    {
        printf("Element %d: ",i);
        scanf("%ld",&arr[i]);
    }
    ptr = arr;    // passing array address to pointer
    smallest = arr[0];
    for(i=0;i<N;i++)
    {
        if(smallest > *(ptr+i))    // comparing using pointer
        {
            smallest = *(ptr+i);
        }
    }
    printf("Smallest number in array is %ld",smallest);
}
OUTPUT
let us c chapter 8 arrays problem 7 output


Comments

Popular posts from this blog

Chapter 5 : Functions & Pointers (Let us C)

Let Us C (Chapter 8 : Arrays) : Program-9

Chapter 3: The Loop Control Structure (Let Us C)