Reverse the part of the number which is present from
position i to j. Print the new number.
eg:
num=789876
i=2
j=5
778986



Reverse the part of the number which is present from position i to j. Print the new number. eg: n..

Answer / abdur rab

#include <stdio.h>

void reverse ( int* ip_array, int st_pos, int ed_pos )
{
if ( ( ip_array ) && ( st_pos < ed_pos ) ) {
* ( ip_array + st_pos ) ^= * ( ip_array +
ed_pos ) ^= * ( ip_array + st_pos ) ^= * ( ip_array +
ed_pos );
reverse ( ip_array, ++st_pos, --ed_pos );
}
}

int main ( int argc, char* argv [] )
{
int int_array [20];
int number = 789876;
int counter = 0;
int nloop = 0;
int start_pos = 2;
int end_pos = 5;

/* split the number into an array */
while ( number ) {
int_array [ counter++ ] = number % 10;
number = number / 10;
}

/* reverse the splited array */
reverse ( int_array, 0, counter - 1 );

/* reverse for the particular position */
if ( ( start_pos < end_pos ) && ( end_pos <=
counter ) ) {
reverse ( int_array, ( start_pos - 1 ), (
end_pos - 1 ) );

number = 0;
for ( nloop = 0; nloop < counter; nloop++ )
{
number *= 10;
number += int_array [ nloop ];
}

printf ( "\n %d", number );
}

return ( 0 );
}

Is This Answer Correct ?    2 Yes 1 No

Post New Answer

More C Interview Questions

What are the advantages of using Unions?

0 Answers   IBS,


If I have a char * variable pointing to the name of a function ..

0 Answers  


Why c is called free form language?

0 Answers  


what is the benefit of c30

2 Answers  


Why ordinary variable store only one value  

0 Answers  






void main() { int s[4][2]={ {1234,56},{1212,33},{1434,80},{1312,78} }; int (*p)[2]; int i,j,*pint; for(i=0;i<=3;i++) { p=&s[i]; pint=p; printf("\n"); for(j=0;j<=1;j++) printf("%d",*(pint+j)); } } while running this program it shows a warning-suspicious pointer conversion ie pint=p; my que is why should we assign the value of p to pint again.why cant we use it directly as *(p+j)..but if i use like tat the o/p is garbage value..

1 Answers  


Is c programming hard?

0 Answers  


how to exchnage bits in a byte b7<-->b0 b6<-->b1 b5<-->b2 b4<-->b3 please mail me the code if any one know to rajeshmb4u@gmail.com

3 Answers   Honeywell, Huawei,


Explain how can I right-justify a string?

0 Answers  


Design a program using an array that lists even numbers and odd numbers separately from the 12 numbers supplied by a user.

8 Answers  


write a c program in such a way that if we enter the today date the output should be next day's date.

0 Answers  


What are valid signatures for the Main function?

0 Answers  


Categories