C program to find sum of first and last digit of a number
Learn how to write a C program to find the sum of the first and last digit of a number. This article provides a detailed explanation and sample code for this basic yet essential task in C programming.
Finding the sum of the first and last digits of a number is a common problem in programming that helps in understanding basic arithmetic operations and control structures. In this article, we will guide you through writing a C program to find the sum of the first and last digits of a given number, providing a detailed explanation and sample code.
Steps to Find the Sum of the First and Last Digit
To solve this problem, follow these steps:
- Input the Number: Read the number from the user.
- Extract the Last Digit: Find the number's last digit using the modulus operator.
- Extract the First Digit: Find the first digit by continuously dividing the number by 10 until the number is less than 10.
- Calculate the Sum: Add the first and last digits.
- Print the Result: Output the sum to the console.
Write a C program to find sum of first and last digits of a number
Here's a C program to find the sum of the first and last digit of a number:
#include <stdio.h>
int main() {
int number, firstDigit, lastDigit, sum;
// Input the number
printf("Enter an integer: ");
scanf("%d", &number);
// Extract the last digit
lastDigit = number % 10;
// Extract the first digit
firstDigit = number;
while (firstDigit >= 10) {
firstDigit /= 10;
}
// Calculate the sum
sum = firstDigit + lastDigit;
// Print the result
printf("Sum of the first and last digit: %d", sum);
return 0;
}
Output
Enter an integer: 1234
Sum of the first and last digit: 5
Explanation
- Input the Number: The program prompts the user to enter an integer. The
scanf
function reads the input number and stores it in the variablenumber
. - Extract the Last Digit:
- The last digit is found using the modulus operator (
%
), which gives the remainder whennumber
is divided by 10. - For example, if the number is 1234,
lastDigit = 1234 % 10
will result inlastDigit = 4
.
- The last digit is found using the modulus operator (
- Extract the First Digit:
- The first digit is found by repeatedly dividing the number by 10 until it is less than 10.
- For example, if the number is 1234, the sequence of operations will be
1234 / 10 = 123
,123 / 10 = 12
,12 / 10 = 1
, resulting infirstDigit = 1
.
- Calculate the Sum: The sum of the first and last digits is calculated by adding
firstDigit
andlastDigit
. - Print the Result: The program prints the sum to the console using the
printf
function.
Detailed Steps
- Step 1: Input the Number
- Use
printf
to prompt the user for input. - Use
scanf
to read the number from the user.
- Use
- Step 2: Extract the Last Digit
- Use the modulus operator (
%
) to get the last digit of the number.
- Use the modulus operator (
- Step 3: Extract the First Digit
- Use a
while
loop to divide the number by 10 until it is less than 10.
- Use a
- Step 4: Calculate the Sum
- Add the first and last digits.
- Step 5: Print the Result
- Use
printf
to display the sum of the first and last digits.
- Use