Q:

C Program To Find Smallest Number In An Array (3 Different Way)

belongs to collection: Array Programs in C

0

you have to make this program in the following way:

  • C Program To Find Smallest Number In An Array Using Recursion
  • C Program To Find Smallest Number In An Array Using Pointer
  • C Program To Find the 2nd Smallest Number In An Array

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

C Program To Find Smallest Number In An Array Using Recursion

Algorithm -:

  1. Program Start
  2. Variable Declaration
  3. Input Values
  4. Calling Recursive Function to find Smallest Number
  5. Display Smallest Number
  6. Program End

Program -:

//C Program To Find Smallest Number In An Array Using Recursion 

#include<stdio.h>
int smallest(int a[],int n);

void main()
{
    int i,j,n,a[20];
    printf("enter a Number :");
    scanf("%d",&n);
    printf("enter values : ");

    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }

    printf("\nSmallest Number is : %d",smallest(a,n));
}
int smallest(int a[],int n)
{
    int min;

    if(n==1)
        return a[0];

    else {
        min=smallest(a,n-1);

        if(min<a[n-1])
            return min;
        else

            return a[n-1];
    }

}

Output -:

Enter a Number 5
Enter values : 10
28
8
19
81

Smallest Number is : 8

C Program To Find Smallest Number In An Array Using Pointer

Algorithm -:

  1. Program Start
  2. Variable Declaration
  3. Input number
  4. Check the condition
  5. Display Smallest Number
  6. Program End

Program -:

//C Program To Find Smallest Number In An Array Using Pointer

#include<stdio.h>
void  main()
{
    int i,*ptr, n,a[100],small;

    printf("Enter How many number you want?:\n") ;
    scanf("%d",&n) ;

    printf("Enter %d numbers\n",n) ;
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]) ;
    }
    ptr = &a[0];
    small= *ptr;
    for(i=0;i<n;i++,*ptr++)
    {
        if(*ptr<small)
        {
            small=*ptr;
        }
    }
    printf("Smallest Number is : %d",small);
}

Output -:

Enter How many number you want?
5
Enter 5 number
21
34
3
27
32

Smallest Number is : 3

C Program To Find the 2nd Smallest Number In An Array

Program -:

//C Program To Find the 2nd Smallest Number In An Array

#include<stdio.h>
void  main()
{
    int i,n,array[100],smallest, secsmallest;

    printf("Enter How many number you want?:\n") ;
    scanf("%d",&n) ;

    printf("Enter %d Values \n",n) ;
    for(i=0;i<n;i++)
    {
        scanf("%d",&array[i]) ;
    }
   if (array[0] < array[1]) {
        smallest = array[0];
        secsmallest = array[1];
    }
    else {
      smallest = array[1];
      secsmallest = array[0];
    }
    for (i = 2; i < n; i++) {
        if (array[i] < smallest) {
        secsmallest = smallest;
        smallest = array[i];
        }
        else if (array[i] < secsmallest) {
            secsmallest = array[i];
        }
    }
    printf(" \nSecond Smallest Element is %d", secsmallest);
}

Output -:

Enter How many number you want?
5
Enter 5 number
3
14
9
23
32

Second Smallest Element is : 9

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C Program To Find Largest Number In An Array... >>