Click here to Skip to main content
15,938,446 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i wrote a program in C to print me the reverse of a num

#include<stdio.h>  
 int main()    
{    
int n, reverse=0, rem;    
  scanf("%d", &n);    
  while(n!=0)    
  {    
     rem=n%10;    
     reverse=reverse*10+rem;    
     n/=10;    
  }    
  printf("The reverse of %d is %d",n , n , reverse);    
return 0;  
}   


i get error that "pre>too many arguments for format" in printf but i m new to C and learn that is the way for print to work

What I have tried:

.......................................................
Posted
Updated 19-Nov-22 3:39am
Comments
Michael_Davies 19-Nov-22 8:11am    
printf("The reverse of %d is %d",n , n , reverse);

Three parameters only two %d... so if n was 21 and reverse was 12 it would print 21 21.
merano99 19-Nov-22 8:29am    
You never will see 21 at the end.
Michael_Davies 19-Nov-22 9:21am    
sorry...meant 12 12 ... remmebering I said "IF n was 21", obviously not 21 as it is the index in the loop so will be 0 at the end...
Pascal Vlad 19-Nov-22 8:21am    
so how can i write it please

Quote:
i get error that "pre>too many arguments for format"

C++
printf("The reverse of %d is %d",n , n , reverse);
                       ^ first argument request
                             ^ second argument request
                                 ^ first argument
                                     ^ second argument
                                        ^ third argument

Quote:
i wrote a program in C to print me the reverse of a num

If you input 1000, with your code, the result will be 1 instead of 0001.
Solution: handle the input like letters.
 
Share this answer
 
Look at your code:
C
printf("The reverse of %d is %d",n , n , reverse); 
And look at the error message: "too many arguments for format"
You call to printf has a format that uses two parameters, and yo0u provide three. Get rid of one n.

You should expect to get syntax errors every day, probably many times a day while you are coding - we all do regardless of how much experience we have! Sometimes, we misspell a variable, or a keyword; sometimes we forget to close a string or a code block. Sometimes the cat walks over your keyboard and types something really weird. Sometimes we just forget how many parameters a method call needs.

We all make mistakes.

And because we all do it, we all have to fix syntax errors - and it's a lot quicker to learn how and fix them yourself than to wait for someone else to fix them for you! So invest a little time in learning how to read error messages, and how to interpret your code as written in the light of what the compiler is telling you is wrong - it really is trying to be helpful!

So read this: How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^] - it should help you next time you get a compilation error!
 
Share this answer
 
Comments
Pascal Vlad 19-Nov-22 8:42am    
i ve done that and it s printing me The reverse of 0 is 0..whats wrong with my code?!
OriginalGriff 19-Nov-22 8:45am    
Now, let me think ... do you do anything with n that might change it's value between reading a number from the user into it and when you print it? :D
The n is always 0 at the end and is also output twice. The additional variable rem is actually not needed.

//update:
In principle, your code works, only small things would have to be changed. You should find these out by yourself very easily with a debugger.
C
int n, reverse = 0;
scanf("%d", &n);
for (int x=n; x > 0; x /= 10) {
   reverse = reverse * 10 + x % 10;
}
// printf("The reverse of %d is %d", n, n, reverse);
printf("The reverse of %d is %d", n, reverse);

If you want to create leading zeros like 0001 the advice of Patrice would be a possible solution.
 
Share this answer
 
v2
Comments
Pascal Vlad 19-Nov-22 8:34am    
i deleted it but still the problem exists
merano99 19-Nov-22 11:59am    
I don't know what problem is meant by "the problem exists". I have extended my solution again.

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



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