C Program To Find Smallest Element In An Array

Lets write a C program to find smallest element in an array, without sorting the elements. And also print the position at which the smallest number is present in the array.

Related Read:
C Program To Find Biggest Element of An Array

Example: Expected Output

Enter 5 integer numbers
5
2
6
4
3

Biggest of 5 numbers is 2, at position 2.
array with size 5

Video Tutorial: C Program To Find Smallest Element In An Array


[youtube https://www.youtube.com/watch?v=0lnvlxKPD3A]

YouTube Link: https://www.youtube.com/watch?v=0lnvlxKPD3A [Watch the Video In Full Screen.]

Source Code: C Program To Find Smallest Element of An Array

Method 1

#include<stdio.h>

#define N 5

int main()
{
    int a[N], i, small, pos;

    printf("Enter %d integer numbers\n", N);
    for(i = 0; i < N; i++)
    {
        scanf("%d", &a[i]);

        if(i == 0 || small > a[i])
        {
            small = a[i];
            pos   = i + 1;
        }
    }

    printf("Smallest Number: %d, at position %d.\n", small, pos);

    return 0;
}

Output 1:
Enter 5 integer numbers
9
8
6
2
5
Smallest Number: 2, at position 4.

Output 2:
Enter 5 integer numbers
5
6
1
3
2
Smallest Number: 1, at position 3.

Logic To Find Smallest Element In An Array

We ask the user to input N integer numbers. N being Macro, used to assign array size. In above source code N is assigned a value of 5. So user enters 5 integer numbers. While the user inputs numbers we check if it’s the first number input by the user. If its the first number/element, then we assign that first number/element entered by the user to variable small and assign 1 to variable pos, indicating that its the first element in the array.

Next, for each consecutive iteration of the for loop we check if the new value entered by the user is smaller than the value present in variable small. If it’s true, then we assign the new value entered by the user to variable small and also update the value of pos accordingly.

Once i < N condition is false, control exits for loop and we print the value present inside variable small and pos which will have smallest of N numbers or the smallest element of the array and position of that element in the array.

Explanation With Example

If int a[5] = {5, 2, 6, 4, 3};

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

        if(i == 0 || small > a[i])
        {
            small = a[i];
            pos   = i + 1;
        }
    }
indexa[index]smallpos = index + 1
0a[0] = 551
1a[1] = 222
2a[2] = 6
3a[3] = 4
4a[4] = 3
5

a[5] = {5, 2, 6, 4, 3}
small = 2;
pos = 2;

Source Code: C Program To Find Smallest Element of An Array

Method 2

 #include<stdio.h>

#define N 5

int main()
{
    int a[N], i, small, pos;

    printf("Enter %d integer numbers\n", N);

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

    small = a[0];
    pos   = 1;

    for(i = 1; i < N ; i++)
    {
        if(small > a[i])
        {
            small = a[i];
            pos   = i + 1;
        }
    }

    printf("Smallest Number: %d, at position %d.\n", small, pos);

    return 0;
}

Output 1:
Enter 5 integer numbers
9
8
6
2
5
Smallest Number: 2, at position 4.

Output 2:
Enter 5 integer numbers
5
2
6
4
3
Smallest Number: 2, at position 2.

Method 2: LOGIC

Here we accept N integer numbers from the user. Next we assign the first element of the array to variable small and 1 to pos. Now we use another for loop to loop through the array.

Inside for loop
Inside for loop we check if the value present inside variable small is greater or bigger than the value present at a[i]. If it’s true, then we assign the value of a[i] to small and value of (i+1) to variable pos.

At the end of for loop, variable small and pos will have smallest element of the array and the position at which this number is present in the array.

Note: Since we assign the value of first element of the array(which is present at location a[0]) to variable small, while comparing with other elements of the array, we start comparing from a[1] till a[N-1]. That’s the reason we’ve assigned initial value of i to 1 in the second for loop.

Since a[0] is assigned to variable small, there is no point in comparing big with a[0]. So we start comparison from a[1].

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Find Range of Set of Numbers

Write a C program to find the range of a set of numbers entered through the keyboard. Range is the difference between the smallest and biggest number in the list.

Example: If biggest number in the list is 5 and smallest number in the list is 1. The difference between them is the range. i.e., 5 – 1 = 4. So range = 4.

Related Read:
while loop in C programming
if else statement in C
Relational Operators In C
C Program To Find Absolute Value of a Number

Expected Output for the Input

User Input:
Enter the limit
5
Enter 5 numbers
1
2
3
4
5

Output:
Small Number = 1
Big Number = 5
Range is 4

Logic To Find Range of Set of Numbers

First we ask the user to enter the length or the size of the list, and store it inside the variable limit. If the user enters the list size as 5, then we ask the user to enter 5 numbers.

Next we ask the user to enter the first number. We assign the first number entered by the user to variables small and big. Since user already entered 1 number into the list, we decrement the value of variable limit by 1.

Next we take remaining inputs inside the while loop. For each iteration of the while loop we decrement the value of variable limit by 1, until the value of limit is 0. Once value of limit is zero, control exits while loop.

For each input(inside the while loop), we check if the value entered is bigger than the value present in variable big. If its true, we assign the bigger number to variable big. We also check if the value entered is smaller than the value present in variable small. If its true, we assign the smaller number to variable small.

Once the control exits the while loop, variable big will have the biggest number in the list and variable small will have the smallest number in the list.

Finally, we use below formula to calculate range of the list:
range = big – small;

Video Tutorial: C Program To Find Range of Set of Numbers


[youtube https://www.youtube.com/watch?v=-yN6KsA8d-k]

YouTube Link: https://www.youtube.com/watch?v=-yN6KsA8d-k [Watch the Video In Full Screen.]

Source Code: C Program To Find Range of Set of Numbers

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

int main()
{
    int small, big, range, num, limit;

    printf("Enter the limit\n");
    scanf("%d", &limit);

    printf("Enter %d numbers\n", limit);
    scanf("%d", &num);

    small = big = num;

    limit = limit - 1;

    while(limit)
    {
        scanf("%d", &num);

        if(num > big)
        {
            big = num;
        }

        if(num < small)
        {
            small = num;
        }

        limit--;
    }

    range = big - small;

    printf("Small Number = %d\nBig Number = %d\n", small, big);
    printf("Range is %d\n", abs(range));

    return 0;
}

Output 1:
Enter the limit
5
Enter 5 numbers
0
-1
2
3
4
Small Number = -1
Big Number = 4
Range is 5

Output 2:
Enter the limit
6
Enter 6 numbers
10
9
8
7
6
5
Small Number = 5
Big Number = 10
Range is 5

Note: abs() returns the absolute value of a number. Absolute value is always positive.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Check For Alphabet, Number and Special Symbol

Any character is entered through the keyboard, write a C program to determine whether the character entered is a capital letter, a small case letter, a digit or a special symbol.

The following table shows the range of ASCII values for various characters:
Character A – Z : ASCII Value 65 – 90
Character a – z : ASCII Value 97 – 122
Character 0 – 9 : ASCII Value 48 – 57
Special Symbol : ASCII Value 0 – 47, 58 – 64, 91 – 96, 123 – 127

ascii codes

Related Read:
else if statement in C
Relational Operators In C
C Program To Print All ASCII Characters and Code

Expected Output for the Input

User Input:
Enter a Character
$

Output:
$ is a Special Character

Video Tutorial: C Program To Check For Alphabet, Number or Special Symbol


[youtube https://www.youtube.com/watch?v=NBcG-r0P9P8]

YouTube Link: https://www.youtube.com/watch?v=NBcG-r0P9P8 [Watch the Video In Full Screen.]

Source Code: C Program To Check For Alphabet, Number and Special Symbol

#include<stdio.h>

int main()
{
    char ch;

    printf("Enter a Character\n");
    scanf("%c", &ch);

    if(ch >= 65 && ch <= 90)
    {
        printf("%c is an Uppercase Alphabet\n", ch);
    }
    else if(ch >= 97 && ch <= 122)
    {
        printf("%c is an lowercase Alphabet\n", ch);
    }
    else if(ch >= 48 && ch <= 57)
    {
        printf("%c is a Number\n", ch);
    }
    else if( (ch >= 0  && ch <= 47) ||
             (ch >= 58 && ch <= 64) ||
             (ch >= 91 && ch <= 96) ||
             (ch >= 123 && ch <= 127))
    {
        printf("%c is a Special Character\n", ch);
    }

    return 0;
}

Output 1:
Enter a Character
A
A is an Uppercase Alphabet

Output 2:
Enter a Character
i
i is an lowercase Alphabet

Output 3:
Enter a Character
8
8 is a Number

Output 4:
Enter a Character
$
$ is a Special Character

Logic To Check For Alphabet, Number and Special Symbol

We use &&(AND) operator check check for range. i.e., for number, we check from the range 48 to 57. To check if the user entered character lies in this range we use (ch >= 48 && ch <= 57). To check for multiple ranges we use ||(OR) operator. For example, for special symbol:


(ch >= 0 && ch <= 47) ||
(ch >= 58 && ch <= 64) ||
(ch >= 91 && ch <= 96) ||
(ch >= 123 && ch <= 127)

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert