Coding Practice

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

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

Enter number: 19224

First Digit: 1, Last Digit: 4
Source Code
#include<stdio.h>

int main()
{
    int n, num, firstDigit, lastDigit;

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

    firstDigit = num;
    lastDigit = num % 10;

    for(n = 1; firstDigit  >= 10; n++)
    {
        firstDigit = firstDigit / 10;
    }
    printf("\nFirst Digit: %d, Last Digit: %d\n", firstDigit, lastDigit);

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

Enter number: 2021

First Digit: 2, Last Digit: 1

No comments:

Post a Comment

Change Theme
X