how to find first digit of a number in c

                    

 

Code: 

#include <stdio.h>
#include<conio.h>
int main()
{
int c,n;
printf("Enter a Number:");
scanf("%d",&n);
while(n!=0)
{
    n=n/10;
    c=c+1;
}
printf("No.of Digit=%d",c);
getch();
    return 0;
}

 

Sample Input and Output:

Enter a Number:94994                                                                                                                 
No.of Digit=5 

Explanation of above program

Hello everyone ! In this article, we are going to learn how to write a c program to find total number of digit in a number,So Let's get started.

In the first two line of our code we have included our two most important header file stdio.h,conio.h, stdio is the acronym of standard Input Output  and it help us to use most important input/output inbuilt function like printf(),scanf().On the other hand conio.h header file stands for console input and output it provide various inbuilt function but we  mostly commonly two inbuilt function clrscr(),getch().

clrscr() and getch() is mostly used in Turbo C compiler So if you are using some online compiler or any other compiler then just remove conio.h header file and this two function because  newly developed compiler provide this functionality by default(no need explicity used).clrscr() function helps us to clear the screen of previously executed program, getch() function is also play most important in few c compiler,it is just to hold the screen.

Let's understand more clearly,when we execute our code in Turbo C compiler then the output is come and go so fast,we are not able to see the output properly.So to hold and see the output on screen we use getch() function.

In the next line of code,we have defined main function in which we are going to define the actual logic of our program.those who don't I would like to tell them that main function is a function from which the execution of c program is started and there can be only main function in c program if we try to define more than one main function then the compiler will us error.

In the next line we have declared two variable of type int(Integer) c,n.In which c variable will be used as a digit counter and we will use n variable to take user input of which number user want to find total number of digit.

In the next line we are printing the following statement "Enter a Number" from this statement user will get to known program is waiting for our input and unless and until we don't type any input the program will not move forward because after the printf() statement we have written scanf() function which help us to take user input and stored in n variable As you can see that we have wrote %d format specifier in scanf() because we are storing the input in integer variable and I would like to add more important thing in it is ,we are using &(ampersand) before variable n because it will tell compiler that we want to store the input in memory location assigned to n variable.

In the next line of our program,we have wrote one while loop with some condition.

while( n! = 0)

The meaning of this condition n ! = 0(n is not equal to 0) execute the code inside the while loop unless and until this statement become false i.e. this while loop will be executed unless the value of n becomes 0.

So now let's move inside the while loop iteration by iteration

Sample value of n = 757

1st iteration 

while (n ! = 0)

while 757 ! = 0 (True)

{    

    n = 757 / 10 

 //now the value of  n will be 75 because when we divide 757/10 then it should return  75.7(In general mathematics) since we are using  integer data type variable n to store result  therefore it will truncate the value after decimal point so it will return only 75.

c = c + 1//(by default the value of integer type variable is 0 but after incrementing it now the value of c is 1) previously the value of c was 1 so now the value will be 2

}

2nd iteration 

while (n ! = 0)

while 75 ! = 0 (True)

{    

    n = 75 / 10 

 //now the value of  n will be 7 

c = c + 1//(previously the value of c was 1 so now the value will be 2) 

}

4rd iteration 

while (n ! = 0)

while 0 ! = 0 (False)

In the fourth iteration the value of n become 0 therefore n ! = 0 will not be satisfied and the control of program will come outside and statement outside the while will be executed.

First Let's whether our answer is correct or not

our input was 757 it means it contain 3 digit

and the value of our output variable c(digit counter) is also 3.

Therefore our logic is correct and program is working fine. 

Outside  the while loop we have written one print statement in which we are printing "No.of Digit=%d" In this no.of digit will be printed as it is and the value of variable c(digit counter) will be printed since it is of integer data type that's why we have used % format specifier 

In the next line we have written getch() statement to hold the screen after execution and return 0 statement to tell the compiler that the program is executed normally.

 


If you any have doubt,suggestion regarding this post or website then feel free to share with us.
 
If you have learned something new from this post then share this post with your family and
friends.
 
Happy Learning :)😎  

  Previous Next

Post a Comment

Previous Post Next Post