codescracker
c

C Program to Find Sum of First & Last, Product of Mid Digits



« C Tutorial C Examples »

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

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

// Sum of First+Last & Product of Mid Digits
// ----codescracker.com----

#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 under Code::Blocks IDE, therefore after 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 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, that what will happen if user supply 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 Sum of First and Last Digit

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

// Print Sum of First and Last Digit
// ----codescracker.com----

#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 sample run:

c find sum of first last digit

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


« C Tutorial C Examples »