C Program to Find First Digit Of a Number

In this tutorial, i am going to show you how to find first digit of a number in c program using log() & pow() , while loop and functions.

All C Program to Find First Digit Of a Number

  • C Program to Find First Digit Of a Number using Log10() and pow()
  • C Program to Find First Digit Of a Number using While Loop
  • C Program to Find First Digit Of a Number using Function

C Program to Find First Digit Of a Number using Log10() and pow()

/* C Program to Find First Digit Of a Number */
 
#include <stdio.h>
#include <math.h>
 
int main()
{
  	int Number, FirstDigit, Count;
 
  	printf("\n Please Enter any Number that you wish  : ");
  	scanf("%d", & Number);
  	
  	Count = log10(Number);
  	
  	FirstDigit = Number / pow(10, Count);
 
	printf(" \n The First Digit of a Given Number %d =  %d", Number, FirstDigit);
 
  	return 0;
}

The result of the above c program; as follows:

Please Enter any Number that you wish  : 123
The First Digit of a Given Number 123 =  1

C Program to Find First Digit Of a Number using While Loop

/* C Program to Find First Digit Of a Number */
 
#include <stdio.h>
 
int main()
{
  	int Number, FirstDigit;
 
  	printf("\n Please Enter any Number that you wish  : ");
  	scanf("%d", & Number);
  	
  	FirstDigit = Number;
  	
  	while(FirstDigit >= 10)
  	{
  		FirstDigit = FirstDigit / 10;
	}
  
	printf(" \n The First Digit of a Given Number %d =  %d", Number, FirstDigit);
 
  	return 0;
}

The result of the above c program; as follows:

Please Enter any Number that you wish  : 2365
The First Digit of a Given Number 2365 =  2

C Program to Find First Digit Of a Number using Function

#include <stdio.h>
#include <math.h>
int First_Digit(int num);
 
int main()
{
  	int Number, FirstDigit;
 
  	printf("\n Please Enter any Number that you wish  : ");
  	scanf("%d", & Number);
  	
  	FirstDigit = First_Digit(Number);
  	
	printf(" \n The First Digit of a Given Number %d =  %d", Number, FirstDigit);
 
  	return 0;
}
int First_Digit(int num)
{
	while(num >= 10)
	{
		num = num / 10;
	}
	return num;
}

The result of the above c program; as follows:

Please Enter any Number that you wish  : 7895
The First Digit of a Given Number 7895 =  7

More C Programming Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *