In this tutorial you will learn about the C program to find Sum of First and Last Digit of a Number and its application with practical example.
C Program to find Sum of First and Last Digit of a Number
In this tutorial, we will learn to create a C program that will find Sum of the First and Last digits of a Number in C programming.
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- Arithmetic operations in C programming.
- Operators in C programming.
What is the sum of digits?
In summation, we add the data of two variables or the data in the variable. In this program, we will add the first and last digits by separating each digit and adding them and at last will print the resultant data of that variable.
1. Declaring the variables for the program.
2. Taking the input number from the user.
3. Finding the digits in that given number.
4. Passing those variables to the program.
5. Adding the First and Last Digit of a Number.
6. Printing the resultant number to the user.
7. End the program.
Adding the First and Last Digit of a Number
In this program first, we will take the input number from the user. Then we will separate the digits from that number. Now we will add those first and last digits. And at last, we will print that output number
Let us take the example program from the below code for Add First and Last Digit of a Number.
* C program to find sum of first and last digit of a number using loop
//declaring the required variable for the program.
int num, sum=0, fdigit, ldigit;
/* Taking the Input a number from user */
printf("Enter any number to find sum of first and last digit: ");
/* Find last digit to sum */
ldigit = num % 10;
/* Copy num to first digit */
fdigit = num;
/* Find the first digit by dividing num by 10 until first digit is left */
while(num >= 10)
num = num / 10;
fdigit = num;
/* Find sum of first and last digit*/
sum = fdigit + ldigit;
//printing the output number.
printf("Sum of first and last digit = %d", sum);
In the above program, we have first initialized the required variable.
- num = it will hold the integer value.
- fdigit = it will hold the integer value.
- ldigit = it will hold the integer value.
- sum = it will hold the integer value.
Input Number from the user for the program.
Sum of the First and Last Digit of a Number.
Printing output addition of the number.