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

Answer Posted / abdur rab

#include <stdio.h>

int get_digits ( int number )
{
int n_digits = 0;
while ( number ) {
n_digits++;
number = number / 10;
}

return ( n_digits );
}

int main ( int argc, char* argv [] )
{
int number = 789876;
int digits = 0;
int temp_number = 0;
int final_number = 0;
int counter = 0;

int start_point = 2;
int end_point = 5;

digits = get_digits ( number );

if ( ( start_point < end_point ) && ( end_point <=
digits ) ) {

temp_number = number;
if ( start_point - 1 ) final_number =
number / pow ( 10, ( digits - ( start_point - 1 ) ) );

counter = digits;
while ( temp_number ) {
if ( ( counter <= ( end_point ) )
&& ( counter >= ( start_point ) ) ) {
final_number *= 10;
final_number += (
temp_number % 10 );
}
temp_number /= 10;
counter--;
}
if ( digits - end_point ) {
final_number *= pow ( 10, ( digits -
end_point ) );
final_number += number % (int) (
pow ( 10, ( digits - end_point ) ) );
}
}

printf ( "\n Number: %d", number );
printf ( "\nFinal Number: %d", final_number );
printf ( "\nS_Pos: %d, E_Pos: %d", start_point,
end_point );

return ( 0 );

}

Is This Answer Correct ?    0 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is external variable in c?

613


What is variable initialization and why is it important?

619


What should malloc() do?

645


Explain setjmp()?

657


write a program for the normal snake games find in most of the mobiles.

1786






What is the explanation for modular programming?

685


Without Computer networks, Computers will be half the use. Comment.

1876


A collection of functions,calls,subroutines or other data a) library b) header files c) set of files d) textfiles

647


Why do we use int main?

610


How to throw some light on the b tree?

605


What are the 3 types of structures?

572


What is meant by realloc()?

677


What is wrong in this statement? scanf(ā€œ%dā€,whatnumber);

728


What is the purpose of realloc()?

672


Explain the difference between structs and unions in c?

574