Coding Practice

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

Find sum of first and last digit of a number
Sample Output
Find sum of first and last digit of a number

Number: 12349

First Digit: 1, Last Digit: 9
Sum = 10
Source Code
#include<stdio.h>

int main()
{
    int i, number, firstDigit, lastDigit, sum;

    printf("Find sum of first and last digit of a number\n\nNumber: ");
    scanf("%d", &number);

    lastDigit = number % 10;

    while(number >= 10)
    {
        number = number / 10;
    }
    firstDigit = number;
    sum = firstDigit + lastDigit;

    printf("\nFirst Digit: %d, Last Digit: %d\nSum = %d\n\n", firstDigit, lastDigit, sum);

    return 0;
}
Sample Output
Find sum of first and last digit of a number

Number: 7654

First Digit: 7, Last Digit: 4
Sum = 11

No comments:

Post a Comment

Change Theme
X