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

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:

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

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:

#### Program Explained

• Receive any 4-digit number as input
• Now initialize the number to any variable say temp
• Create while loop that runs until the value of temp is greater than 0
• Inside the while loop, increment any variable say tempcount and divide temp with 10
• Never forgot to initialize 0 to tempcount at start of the program
• Here, using while loop, we have counted the number digit-by-digit to check out whether the given number is a 4-digit number or not
• That is, if user enters 2583 as input, therefore, num holds this entered value
• The value of num gets initialized to temp. Now temp holds 2583
• As the value 2583 is greater than 0, therefore the program flow goes inside the while loop and increments the value of tempcount and the initial value of tempcount is 0, therefore at first run of the while loop, tempcount holds its value as 1, and temp/10 or 2583/10 or 258 is initialized to temp
• At second run of while loop, as 258 is again greater than 0, therefore again program flow goes inside the while loop, and tempcount increments its value and becomes 2, then temp/10 or 258/10 or 25 is initialized to temp
• At third run, follow the same process and continue doing until the temp holds its value as 0
• After exiting from the while loop, the variable tempcount holds the value that represents the total number of digit present in that given number by user at run-time. That is if user enters 234, then tempcount holds its value as 3 (as it is a 3-digit number) after running and exiting from the while loop
• Now check whether tempcount is equal to 4 or not
• If it is, then the given number is a 4-digit number
• And if it is a 4-digit number, then follow the next step to calculate and print the sum of its first and last digit, and product of its mid digit (second and third digit)
• Otherwise, if it is not a 4-digit number, then print a message like please enter a 4-digit number and exit from the program
• To calculate the sum of its first and last digit, and product of its mid digit, follow the steps given below
• Use while loop that runs until the value of num becomes 0
• Inside the loop, find its digit using modulous or remainder (%) operator and initialize it to a variable say rem
• Never forgot to declare and initialize a variable say count with 1 at start of the program
• Now check what is the value of count after calculating one of the digit of given number (last digit)
• If its value is 1, then initialize the value of rem to a variable say first. If its value is 2, then initialize the value of rem to a variable say second, and so on.
• Divide the number by 10 at last of the if-else block and increment the value of count variable
• For example, let's suppose that user has entered a number say 2583
• As the number is checked that it is already a 4-digit number in above steps
• Therefore inside the if block, at first run of the while loop
• The last digit was initialized to rem variable. That is num%10 or 2583%10 or 3 is initialized to rem
• As at first run, count holds its value as 1, therefore rem or 3 will be initialized to first
• Then num/10 or 2583/10 or 258 is initialized to num and count++ statement runs, that means 1++ runs, that means 2 will be initialized to count
• Do the same steps as told above to find the second, third and fourth digit of the number
• Here we have initialized the last or fourth digit as first digit, third digit as second digit, second digit as third digit, and first digit as last or fourth digit. Because we have to find the sum of first and last digit, that is first+last and product of second and third digit that is second*third (mid digit). Here the order does not matter, that is first+last gives the same result as last+first gives, in same way, third*second also gives the same result as second*third gives
• You can also change the name of the variable first with last, second with third, third with second and last with first
• As calculating the remainder using % operator gives you the last digit. It does not matter, you can arrange with yourself, it is up to you
• After exiting from the if block's while loop, initialize first+last to sum and second*third to prod
• Print the value of sum (as sum of first and last digit) and prod (as product of mid digit) as output

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

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