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

Introduction To Arrays: C Programming Language

Arrays is one of the most important topics in C programming language. In this video tutorial I’ll give you a brief introduction to Arrays.

Declaring Normal/regular Variable

Syntax:

Data_type variable_name;

Ex: int a;

Declaring Array Variable

Syntax:

Data_type variable_name[array_size];

Ex: int a[5];

Here array variable is a, it can hold upto 5 integer values.

Definition of Array

An array is a collection of data items, of same data type, accessed using a common name.

Important Notes About Arrays In C

1. All the elements inside an array MUST be of same data type.
2. If you try to enter more elements than the size allocated to the array, it’ll start throwing error.
3. If you input less number of elements than the size of array, then remaining memory blocks will be filled with zeros.
4. Array variable name(without index) holds the base address or the address of first element of the array.
5. Previous address plus the size of the data type of the array gives the address of next element in the array.

Related Read:
For Loop In C Programming Language
Basics of Pointers In C Programming Language

Types of Array

There are two types of arrays in c programming:
1. One-dimensional array.
2. Multi-dimensional array.

In today’s tutorial we’ll be learning basics of one-dimensional array.

Since one-dimensional array contains some linear type of data, its also called as list or vector.

Two-dimensional arrays are often referred to as Tables or Matrix.

Video Tutorial: Introduction To Arrays: C Programming Language


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

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

Source Code: Introduction To Arrays: C Programming Language

Array Read Write: integer type array

#include<stdio.h>

int main()
{
    int a[5], i;

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

    printf("Array elements are:\n");
    for(i = 0; i < 5; i++)
    {
        printf("%d\n", a[i]);
    }

    return 0;
}

Output:
Enter 5 integers
6
8
5
9
2
Array elements are:
6
8
5
9
2

Declaring and Initializing: integer type array

#include<stdio.h>

int main()
{
    int a[5] = { 4, 5, 1, 9, 2 }, i;

    printf("Array elements are:\n");
    for(i = 0; i < 5; i++)
    {
        printf("%d\n", a[i]);
    }

    return 0;
}

Output:
Array elements are:
4
5
1
9
2

Since a[5] is of type integer, all the array elements must be integers too. we must enclose all the elements inside curly braces and each element must be separated by a comma.

Trying to insert more values than array size

#include<stdio.h>

int main()
{
    int a[5] = { 4, 5, 1, 9, 2, 6 }, i;

    printf("Array elements are:\n");
    for(i = 0; i < 5; i++)
    {
        printf("%d\n", a[i]);
    }

    return 0;
}

Output:
warning: excess elements in array initializer.

In above source code we are trying to insert 6 integer values inside a[5], which can hold only 5 integer numbers. Hence compiler throws error and stops further compilation.

Inserting less elements/values than array size

#include<stdio.h>

int main()
{
    int a[5] = { 4, 5, 1 }, i;

    printf("Array elements are:\n");
    for(i = 0; i < 5; i++)
    {
        printf("%d\n", a[i]);
    }

    return 0;
}

Output:
Array elements are:
4
5
1
0
0

Here array size is 5, but we’re only initializing 3 integer values. So rest of it will be filled with zeros.

To avoid conflict between number of elements and array size

#include<stdio.h>

int main()
{
    int a[] = { 4, 5, 2, 6, 1, 2, 4, 5 }, i;

    printf("Array elements are:\n");

    for(i = 0; i < 8; i++)
    {
        printf("%d\n", a[i]);
    }

    return 0;
}

Output:
Array elements are:
4
5
2
6
1
2
4
5

Here we’re not specifying the size of array variable a. Compiler dynamically allocates size to it based on the number of integer numbers assigned to it.

Another method of assigning values to array variable

#include<stdio.h>


int main()
{
    int a[3], i;

    a[0] = 4;
    a[1] = 5;
    a[2] = 9;

    printf("Array elements are:\n");
    for(i = 0; i < 3; i++)
    {
        printf("%d\n", a[i]);
    }

    return 0;
}

Output:
Array elements are:
4
5
9

We could use the index and insert the value at specified position inside an array.

Note: Indexing starts from 0 in C programming language. For example, if you have an array a[5], then the elements are accessed one by one like this: a[0], a[1], a[2], a[3], a[4].

Overwriting value of elements in an array

#include<stdio.h>

int main()
{
    int a[] = { 4, 5, 2, 6, 1, 2, 4, 5 }, i;

    a[5] = 100;

    printf("Array elements are:\n");

    for(i = 0; i < 8; i++)
    {
        printf("%d\n", a[i]);
    }

    return 0;
}

Output:
Array elements are:
4
5
2
6
1
100
4
5

Here previous value of a[5], which is 2 will be replaced by 100.

Pointers and Arrays

#include<stdio.h>

int main()
{
    int a[5] = { 4, 5, 2, 6, 1 };

    printf("%d\n", a);
    printf("%d\n", &a[0]);

    return 0;
}

Output:
6356716
6356716

Here variable a will have base address or the address of first array element. In above program we’re printing the value of a and also the address where the first element of the array is stored. Both display the same address, meaning: a has base address or the address of first element in the array.

Pointers and Arrays

#include<stdio.h>

int main()
{
    int a[5] = { 4, 5, 2, 6, 1 };

    printf("%d\n", &a[0]);
    printf("%d\n", &a[1]);
    printf("%d\n", &a[3]);
    printf("%d\n", &a[4]);

    return 0;
}

Output:
6356716
6356720
6356728
6356732

If you observe above addresses, there is a difference of 4 between each address. That’s because each memory cell stores integer type data(in above program), which is allocated with 4 bytes of memory(it is machine dependent).

Characters and Arrays

#include<stdio.h>

int main()
{
    char ch[5] = { 'A', 'P', 'P', 'L', 'E' };
    int i;

    for(i = 0; i < 5; i++)
        printf("%c", ch[i]);

    return 0;
}

Output:
APPLE

String and Arrays

#include<stdio.h>

int main()
{
    char ch[5] = { 'A', 'P', 'P', 'L', 'E' };

    printf("%s", ch);

    return 0;
}

Output:
APPLE&

Array of characters is called as string. Observe the output of above program. It has ampersand symbol at the end. To remove this kind of random symbols we need to let the program know the end of a string.

#include<stdio.h>

int main()
{
    char ch[6] = { 'A', 'P', 'P', 'L', 'E', '\0' };

    printf("%s", ch);

    return 0;
}

Output:
APPLE

Look at the last element in the array. Its forward slash followed by zero. That indicates end of string.

#include<stdio.h>

int main()
{
    char ch[] = { 'I', 'B', 'M', '\0' };

    printf("%s", ch);

    return 0;
}

Output:
IBM

#include<stdio.h>

int main()
{
    char ch[] = { 'I', 'B', 'M', '\0' };

    printf("%d\n", &ch[0]);
    printf("%d\n", &ch[1]);
    printf("%d\n", &ch[2]);

    return 0;
}

Output:
6356732
6356733
6356734

Character type data has 1 byte of allocated memory. Since this array stores characters in each cell, the address of consecutive element is 1 byte apart.

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 Biggest of Three Numbers using Function

In this video tutorial you’ll learn how to find biggest of three integer numbers using function.

Related Read:
Nested if else Statement In C
Biggest of 3 Numbers: C
Biggest of 3 Numbers Using Ternary Operator: C

Video Tutorial: C Program To Find Biggest of Three Numbers using Function


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

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

Source Code: C Program To Find Biggest of Three Numbers using Function

#include<stdio.h>

int biggest(int, int, int); // function prototype

int main()
{
    int a, b, c;

    printf("Enter 3 integer numbers\n");
    scanf("%d%d%d", &a, &b, &c);

    //function call biggest(a, b, c)
    printf("Biggest of %d, %d and %d is %d\n", a, b, c, biggest(a, b, c));

    return 0;
}

// function definition
int biggest(int x, int y, int z)
{
    if(x > y && x > z)
    {
       return x;
    }
    else
    {
       if(y > z)
          return y;
       else
          return z;
    }
}

Output
Enter 3 integer numbers
50
40
60
Biggest of 50, 40 and 60 is 60

Source Code: C Program To Find Biggest of Three Numbers using Function, Using Ternary Operator

#include<stdio.h>

int biggest(int, int, int); // function prototype

int main()
{
    int a, b, c;

    printf("Enter 3 integer numbers\n");
    scanf("%d%d%d", &a, &b, &c);

    //function call biggest(a, b, c)
    printf("Biggest of %d, %d and %d is %d\n", a, b, c, biggest(a, b, c));

    return 0;
}

// function definition
int biggest(int x, int y, int z)
{
   return( (x>y && x>z)?x:(y>z)?y:z );
}

Logic To Find Biggest of 3 Numbers using Function

We ask the user to enter 3 integer numbers. We pass those 3 integer numbers to user defined function biggest. Inside function biggest we use ternary operator to determine the biggest of those 3 numbers. Function biggest returns the biggest of the 3 numbers back to the calling method/function – in above progam its main() method.

Note: Function biggest returns integer type data. And it takes 3 arguments of type integer. We’re calling function biggest from inside printf() function.

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 Biggest of Two Numbers using Function

In this video tutorial you’ll learn how to find biggest of two integer numbers using function.

Related Read:
Biggest of Two Numbers: C
Biggest of Two Numbers Using Ternary Operator: C

Video Tutorial: C Program To Find Biggest of Two Numbers using Function


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

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

Source Code: C Program To Find Biggest of Two Numbers using Function

#include<stdio.h>

int biggest(int, int); //  function prototype

int main()
{
    int a, b;

    printf("Enter 2 integer numbers\n");
    scanf("%d%d", &a, &b);

    // function call biggest(a, b)
    printf("Biggest of %d and %d is %d\n", a, b, biggest(a, b));

    return 0;
}

//function definition
int biggest(int x, int y)
{
    return( x>y?x:y );
}

Output 1
Enter 2 integer numbers
50
25
Biggest of 50 and 25 is 50

Output 2
Enter 2 integer numbers
-5
-10
Biggest of -5 and -10 is -5

Logic To Find Biggest of 2 Numbers using Function

We ask the user to enter 2 integer numbers. We pass those 2 integer numbers to user defined function biggest. Inside function biggest we use ternary operator to determine the biggest number. Function biggest returns the biggest of the 2 numbers.

x>y?x:y

Here if x is greater than y, x will be returned else y will be returned.

Note: Function biggest returns integer type data. And it takes 2 arguments of type integer. We’re calling function biggest from inside printf() function.

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 Sum of All Even Numbers Between Range, using For loop

Lets write a C program to find sum of all the even numbers between range or between 2 integers input by the user, using for loop.

Even Number: An even number is an integer that is exactly divisible by 2.

For Example: 14 % 2 == 0. When we divide 14 by 2, it gives a reminder of 0. So number 14 is an even number.

Note: In this C program we ask the user to input start and end value. We assume that the user enters bigger value for variable end and smaller value for variable start. i.e., start < end If start value is greater than value present in end, we swap the values of variable start and end.

If user enters start = 5 and end = 14. C program finds all the even numbers between 5 and 14, including 5 and 14. So the even numbers are 6, 8, 10, 12, 14. We add all these even numbers and output the sum to the console window. i.e., 6 + 8 + 10 + 12 + 14 = 50. We out put the value 50 as result.

Related Read:
Decision Control Instruction In C: IF
For Loop In C Programming Language
Even or Odd Number: C Program
C Program to Generate Even Numbers Between Two Integers

You can also watch C Program To Find Sum of All Even Numbers Between Two Integers, using While loop

Video Tutorial: C Program To Find Sum of All Even Numbers Between Range, using For loop


[youtube https://www.youtube.com/watch?v=9nKzyNnM2CY]

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

Logic To Find Sum of All Even Numbers Between Range, using For loop

Step 1: We ask the user to enter start and end value.

Step 2: We initialize count to start and iterate through the for loop until value of count is less than or equal to value of variable end. For each iteration of for loop count value increments by 1.

Step 3: For every iteration we check if value present in variable count is a even number. i.e., count % 2 == 0. If this condition is true, then we add the value present in variable count to the previous value of variable sum.

Step 4: Once the control exits for loop, we print the value present in variable sum – which has the sum of all the even numbers between the range entered by the user.

Source Code: C Program To Find Sum of All Even Numbers Between Range, using For loop

#include<stdio.h>

int main()
{
    int start, end, temp, count, sum = 0;

    printf("Enter start and end values\n");
    scanf("%d%d", &start, &end);

    if(start > end)
    {
        temp  = start;
        start = end;
        end   = temp;
    }

    printf("Even numbers between %d and %d are:\n", start, end);
    for(count = start; count <= end; count++)
    {
        if(count % 2 == 0)
        {
            printf("%d\n", count);
            sum = sum + count;
        }
    }

    printf("Sum of all the even numbers from %d to %d is %d\n", start, end, sum);

    return 0;
}

Output 1:
Enter start and end values
5
14
Even numbers between 5 and 14 are:
6
8
10
12
14
Sum of all the even numbers from 5 to 14 is 50

Output 2:
Enter start and end values
23
14
Even numbers between 14 and 23 are:
14
16
18
20
22
Sum of all the even numbers from 14 to 23 is 90

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