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 / mahfooz alam

#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
using namespace std;
int getdnum(int num)
{
int numd=0;
while(num!=0)
{
numd++;
num=num/10;
}
return numd;
}
int reversenum(int i,int j ,int d,int num)
{
int a=(num/(pow(10,d-i+1)));
int b=(num/(pow(10,d-j)));
int c=num%static_cast<int>(pow(10,d-j));
int n=0;
int k;
for(k=0;k<=(j-i);k++)
{
n+=(b%10)*(pow(10,j-i-k));
b=b/10;
}
n=a*pow(10,d-i+1)+c+n*pow(10,d-j);
return n;

}
int main()
{
int i,j,k,l,m;
cin>>i>>j>>k;
int d=getdnum(i);
m=reversenum(j,k,d,i);
cout<<m<<endl;
return 0;
}

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Which one would you prefer - a macro or a function?

604


a program that can input number of records and can view it again the record

1486


What is memory leak in c?

637


What are local static variables? How can you use them?

647


what is the format specifier for printing a pointer value?

617






What are the valid places to have keyword “break”?

651


write a program to print largest number of each row of a 2D array

1872


Does c have function or method?

591


Explain what is a 'locale'?

585


Write a code on reverse string and its complexity.

608


Can you write the algorithm for Queue?

1556


Why isn't it being handled properly?

645


typedef struct{ char *; nodeptr next; } * nodeptr ; What does nodeptr stand for?

1076


How can I pad a string to a known length?

612


What is a structure in c language. how to initialise a structure in c?

609