- C Programming Examples
- C Programming Examples
- C Print Hello World
- C Get Input from User
- C Print Integer
- C Add Two Numbers
- C Check Even or Odd
- C Check Prime or Not
- C Check Alphabet or Not
- C Check Vowel or Not
- C Check Leap Year or Not
- C Check Reverse equal Original
- C Add Subtract Multiply Divide
- C Make Calculator
- C Add Digits of Number
- C Calculate Average, Percentage
- C Calculate Arithmetic Mean
- C Calculate Student Grade
- C Print Table of Number
- C Print Prime Numbers
- C Add n Numbers
- C Interchange Numbers
- C Reverse a Number
- C Swap Two Numbers
- C Count Positive Negative Zero
- C Find Largest of Two Numbers
- C Find Largest of Three Numbers
- C Find Factorial of Number
- C Find LCM & HCF
- C Find LCM of n Numbers
- C Find HCF of n Numbers
- C Area & Perimeter of Square
- C Area & Perimeter of Rectangle
- C Area & Circumference of Circle
- C Convert Fahrenheit to Celsius
- C Convert Celsius to Fahrenheit
- C Print ASCII Value
- C Print Fibonacci Series
- C Check Palindrome or Not
- C Check Armstrong or Not
- C Generate Armstrong Numbers
- C Find nCr and nPr
- C Convert Decimal to Binary
- C Convert Decimal to Octal
- C Convert Decimal to Hexadecimal
- C Convert Binary to Decimal
- C Convert Binary to Octal
- C Convert Binary to Hexadecimal
- C Convert Octal to Decimal
- C Convert Octal to Binary
- C Convert Octal to Hexadecimal
- C Convert Hexadecimal to Decimal
- C Convert Hexadecimal to Binary
- C Convert Hexadecimal to Octal
- C Pattern Printing Programs
- C Print Diamond Pattern
- C Print Floyd's Triangle
- C Print Pascal's Triangle
- C Print Smiling face
- C 1D Array Programs
- C Linear Search
- C Binary Search
- C Add Two Numbers using Pointer
- C Find Largest Element in Array
- C Find Smallest Element in Array
- C Reverse an Array
- C Insert Element in Array
- C Delete Element from Array
- C Merge Two Arrays
- C Bubble Sort
- C Selection Sort
- C Insertion Sort
- C 2D Array Programs
- C Add Two Matrices
- C Subtract Two Matrices
- C Transpose a Matrix
- C Multiply Two Matrices
- C 3D Array Programs
- C Print String
- C Find Length of String
- C Compare Two String
- C Copy a String
- C Concatenate String
- C Reverse a String
- C Delete Vowels from String
- C Delete Word from String
- C Find Frequency of Character
- C Count Word in String
- C Remove Spaces from String
- C Sort a String
- C Uppercase to Lowercase
- C Lowercase to Uppercase
- C Swap Two Strings
- C Check Anagram or Not
- C Generate Random Numbers
- C Read a File
- C Write Content to File
- C Read & Display File
- C Copy a File
- C Merge Two Files
- C List Files in Directory
- C Delete a File
- C Encrypt & Decrypt a File
- C Print Date
- C Get IP Address
- C Shutdown Computer
- C More Programs
- C Check Palindrome String
- C Inches to Centimetres
- C Kilogram to Gram
- C Count Even Odd
- C Count Vowels Consonants
- C Find Profit Loss
- C Sum All Matrix Elements
- C First & Last Digit Sum
- C Largest Element in Matrix
- C Pass Array to Function
- C Address of Variable
- C Array Element at Even
- C Array Element at Odd
- C Print Even Array Elements
- C Print Odd Array Elements
- C Reverse File
- C Print Message with Time
- C Print Number in Words
- C Print Successive Character
- C Sum of their Square
- C Replace Vowel in String
- C Sort Words in Ascending
- C Sort Words in Descending
- C Sort String in Alphabetical
- C Programming Tutorial
- C Tutorial
- C Programming Library
- C Standard Library
- C Programming Test
- C Programming Test
- Give Online Test
- All Test List

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:

- 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

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