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
What is external variable in c?
What is variable initialization and why is it important?
What should malloc() do?
Explain setjmp()?
write a program for the normal snake games find in most of the mobiles.
What is the explanation for modular programming?
Without Computer networks, Computers will be half the use. Comment.
A collection of functions,calls,subroutines or other data a) library b) header files c) set of files d) textfiles
Why do we use int main?
How to throw some light on the b tree?
What are the 3 types of structures?
What is meant by realloc()?
What is wrong in this statement? scanf(ā%dā,whatnumber);
What is the purpose of realloc()?
Explain the difference between structs and unions in c?