Click here to Skip to main content
15,888,803 members
Please Sign up or sign in to vote.
1.77/5 (4 votes)
See more:
C#
#include<stdio.h>
#include<math.h>
int main(void){
    long n, number = 0;
    int x=0, counter = 0;
    printf("Enter a Positive Number.\n=>");
    scanf("%ld", &n);
    while(n != 0){
        x = n%10;
        if(x != 0){
            x = x * pow(10, counter);
            number = number + x;
            counter++;
        }
        n= n/10;

    }
    printf("The Number after removing all '0' from given number is : %ld", number);
}


suppose I entered a enter 6204040. Then i'm expecting 6244 as a output, but i'm getting 6243 as output.
What is wrong in the code???
Posted

1 solution

Well - the best way to find out is to use a debugger.
This is a tool which allows you to run your program and look at exactly what is happening while it runs.
Put a breakpoint on the line
C++
while(n != 0){
And run your program. When it reaches the line it will stop to allow you to decide what happens next, and to look at what is in the variables.
Step through your program a line at a time, working out what you expect to happen each time, and comparing it to what does happen. If they aren't the same, then why not?
If they are, move on to the next line and keep going until something looks wrong.

When you find a difference, it should be reasonably obvious what the problem is!
This is a skill - predictably called debugging - which like all skills you only develop and improve by using. And it's a lot easy to learn when you are working with a tiny program like this one than later when you have half a million lines of code and don't know where the problem is! :laugh:

Give it a try - it's not difficult, really - and you will learn a very useful skill!
 
Share this answer
 
Comments
Dishank Bansal 24-Jan-16 8:15am    
Yeah!! thank you.
Debugging worked.
OriginalGriff 24-Jan-16 8:17am    
You're welcome!
Rahul VB 29-Jan-16 9:09am    
Griff !!!! you have not changed :) its been a long time :) answer 5ed
OriginalGriff 29-Jan-16 9:54am    
It has! How are you doing? Doesn't look like you've even logged in for six months or so...so you must have been busy!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900