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

In this blog post, we learn how to write a C Program to find sum of first and last digit of a number?. We will write the C program to find sum of first and last digit of a number using the Arithmetic Operators. Here we will calculate the sum of first and last digit of a number using the loop and without using the loop. Let see an example,

Input : 12345 

Output : (First digit + last digit) => 1+5

 

Algorithm to find the sum of first and last digit using the loop:

 

  • Ask the user to enter an integer number. Suppose n = 12345, where n is an integer variable.
int n = 1234;
  • To find the last digit of a number, we use modulo operator %. When modulo divided by 10 returns last digit of the input number.
lastDigit = num % 10
  • To find the first digit of a number we divide the given number by 10 until the number is greater than 10. In the end, we get the first digit.
  • In the last, calculate the sum of first and last digit.
sum = firstDigit + lastDigit;

 

C Program to find the sum of first and last digit using loop:

 

#include <stdio.h>

int main()
{
    int n, sum=0, firstDigit, lastDigit;

    printf("Enter number to find sum of first and last digit = ");
    scanf("%d", &n);

    // Find last digit of a number
    lastDigit = n % 10;


    //Find the first digit by dividing n by 10 until n greater then 10
    while(n >= 10)
    {
        n = n / 10;
    }
    firstDigit = n;


    //Calculate sum of first and last digit
    sum = firstDigit + lastDigit;

    printf("Sum of first and last digit = %d", sum);

    return 0;
}

Output:

Enter number to find sum of first and last digit = 12345

Sum of first and last digit = 6

 

 

C Program to find the sum of first and last digit without using loop:

 

#include <stdio.h>

int main()
{
    int n, sum=0, firstDigit, lastDigit,digit;

    printf("Enter number to find sum of first and last digit = ");
    scanf("%d", &n);

    //Find last digit of a number
    lastDigit = n % 10;

    //Find total number of digit - 1
    digit    = (int)log10(n);

    //Find first digit
    firstDigit = (int) (n / pow(10, digit));

    //Calculate sum of first and last digit
    sum = firstDigit + lastDigit;

    printf("Sum of first and last digit = %d", sum);

    return 0;
}

Output:

Enter number to find sum of first and last digit = 12345

Sum of first and last digit = 6

 

Note: log10() is a mathematical function present in math.h header file. It returns log base 10 value of the passed parameter to log10() function.

 

C Program to find the sum of first and last digit using a function:

 

#include <stdio.h>

int firstDigit(int n)
{
    //Remove last digit from number
    //till only one digit is left
    while (n >= 10)
        n /= 10;

    //Return the first digit
    return n;
}


int lastDigit(int n)
{
    //Return the last digit
    return (n % 10);
}


int main()
{
    int n, sum = 0, firstDigit, lastDigit,digit;

    printf("Enter number to find sum of first and last digit = ");
    scanf("%d", &n);

    //Find last digit of a number
    lastDigit = n % 10;

    //Find total number of digit - 1
    digit = (int)log10(n);

    //Find first digit
    firstDigit = (int) (n / pow(10, digit));

    //Calculate sum of first and last digit
    sum = firstDigit + lastDigit;

    printf("Sum of first and last digit = %d", sum);

    return 0;
}

Output:

Enter number to find sum of first and last digit = 12345

Sum of first and last digit = 6

 

 

2 comments

  1. does this work if I entered 012345 as input?
    In my case, it’s showing 6 but the answer is wrong as it should be 5 (5+0) I guess?

Leave a Reply

Your email address will not be published. Required fields are marked *