C program to find the sum of the first and last digits, the product of the middle digits

In this article, we will learn how to create a program in C that will ask the user to enter any 4-digit number to find and print the sum of its first and last digits and then the product of its middle digit. For example, if the user enters 8427 as input, then the program will calculate the sum of the first and last digits, which will be 8+7 or 15, and then the product of the mid-digit, which will be 4*2 or 8.

After this program, we have also created another that will find and print the sum of the first and last digits of any two- or more-digit number given by the user. But let's first take a look at the program given here to learn how to calculate and print the sum of the first and last digits and the product of the mid-digits of a 4-digit number.

#include<stdio.h>
#include<conio.h>
int main()
{
    int num, first, last, second, third, prod, sum;
    int rem, count=1, temp, tempcount=0;
    printf("Enter any four digit number: ");
    scanf("%d", &num);
    temp = num;
    while(temp>0)
    {
        tempcount++;
        temp = temp/10;
    }
    if(tempcount==4)
    {
        while(num>0)
        {
            rem = num%10;
            if(count==1)
                first = rem;
            else if(count==2)
                second = rem;
            else if(count==3)
                third = rem;
            else if(count==4)
                last = rem;
            num = num/10;
            count++;
        }
        sum = first+last;
        prod = second*third;
        printf("\nSum of first and last digit (%d + %d) = %d", last, first, sum);
        printf("\nProduct of mid digits (%d * %d) = %d", third, second, prod);
    }
    else
        printf("\nKindly enter only four digit number!!");
    getch();
    return 0;
}

As the program was written in the Code::Blocks IDE, therefore, after a successful build and run, you will get the output as shown in the snapshot given here:

c program first last sum mid digit product

Now supply any 4-digit number, say 2583, as input and press the ENTER key to see the output as given here:

print sum of first last digit c program

Let's check the above program with another sample run to see what will happen if the user supplies any number that is not a 4-digit number, say 34. Here is the sample run:

print product of mid digits c program

Program Explained

Print the sum of the first and last digit in C

Let's create another program that will ask the user to enter any number (2 or more than a 2-digit number) to calculate and print the sum of its first and last digit.

#include<stdio.h>
#include<conio.h>
int main()
{
    int num, first, last, sum;
    int rem, count=1, temp, tempcount=0;
    printf("Enter any four digit number: ");
    scanf("%d", &num);
    temp = num;
    while(temp>0)
    {
        tempcount++;
        temp = temp/10;
        if(tempcount>1)
            break;
    }
    if(tempcount>1)
    {
        while(num>0)
        {
            rem = num%10;
            if(count==1)
                first = rem;
            num = num/10;
            count++;
        }
        last = rem;
        sum = first+last;
        printf("\nSum of first and last digit (%d + %d) = %d", last, first, sum);
    }
    else
        printf("\nPlease enter 2-digit or more than 2-digit number");
    getch();
    return 0;
}

Here is the final snapshot of the sample run:

c find sum of first last digit

In the above program, inside the while loop of the if block, the last value of the rem variable will be the value of the first digit, and the first value of rem will be the last digit of the given number.

C Quiz


« Previous Program Next Program »