C program to find the sum of first and last digit of any number

Previous Program Next Program

Write a C program to enter any number and find the sum of first and last digit of the number using for loop. How to find sum of first and last digit of any number in C programming using loop. Logic to find sum of first and last digit of any number without using loop in C program.

Example

Input

Input number: 12345

Output

Sum of first and last digit: 6

Required knowledge

Basic C programming, For loop

Logic to find sum of first and last digit

Before I formally describe the logic to find sum of first and last digit. You must know how to find first and last digit. If you don't check out these two recommended readings -

After you are done with finding first and last digit of a given number. Let us move on to the logic section.

  1. Read a number from user. Store it in some variable say num.
  2. Find the last digit and store it in some variable. Which is lastDigit = num % 10.
  3. Find the first digit of the number by dividing num = num / 10 till num >= 10.
  4. Finally do the sum i.e. sum = firstDigit + lastDigit.

Program to find sum of first and last digit

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

#include <stdio.h>

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

    /* Read a number from user */
    printf("Enter any number to find sum of first and last digit: ");
    scanf("%d", &num);
    
    /* If it is 2 digit number then find last digit */
    if(num > 10) 
    {
        /* Adds last digit to sum */
        lastDigit = num % 10;
    }

    /* Find the first digit by dividing n by 10 until first digit is left */
    while(num >= 10)
    {
        num = num / 10;
    }
    firstDigit = num;

    /* Compute the sum */
    sum = firstDigit + lastDigit; 

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

    return 0;
}

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

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

#include <stdio.h>
#include <math.h>

int main()
{
    int num, sum, digits, firstDigit, lastDigit;

    sum = 0;

    /* Read a number from user */
    printf("Enter any number to find sum of first and last digit: ");
    scanf("%d", &num);
    
    lastDigit  = num % 10; //Get last digit
    digits    = (int) log10(num); //Total number of digits - 1
    firstDigit = (int) (num / pow(10, digits)); //Get first digit

    /* Compute the sum */
    sum = firstDigit + lastDigit; 

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

    return 0;
}
Output
Enter any number to find sum of first and last digit: 12345
Sum of first and last digit = 6

Happy coding ;)

You may also like

Previous Program Next Program
Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< pre>< code >----Your Source Code---- < / code > < / pre > (Remove spaces from pre and code tags)

1 comment: