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
Answers were Sorted based on User's Feedback
#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 |
Answer / 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 |
I use turbo C which allocates 2 bytes for integers and 4 bytes for long. I tried to declare array of size 500000 of long type using the following code... long *arr; arr=(long *)(malloc)(500000 * sizeof(long)); It gives a warning that "Conversion may lose significant digits in function main"... And the resulting array size was very less around 8400 as compared to 500000. Any suggestions will be welcomed....
What is exit() function?
Compare interpreters and compilers.
why to assign a pointer to null sometimes??how can a pointer we declare get assigned with a garbage value by default???
A banker has a seif with a cipher. Not to forget the cipher, he wants to write it coded as following: each digit to be replaced with the difference of 9 with the current digit. The banker chose a cipher. Decipher it knowing the cipher starts with a digit different than 9. I need to write a program that takes the cipher from the keyboard and prints the new cipher. I thought of the following: Take the input from the keyboard and put it into a string or an array. Go through the object with a for and for each digit other than the first, substract it from 9 and add it to another variable. Print the new variable. Theoretically I thought of it but I don't know much C. Could you give me any kind of hint, whether I am on the right track or not?
What is the deal on sprintf_s return value?
How to convert decimal to binary in C using recursion??
design and implement a program that reads floating-points numbers in a sentinel-controlled loop until the user terminates the program by entering zero.your program should determinate and print the smallest,largest and average of the supplied numbers.
Write a program in c to replace any vowel in a string with z?
An integer that indentifies the position of a data item in a sequence of data items a) value b) number c) index d) all of the above
write a program in c language to print your bio-data on the screen by using functions.
6 Answers College School Exams Tests, IBM,
Was 2000 a leap year?