Find the bug in this C program!

Status
Not open for further replies.

prateek

Banned
Banned
266
2009
72
0
Hello Guys,
I am new in C Programming

I had wrote a code to reverse the number given by the user

but the problem is

When the user inputs number less than 10 it works fine
but just as the user inputs number like 134 or 351 nothing happens

help me what is the problem

here is the program
Code:
#include<stdio.h>
#include<conio.h>
main()
	{
		int num=0, revnum=0, temp=0;
		printf("Program to reverse the number");
		printf("\n\nEnter a Number ");
		scanf("%d",&num);
		if (num<=10)
			printf("\nEnter Number Greater than 10\n");
		else
			{
				while(num!=0)
				{
					temp=num%10;
					revnum=revnum*10+temp;
					num=num%10;
				}
				printf("Reversed Number=%d",revnum);
			}
		getch();
	}



Please don't write your own new code , i want just to find the bug in the program
 
16 comments
Hey! i got it

there was only one silly mitake

i am soo foolish


the mistake was
this


Wrong : num=num%10;
Correct : num=num/10;
 
Here is the working correct program

Code:
#include<stdio.h>
#include<conio.h>
main()
	{
		int num=0, revnum=0, temp=0;
		printf("Program to reverse the number");
		printf("\n\nEnter a Number ");
		scanf("%d",&num);
		if (num<=10)
			printf("\nEnter Number Greater than 10\n");
		else
			{
				while(num!=0)
				{
					temp=num%10;
					revnum=revnum*10+temp;
					num=num/10;
				}
				printf("Reversed Number=%d",revnum);
			}
		getch();
	}
 
Well i would use a different method

Note i code in Dev C++

Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>

main()
{
      
      char a1[20];
      printf("\tEnter the number here : ");
      scanf("%s",&a1);
      printf("\tThe Reverse of the number is %s",strrev(a1));
getch();      
}
 
@bharat
thanks , i will surely visit that site and learn new things
Yes, I have books :) for C

@Mind Freak™
Since I am new in C , we haven't yet taught array and also that function strrev

but your coding is very small and :) cool
 
Well it is a small function and simple in coding

but the only small tiny thing is that it will reverse words too xD :p

Update :

It reverses number also ur basic work is getting finished xD
 
Mind Freak™ coding is advance and developed in DEV C++ , anyways you let stick to the basic concept of c first and for logic just use some tricky questions . You'll get into it .
 
If you want to reverse the number in 4 steps do -

Accept number
itoa
strrev
atoi

Such question Are Asked During Placements.
 
Allan, as far as I know, itoa and atoi are C++ functions, not C and in this case he's coding in C.

Also it's likely more efficient using the way he's doing it as you're only dealing with 3 integers at any one time, no arrays.
 
ah seems they are in the stdlib, my bad.

It remains though that it'd be quite less efficient than his current code so, really, it isn't needed.
 
Status
Not open for further replies.
Back
Top