C Program to Find First Digit Of a Number

C program to find first digit of a number; Through this tutorial, we will learn how to find first digit of a number in c program using log() & pow() , while loop and functions.

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 output 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 output 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 output of the above c program; as follows:

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

Recommended C Programs

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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