Click here to Skip to main content
16,017,857 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Write a C program to read any integer from user and compute the reverse of given number. Also output 
whether the reverse number is prime or not. Sample Input/Output: 
Enter any integer: 4521
Reverse number is: 1254. It is not a prime number.


What I have tried:

#include<stdio.h>

int main()
{
    int i,p,num, ans = 0;

    
    scanf("%d",&num);

   printf("Please enter a number: \n");
   scanf("%d", &num);
   for(i=1; i<=num; i++)
   {
      if(num%i==0)
      {
         p++;
      }
   }
   if(p==2)
   {
      printf("Entered number is %d "\
             "and it is a prime number.",num);
   }
   else
   {
      printf("Entered number is %d "\
             "and it is not a prime number.",num);
   }
    while(num > 0)
    {
        
        int mod = num % 10;

        ans = ans * 10 + mod;

      
        num = num / 10;
    }

 
    printf("Reversed Number is %d\n",ans);

    return 0;
}
Posted
Updated 24-Feb-22 1:41am

1 solution

Loads of things you need to think about here:
1) Why are you reading the number first, then prompting for it, then reading it again?
2) What value does p have when the app starts?
3) Since every number is divisible by 1 and itself by definition, why are you testing either of those values?
4) Since every number greater than 2 has been test divided by two, why do you have to go past half the number at all?
5) Since every number greater than 2 has been test divided by two, why do you have to heck any multiple of two? Is there a good reason to increment by one?
6) Why do you always run to the end of the loop? if you don't check 1 or the number, just use break to exit the loop as soon as you find a divisor.
 
Share this answer
 
Comments
Sakib1306 24-Feb-22 11:03am    
please help me re-write the code
OriginalGriff 24-Feb-22 11:41am    
No, that's not what we are here for - this is your homework, not mine, and your teacher wants to see what you can do, not me! LOL.
Plus, if I do it for you, you don't learn anything and the next exercise gets harder for you to do ...

Read my questions above one by one, and see if you can work out what I'm saying.
Start with the first one and it's be pretty obvious what you have to do!

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