Prime no between m and n - c

I tried a prime no generating question in SPOJ(link : http://www.spoj.com/problems/PRIME1/) .
I'm using seive algorithm . I get the SIGSEGV error when i use spoj gcc . But when i compiled using my ubuntu gcc it works for all the test cases.
Here is my source code . Plz Help
float sqroot( float x)
{
float a , b;
a = x; // copy given value to 'a'
do
{
b = a; // copy value of 'a' to 'b' before 'a' is modify
a = (a + x/a) / 2; // modify 'a' value until we reach sqroot result
}
while( a!= b); // execute loop until a == b
return( a); // 'a ' or 'b' is sqroot of 'x'
}
int main()
{
int prime[4000];
int prime_index=0;
bool find_prime[100001];
int i,j;
int m,n;
int iremainder;
int T,t_index;
int PRIME_FLAG=1;
float square;
int limit;
prime_index++;
prime[prime_index]=2;
for(i=3;i<=32000;i=i+2)
{
PRIME_FLAG=1;
square = sqroot((float)i);
limit = ((int)(square))+1;
for(j=1;j<=prime_index,prime[j]<=limit;j++)
{
if(prime[j]!=0)
{
if((i%prime[j]) == 0)
{
PRIME_FLAG = 0;
break;
}
}
}
if(PRIME_FLAG)
{
prime_index++;
prime[prime_index]=i;
printf("%d\n",i);
}
}
printf("Enter the no of test cases:");
scanf("%d",&T);
if(T<=10)
{
for(t_index=1;t_index<=T;t_index++)
{
printf("Enter the values of m and n :");
scanf("%d%d",&m,&n);
if((m>=1) && (n<=1000000000) && ((n-m)<=100000))
{
if(m == 1)
m=2;
//Set all numbers from m to n as prime
for(i=m;i<=n;i++)
find_prime[i]=true;
//Find the prime numbers between m to n
square = sqroot((float)n);
limit = ((int)(square))+1;
for(i=1;i<=prime_index,prime[i]<=limit;i++)
{
if(m>=prime[i])
{
if(prime[i]!=0)
iremainder=m%prime[i];
j=prime[i]*iremainder;
}
else
{
iremainder=prime[i]-m;
if(m+iremainder == prime[i])
j=2*(m+iremainder);
else
j=m+iremainder;
}
for(;j<=n;j=j+prime[i])
find_prime[j]=false;
}
//Print all prime no's
for(i=m;i<=n;i++)
{
if(find_prime[i])
printf("%d\n",i);
}
}
}
}
return 0;
}

your for loops are wrong.
for(j=1;j<=prime_index,prime[j]<=limit;j++)
should be
for(j=1;(j<=prime_index)&&(prime[j]<=limit);j++)
the first condition will be executed, but the result will be ignored.
So it is luck, that you find a number in the uninitialized array that is larger than limit before you go out of range of the array, which will lead to a SIGSEGV.
for(i=1;i<=prime_index,prime[i]<=limit;i++)
has the same problem.
Click here for more details

Related

what thing i should change from this code

I want to make a program to count the sum of digits in a string but only using stdio.h
but the program needs to count until its less than 10
so the example you input 56 it would be 5+6=11 then 1+1=2 and so on
here's my code. For now I'm just confused how to check if its whether more than 9 or not
#include<stdio.h>
int plus(int n);
int main(void)
{
int n, digit, test;
scanf("%d", &n);
test = plus(n);
while(test != 0)
{
if(test > 9)
plus(test);
else
break;
}
printf("%d", test);
}
int plus(int n)
{
int digit=0,test=0;
while(n != 0)
{
digit = n%10;
test = test + digit;
n = n/10;
}
return test;
}
You are not storing the value returned by plus function in the while body.
You can change the condition in while to check whether it is greater than 9 or not, and assign test as test = plus(test);
So, your while will look like this.
while(test > 9)
{
test=plus(test);
}
You need to recursively call the function plus() until the value returned by it becomes less than 10. Like shown below:
int main(void)
{
int n=56;
while(n> 10)
{
n = plus(n);
}
printf("%d", n);
}

finding count of minima or maxima in a set of numbers

The user inputs n number of datas to be followed in the next line and
then it inputs n numbers a1,a2,...,an. These numbers are heights of a
some mountains. The set of these numbers is "ACCEPTABLE" if there is
only one maxima or minima. for example "1 2 3 2 1", has only one
maxima which is 3. Also "1 2 3 4" has one maxima. but "1 10 9 8 7 6 5
6 7" is not acceptable because it has two maxima (10 and 7) or two
minima (1 and 5).
In other word, the set is acceptable if and only if it is in one of
this forms:
a1<=a2<=a3 ... <= ai > a(i+1) > ... >an
or
a1>=a2>=a3 ... >= ai < a(i+1) < ... < an.
I must submit the answer in a judge system which tests it with unknown test cases. Using any type of Array or Vector is completely prohibited.
My solution is this:
//C code.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,temp;
scanf("%d",&n);
if (n==1)
{
int l;
scanf("%d",&l);
printf("Yes");
}
else
{
int a,b;
int last;
int changes =0;
int dec =0 , inc =0; //flag: checking if the set is incremental or decremental till now
scanf("%d %d",&a,&b);
if (a>b)
{
dec=1;
}
else if (a<b)
{
inc = 1;
}
else
{
inc =1;
dec = 1;
}
last = b;
for (int i =2;i<n;i++)
{
scanf("%d",&temp);
if (temp>last && dec==1)
{
inc = 1;
dec= 0;
changes++;
}
if (temp<last && inc==1)
{
inc =0;
dec=1;
changes++;
}
if (!(inc==1 && dec==1) && temp == last)
{
changes++;
}
last = temp;
last = temp;
}
if (changes <=1)
{
printf("Yes");
}
else
{
printf("No");
}
}
return 0;
}
It gets the right answer for the examples that are in the question but it fails on some unknown test cases. Any idea how to fix this? Can anyone give me a test case that is not solved right in this code?
P.1:
I added
if (!(inc==1 && dec==1) && temp == last)
{
changes++;
}
and it accepted one of the failing test cases but still one remains.
P.2:
This is my other algorithm which fails on some test cases but the judge accepts its answers on the failing test cases of the first one:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
int inc=0;
int dec=0;
int peak=0;
int valley=0;
int last = -1;
int a;
scanf("%d",&n);
for (int i =0;i<n;i++)
{
if (last!=-1)
{
last =a;
}
scanf("%d",&a);
if (last!=-1)
{
if (a>last)
{
if (!(inc==1))
{
valley++;
inc =1;
dec=0;
}
}
if (a<last)
{
if (!(dec==1))
{
peak++;
dec=1;
inc =0;
}
}
}
last =0;
}
if (valley<=1 && peak<=1)
{
// printf("valley: %d , peak:%d",valley,peak);
printf("Yes");
}
else
{
printf("No");
}
return 0;
}
P.3
New algorithm:
#include <stdio.h>
#include <stdlib.h>
int main()
{
long long int n,temp;
scanf("%lld",&n);
if (n==1)
{
long long int l;
scanf("%lld",&l);
printf("Yes");
}
else
{
long long int a,b;
long long int last;
long long int changes =0;
int dec =0 , inc =0; //flag: checking if the set is incremental or decremental till now
scanf("%lld %lld",&a,&b);
if (a>b)
{
dec=1;
}
else if (a<b)
{
inc = 1;
}
else
{
inc =1;
dec = 1;
}
last = b;
for (long long int i =2;i<n;i++)
{
scanf("%lld",&temp);
if (temp>last && dec==1)
{
inc = 1;
dec= 0;
changes++;
}
if (temp<last && inc==1)
{
inc =0;
dec=1;
changes++;
}
if (changes>=1 && temp == last)//new change
{
changes+=100;
}//end of new change
last = temp;
}
if (changes <=1)
{
printf("Yes");
}
else
{
printf("No");
}
}
return 0;
}
scanf("%d",l); should be scanf("%d", &l);, scanf requires the address of the variable.
So the test cases with n == 1 failed.
Always have a look at the compiler warnings: https://ideone.com/MKq3WK
“4 1 1 2 1” says “No” but should say “Yes”. The code does not correctly handle the case where inc and dec are both 1 initially.
Additionally, the code must use different criteria for the first part of the sequence (up until the first change in direction is observed) and the second. In the first part, equality is permitted and does not cause any disqualification or change of state. In the second part, equality is disqualifying.
Joke
I should not do this, but sometimes one cannot resist. The following should solve the problem. Do not use it.
#include <stdio.h>
int main(void)
{
int c, n, p, s;
scanf("%d%d", &n, &c);
#define Table \
{ 1, 0, 3 }, \
{ 1, 1, 2 }, \
{ -1, -1, 2 }, \
{ 4, 3, 3 }, \
{ 4, -1, -1 }, \
for (s = 0; 0 <= s && --n; s = (int [][3]) {Table} [s] [1+(p>c)-(p<c)])
{ p = c; scanf("%d", &c); }
printf("%s\n", 0 <= s ? "Yes" : "No");
}
With the P.2 solution, "4 1 2 1 1" is accepted, but it should not, since 1 is not greater than 1!
We have to detect a change of direction of the inequality, then disallow a further change. Anyway, as long as the elements are equal, you cannot make up your mind between growing or decreasing.
I would use a state variable with six possible values:
x: no element has been input yet;
0: we don't know anything;
1: we are in the initial raising section;
-1: we are in the initial falling section;
2: we are in the final raising section;
-2: we are in the final falling section.
Considering the previous and current input values, the following transitions apply:
State x: -> 0 (unconditionally)
State 0: p < c -> 1, p > c -> -1
State 1: p > c -> -2
State -1: p < c -> 2
State 2: p >= c -> Fail
State -2: p <= c -> Fail
The initial state is x. If we never fail, success is assumed when the input has been exhausted. This can be implemented with a simple switch statement and two static variables to remember the previous value and the state.
The specifications are unclear about whether some section can be empty, so I'll leave it as is.
Python proof of concept:
Input= [1, 2, 3, 2, 1]
#Input= [1, 10, 9, 8, 7, 6, 5, 6, 7]
#Input= [1, 2, 3, 4]
#Input= [4, 1, 2, 1, 1]
def Process(c):
global p, s
if s == None:
s= 0
elif s == 0:
if p < c:
s= 1
elif p > c:
s= -1
elif s == 1:
if p > c:
s= -2
elif s == -1:
if p < c:
s= 2
elif s == 2:
if p >= c:
exit(-1)
elif s == -2:
if p <= c:
exit(-1)
p= c
s= None
for c in Input:
Process(c)
The judge finally accepted this code. The main problems was with the conditions were some numbers get equal like 111222. Also the problem statement was right and it was not <= or >= after the ai.
Code in C:
#include <stdio.h>
#include <stdlib.h>
int main()
{
long long int n,temp;
scanf("%lld",&n);
if (n==1)
{
long long int l;
scanf("%lld",&l);
printf("Yes");
}
else
{
long long int a,b;
long long int last;
int flag =1;
long long int changes =0;
int incr=0,decr=0;
int equ=0;
int dec =0 , inc =0; //flag: checking if the set is incremental or decremental till now
scanf("%lld %lld",&a,&b);
if (a>b)
{
dec=1;
}
else if (a<b)
{
inc = 1;
}
else
{
equ=1;
}
last = b;
for (long long int i =2;i<n;i++)
{
scanf("%lld",&temp);
if (temp > last && equ==1)
{
inc = 1;
dec=0;
equ=0;
}
else if (temp <last && equ==1)
{
inc = 0;
dec = 1;
equ= 0;
}
else if (temp>last && dec==1)
{
inc = 1;
dec= 0;
changes++;
incr++;
}
else if (temp<last && inc==1)
{
inc =0;
dec=1;
changes++;
decr++;
}
if (changes>=1 && temp == last && incr>=0 && decr >=0)
{
flag = 0;
changes+=100;
}
last = temp;
}
if (changes <=1&& flag)
{
printf("Yes");
}
else
{
printf("No");
}
}
return 0;
}

c program to check valid sudoku

i'm writing c code to check whether given matrix is valid sudoku solution or not.
Input would be matrix of n*n size.
I have written code to check row and column but i'm not getting how to validate grids of sqrt(n)*sqrt(n) size.
my code is here
#include<stdio.h>
int main()
{
int i,j,count=0,sumrow;
int sumcol;
int n;
scanf("%d",&n);
int arr[n+1][n+1];
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&arr[i][j]);
for(i=1;i<=n;i++)
{
sumcol=0;
for(j=1;j<=n;j++)
sumcol+=arr[j][i];
if(sumcol!=(n*(n+1)/2))
count++;
}
for(i=1;i<=n;i++)
{
sumrow=0;
for(j=1;j<=n;j++)
{
sumrow+=arr[i][j];
}
// printf("%d\n",sumrow);
if(sumrow!=(n*(n+1)/2))
count++;
}
//printf("count%d ",count);
if(count==0)
printf("yes");
else
printf("no");
return 0;
}
Here i have a better solution. Instead of sum, we can use an integer flag.
//read sudoku
for(i=0;i<9;i++)
for(j=0;j<9;j++)
{
scanf("%c",&c);
a[i][j]=c-'0';
}
//checking rows
for(i=0;i<9;i++)
{
flag=0x0000;
for(j=0;j<9;j++)
flag|=1<<(a[i][j]-1);
if(flag!=0x01FF)
report("row",i,j-1);
}
//checking cols
for(j=0;j<9;j++)
{
flag=0x0000;
for(i=0;i<9;i++)
flag|=1<<(a[i][j]-1);
if(flag!=0x01FF)
report("col",i-1,j);
}
//checking Squares (3x3)
for(si=0;si<3;si++)
{
for(sj=0;sj<3;sj++)
{
flag=0x0000;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
flag|=1<<(a[si*3+i][sj*3+j]-1);
}
if(flag!=0x01FF)
report("square",si*3+i-1,sj*3+j-1);
}
}
printf("\nThe sudoku is correct");
For detailed description, You may visit this link
You can check each of the smaller squares whether they are valid or not.For example if n=9 you have to check each of the 3 by 3 squares.To check each smaller square you can use an array of 10 elements and check whether any of the 1 to 9 value is repeated or not.
The algorithm is as follows
for each of the smaller grids
if(any of the digit repeats in a smaller grid)
return 0;
return 1;//valid
Following is some code to do the same
//A is the grid
int small=sqrt(n);
for(int i=0;i<small;i++)
{
for(int j=0;j<small;j++)
{
int row=small*i;//this will find the corresponding row
int col=small*j;//this will find the corresponding column
vector<int> used(10,0)
for(int p=row; p<row+small; p++) {
for(int q=col; q<col+small; q++)
{
if(A[p][q]=='0')//0 is not valid
return 0;
if(used[A[p][q]-'0']==1)//this digit has already been used
return 0;
if(used[A[p][q]-'0']) return 0;
used[A[p][q]-'0']=1;//now this particular digit has occurred and should not occur again
}
}
}
}
//if all's well return 1
return 1;
Note that the above code assumes that entire grid is filled and is not empty.If you want to check a partially filled grid,introduce a check.
int main() {
int a[10][10],i,j,n,k,sum,sum1,sum2,sum3,x,l;
printf("enter the size N of N*N sudoku\n");
scanf("%d",&n);
printf("enter the entries of sudoku row wise \n");
for (i=1;i<=n;i++) {
for (j=1;j<=n;j++) {
scanf("%d",&a[i][j]);
}
printf("\n");
}
printf("---------------------------------\n\n\n\n");
printf("the matrix you entered is \n");
for (i=1;i<=n;i++) {
for (j=1;j<=n;j++) {
printf("%d",a[i][j]);
printf("|");
}
printf("\n");
}
for (i=1;i<=n;i++) {
for (k=i;k==i;k++) {
sum=0;
for (j=1;j<=n;j++) {
sum = sum + a[i][j];
}
if(sum!=45)
x=1;
}
}
for (j=1;j<=n;j++) {
for(k=j;k==j;k++) {
sum=0;
for (i=1;i<=n;i++) {
sum = sum+a[i][j];
}
if (sum!=45)
x=1;
}
}
for (k=1;k<=3;k++) {
l = (1+(k-1)*n/3);
for (i=l;i<=k*n/3;i++) {
for(j=1;j<=3;j++) {
sum1 = sum1+a[i][j];
}
for (j=4;j<=6;j++) {
sum2 = sum2+a[i][j];
}
for (j=7;j<=9;j++) {
sum3 = sum3+a[i][j];
}
}
if (sum1!=45||sum2!=45||sum3!=45)
x=1;
}
if (x==1)
printf("sudoku not correct \n");
else
printf("correct sudoku");
return 0;
}
bool validateMatrix(int g_iMatrix1[][MAXCOLS],
int iROWS,
int iCOLS)
{
bool bRowUsed[MAXROWS][MAXCOLS] = {0};
bool bColUsed[MAXROWS][MAXCOLS] = {0};
bool bBlockUsed[MAXROWS][MAXCOLS] = {0};
//Matrix to keep record if current value is already set in current row..
memset(bRowUsed, false, (MAXROWS) * (MAXCOLS));
//Matrix to keep record if current value is already set in current column..
memset(bColUsed, false, (MAXCOLS) * (MAXCOLS));
//Matrix to keep record if current value is already set in current block of iSQRT * iSQRT..
//Lets assume the matrix is of size 9 * 9..
//So there will be 9 block of 3 * 3..
//Number the blocks from left to right as 0 to 8..
//We will be mapping 0 the block to 0th row, 1st block to 1st row, 2nd block to 2nd row and so on..
memset(bBlockUsed, false, (MAXROWS) * (MAXCOLS));
int iRows = 0,iCols = 0;
int iSQRT = int(sqrt(MAXCOLS));
for(iRows = 0;iRows < iROWS;iRows++)
{
for(iCols = 0;iCols < iCOLS;iCols++)
{
if(bRowUsed[iRows][g_iMatrix1[iRows][iCols] - 1] == true)
{
return false;
}
if(bColUsed[g_iMatrix1[iRows][iCols] - 1][iCols] == true)
{
return false;
}
//Number the blocks from left to right as 1 to 9..
//We will be mapping 0 the block to 0th row, 1st block to 1st row, 2nd block to 2nd row and so on..
//((iRows / iSQRT) * iSQRT) + (iCols / iSQRT) will map the block with above logic..
if(bBlockUsed[((iRows / iSQRT) * iSQRT) + (iCols / iSQRT)][g_iMatrix1[iRows][iCols] - 1] == true)
{
return false;
}
bRowUsed[iRows][g_iMatrix1[iRows][iCols] - 1] = true;
bColUsed[g_iMatrix1[iRows][iCols] - 1][iCols] = true;
bBlockUsed[((iRows / iSQRT) * iSQRT) + (iCols / iSQRT)][g_iMatrix1[iRows][iCols] - 1] = true;
}
}
return true;
}

C- Fix Stack overflow in Recursion

A code in C to find maximum of an array using divide and conquer but it keeps throwing
"stack overflow exception" . Help would be appreciated!
int a[10];
int find(int l,int h)
{
int x;
if(h==0)
{
x=0;
return x;
}
else
{
if(h==1)
{
if(a[0]>a[1])
{
x=0;
return x;
}
else
{
x=1;
return x;
}
}
else
{
int mid,z,y;
mid=(l+h)/2;
y=find(0,mid);
z=find(mid+1,h);
if(a[y]<a[z])
{
x=z;
}
else
{
x=y;
}
return x;
}
}
}
There are only limited variables and I don't see where the function can go into an infinite recursion.
int main()
{
int i,n,max,min,ans;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
ans=find(0,n-1);
printf("the maximum element is- %d\n",ans);
getch();
return 0;
}
Consider the case where you call find(0, 2). Since h > 1, you enter the second else clause, and mid is 1. Then on the second recursive call, it is to find(2, 2). On this recursive call, you again enter the second else, since h is still 2. But the mid is also 2. Now, the first recursive call goes to find(0, 2), which enters an infinite loop.
find(0, 2)
h not 0
h not 1
mid = 1
find(0, 1)
find(2, 2)
h not 0
h not 1
mid = 2
find (0, 2) <-- loop
It seems the intention of the if checks on h is to prevent the mid calculation from being the same as l. If so, then you can calculate the mid variable at the top of your function, and use that as the stopping condition.
It looks like this is an attempt to use divide and conquer to locate the position of the maximum element in the array a. If so, then your first recursive call should restrict itself to the range of [l..mid] instead of going back to 0.
Putting it all together:
int find(int l,int h)
{
int mid = (l+h)/2;
if (mid == l)
{
return (a[l] > a[h]) ? l : h;
}
else
{
int y = find(l, mid);
int z = find(mid+1, h);
return (a[y] > a[z]) ? y : z;
}
}
Here is your code modified, which is running successfully..
The problem is that you weren't checking the difference between l and h but only the value of h...
#include <iostream>
using namespace std;
int a[10];
int find(int l,int h)
{
int x;
if(h-l==0)
{
return h;
}
else
{
if(h-l==1)
{
if(a[l]>a[l+1])
{
return l;
}
else
{
return l+1;
}
}
else
{
int mid,z,y;
mid=(l+h)/2;
y=find(0,mid);
z=find(mid+1,h);
if(a[y]<a[z])
{
x=z;
}
else
{
x=y;
}
return x;
}}}
int main()
{
a[0]=3;
a[1]=7;
a[2]=5;
cout<<find(0,2)<<endl;
return 0;
}
you're using wrong conditions, try:
first if: if(h==l)
second if: if(h-l==1)
third if:
if(a[h]>a[l]) {
return h;
} else {
return l;
}

palindrome of number in C

#include<stdio.h>
int main(void)
{
int rev=0,temp=0,frwd,n,ans=0;
int i,j;
for(i=100;i<=999;i++)
{
for(j=i;j<=999;j++)
{
n = i*j;
frwd = n;
while(n!=0)
{
temp = n%10;
n = n/10;
rev = temp+rev*10;
}
printf("%d",rev);
if((rev == frwd)&&(ans<frwd))
{
ans=frwd;
printf("%d",ans);
}
}
}
return(0);
}
I have tried working out everything but this code doesn't seem to give the correct output.
The desired output is largest palindrome number of 6 digits.
If I am running the individual parts i.e. the reversing of number , checking of number whether or not it is a palindrome or the for loops, they are working fine but in the program they are giving garbage as output.
Any help would be appreciated.
ya the problem is that you are not reinitializing rev to 0 as said by cowanother.anon.ard. Try putting rev=0 in inner for loop.
But you cant get 999999 as the highest palindrome number of 6 digit by your method as u r not checking all the 6 digit numbers.
#include<stdio.h>
int main(void)
{
int rev=0,temp=0,frwd,n,ans=0;
int i,j;
for(i=100000;i<=999999;i++)
{
frwd = n = i;
rev = 0;
while(n!=0)
{
temp = n%10;
n = n/10;
rev = temp+rev*10;
}
if((rev == frwd)&&(ans<frwd))
{
ans=frwd;
}
}
printf("%d\n",ans);
return(0);
}
4 problems with your Code:-
Like another.anon.coward said- you need to put rev=0 inside inner loop
You need to separate each number printed either by a space or a newline ('\n')
printf("\n %d");. Otherwise they will look like one big number (garbage).
Your algorithm is also wrong. According to your program, the largest 6-digit number is 906609 (The correct answer is 999999). For this you should change your inner loop to j=0;j<999;j++ and change n=i*j to n=i*1000+j.
Also move the printf("\n%d",ans); outside the loop.
The corrected program is:
#include <stdio.h>
int main(void)
{
int rev=0,temp=0,frwd,n,ans=0;
int i,j;
for(i=100;i<=999;i++)
{
for(j=0;j<=999;j++) /*CORRECTED THIS LINE,*/
{ rev=0;/*ADDED THIS LINE;*/
n = (i*1000) + j; /*CORRECTED THIS LINE*/
frwd = n;
while(n!=0)
{
temp = n%10;
n = n/10;
rev = temp+rev*10;
}
printf("\n%d",rev); /*THIS LINE,*/
if((rev == frwd)&&(ans<frwd))
{
ans=frwd;
}
}
}
printf("\n%d",ans); /* AND THIS LINE*/
return(0);
}
#include <stdio.h>
int main(void)
{
int rev=0,temp=0,frwd,n,ans=0;
int i,j;
for(i=100;i<=999;i++)
{
for(j=0;j<=999;j++) /*CORRECTED THIS LINE,*/
{ rev=0;/*ADDED THIS LINE;*/
n = (i*1000) + j; /*CORRECTED THIS LINE*/
frwd = n;
while(n!=0)
{
temp = n%10;
n = n/10;
rev = temp+rev*10;
}
printf("\n%d",rev); /*THIS LINE,*/
if((rev == frwd)&&(ans<frwd))
{
ans=frwd;
}
}
}
printf("\n%d",ans); /* AND THIS LINE*/
return(0);
}

Resources