C Program to Find First Digit Of a Number

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

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of 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 from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published.