C program to find sum of first and last digit of a number

Category: C Program

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:

  1. Input the Number: Read the number from the user.
  2. Extract the Last Digit: Find the number's last digit using the modulus operator.
  3. Extract the First Digit: Find the first digit by continuously dividing the number by 10 until the number is less than 10.
  4. Calculate the Sum: Add the first and last digits.
  5. 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

  1. Input the Number: The program prompts the user to enter an integer. The scanf function reads the input number and stores it in the variable number.
  2. Extract the Last Digit:
    • The last digit is found using the modulus operator (%), which gives the remainder when number is divided by 10.
    • For example, if the number is 1234, lastDigit = 1234 % 10 will result in lastDigit = 4.
  3. 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 in firstDigit = 1.
  4. Calculate the Sum: The sum of the first and last digits is calculated by adding firstDigit and lastDigit.
  5. 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.
  • Step 2: Extract the Last Digit
    • Use the modulus operator (%) to get the last digit of the number.
  • Step 3: Extract the First Digit
    • Use a while loop to divide the number by 10 until it is less than 10.
  • 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.

Recommended Posts