Tuesday, July 28, 2009

Write a "C" function which outputs an inverted input integer ( for example it outputs 621 as 126)?

It said to write a function that handles this so I wrote this program that will call the reverse function to reverse the order of the number. Good luck!





#include %26lt;stdio.h%26gt;


#include %26lt;stdlib.h%26gt;





int reverse(int number);





int main(void)


{


int n, answer = 0;


printf("Please enter a number to be reversed: ");


scanf("%d", %26amp;n);





answer = reverse(n);





printf("The reversed format is: %d\n", answer);





exit(0);


}





int reverse(int number)


{


int x = 0;





while(number %26gt; 0)


{


x *= 10;


x += number % 10;


number /= 10;


}





return(x);


}

Write a "C" function which outputs an inverted input integer ( for example it outputs 621 as 126)?
take input in a character array.. then run a loop which take last charter first .. save it in a differnt array... athen pick upthe second last./// like wise u can go...
Reply:IF the length of the integer is not going to change (ex. always 3 digits) you can use division (divide by 100 to obtain the 6, by 10 to obtain the 2 etc.) then use the % operator. by this method you seperate the integer into single integers, which you can then output in whatever order you like, ex. inverted like in your case.
Reply:I'm not going to do your work for you... but here is the general data structure. Get each input as part of an array.. then use a stack structure to push it into a new array. Basically... you start by pushing 6 into stack A... then 2... then 1... now when they hit the enter key, you pop 1 off the stack and push it onto stack B... then pop 2 off and push it onto stack B... then pop 6 off and push into B.... display the B stack in order.
Reply:#include %26lt;stdlib.h%26gt;


#include %26lt;stdio.h%26gt;


#include %26lt;string.h%26gt;








void output_inverted_integer(int pin)


{


char buff[50];





_itoa(pin, buff,10); //put number into string format


_strrev(buff); //reverse the string


puts(buff); //print results to screen





}





//test it!


int main()


{


int a=621;





printf("Before inversion: %d \n, a);


printf("After...: ");


output_inverted_integer(a);





}
Reply:#include%26lt;stdio.h%26gt;


#include%26lt;conio.h%26gt;


main()


int i,j;


printf("Enter the number u want to be Reversed");


scanf("%d",%26amp;i);


while(i%26lt;0)


{


j=i%10;


i=i/10;


printf("%d",j);


}


Actually i am working on SAP.I forget syntax plz check it syntax.But the logic i wrote is correct

floral design

No comments:

Post a Comment