In this tutorial you will learn about the C program to print First Digit of Number and its application with practical example.
C Program to print First Digit of a Number
In this tutorial, we will learn to create a C program that will print the First Digit of a Number in C programming
Prerequisites
Before starting with this tutorial, we assume that you are the best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- Arithmetic operations in C programming.
- Operators in C programming.
What are the digits in the number?
The number of integers in a number is called its digits. A number is said to be a single-digit number when it is between 0 and 9. Hence, the number is said to be a 4-digit number when it is between the range of 1000 and 9999. in c programming, we can print the first and last digit from the number with the help of a small program.
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 |
1. Declaring the variables for the program. 2. Taking the input number from the user. 3. Finding the digits in that given number. 4. Passing those variables to the program. 5. Printing the First Digit of a Number. 6. End the program. |
Printing the First Digit of a Number
In this program first, we will take the input number from the user. Then we will separate the digits from that number. And at last, we will print the first last digit of the number.
Let us take the example program from the below code for printing the First Digit of a Number.
Program Code:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
/* C program to print First Digit of Number */ #include <stdio.h> #include <math.h> int main() { //Declaring the required variables for the program. int no, fdigit, Count; //fdigit = it will hold the integer value for the first digit of number. //no = it will hold the input number from the user. //Count = it will hold the integer value. //Taking the input number from the user printf("\n Please Enter any Number that you wish : "); //Scanning the input number from the user. scanf("%d", & no); //Finding the first digit of number. Count = log10(no); fdigit = no / pow(10, Count); printf(" \n Total Number of Digit in a Given Number %d = %d", no, Count); //Printing the First Digit of a Given Number printf(" \n The First Digit of a Given Number %d = %d", no, fdigit); return 0; } |
Output:-
In the above program, we have first initialized the required variable.
- no = it will hold the integer value.
- fdigit = it will hold the integer value.
- Count = it will hold the integer value.
Input Number from the user for the program.
Finding the First Digit of a Number.
Printing output first and the last digit.