Write a C program to print the first and last digit of the numbers

Write a C program to print the first and last digit of the numbers, In this C program example, we will see how to print the first and last digit of the number.

Problem Statement:

Let’s suppose we have a number and you have to write c code to find the first digit and last digit of the number.

Sample input

Enter the numbers: 626485

Sample output

the first digit is: 6

the last digit is: 5

the first and last digit of this number is: 6 and 5

Algorithm to print  the first and last digit of the numbers

  • Declare three variables.
  • Then, take an input.
  • Take one for loop, to print the first digit and give the conditions and logic and print the first digit.
  • Then, give the logic of the last digit and print the last digit.
  • Print both in the same line with an appropriate message.

C program to print the first and last digit of  the numbers

#include<stdio.h>
void main (){
    int n,first,last,t;
    printf("enter values :");
    scanf("%d",&n);
    
    for(first=n;first>=10;first){
    first=first/10;
      }	
     printf("first digit is :%d \n",first);
        
    last=n%10;
     printf("last digit is :%d \n",last);
        
    printf("the first and last digit of this numbers is: %d \tand  %d \n",first,last);
}

Explanation of this C program

Step 1: Start.

Step 2: Create a header file and include the library in it.

Step 3: Create a void main function.

Step 4: Declare three variables(n,first,last).

Step 5: Then, create an input with the help of scanf for taking numbers from the user to print the first and last digit of the numbers.

Step 6: Create one for loop, to print the first digit give the condition (first=n;first>=10;first)and logic(first=first/10) for first digit.

step 7:  Create a print the first digit with the help of printf.

Step 8: Then, create another logic (last=n%10) for the last digit.

Step 9:  Create a print of the last digit with help of printf.

Step 10: Create printf to print both in the same line and give an appropriate message.

Step 11: End.

The output of this C program

In this, we learned how to write a C program to print the first and last digit of the numbers.