How to remove the middle Digits from a number in C.
The code for Question is given below.
// Program to remove all middle digits of number in c
1. #include<stdio.h>
2. #include<conio.h>
3. void main() {
4. int n,m;
5. clrscr();
6. printf("Enter Any Number:-");
7. scanf("%d",&n);
8. m=n%10;
9. while(n>0) {
10. n=n/10;
11. if(n%10!=1) {
12. continue;
13. }
14. m=(n%10)*10+m;
15. }
16. printf("New Number:- %d",m);
17. getch();
18.}
Please Find Image Of Code Given Below:-

Find the output given below:-

Let me explain the code:
In the given code we first take two variables n and m. we take input in n and then in line 8 we have stored the last digit of the number in m. For example, we have taken the number 12345 a five-digit number and from this, we extract '5' as the last digit number from it. And stored in m.
After that, we make a loop in which condition checks are n greater than 0 if it's true then control goes to the next statement n=n/10; in this statement, n is replaced by a quotient after evaluating that expression. it will iterate until the condition is true.
In the next line if statement checks the condition (n% 10 ! = 1) is true or false. if true then "continue;" statement will be executed rather than the next line will be executed. the "if" condition checks after n dividing by 10 the remainder are not equal to 1. if true then it continues. if not then the next condition will be executed.
In the next statement m stores, the ((n%10)*10 + m) means the remainder left after dividing by 10 and multiplies by 10 and after that added by m previous value.
Hence then you will get an exact result. The middle numbers are removed by IF condition which in a loop. it always continues until it goes to the first place of a number.
I hope you have got your answer.
Comments
Post a Comment