Question:
Could you help me with this Programming problem?
Ghazi C
2011-05-22 12:35:45 UTC
The two integers 21 and 36 have the following property: their product does not change when we permute the digits of each of two integers.
in fact: 21 * 36 = 12 * 63 = 756

Work required:

Write a Pascal program to display all the pairs of integers (a, b) of two digits each, with the property described above.

An algorithm would also be greatly appreciated.
Three answers:
mystic smeg
2011-05-23 02:28:08 UTC
Hi there,



Unfortunately, Java or C++ and Pascal are totally different languages, so you will not be able to compile Java or C++ source code in a Pascal compiler. That said, please find a working Pascal version of the program you describe below, with pseudo code algorithm. Code has been compiled and tested using Turbo Pascal 7, let me know (mystic.smeg@yahoo.co.uk) if you need this for a different Pascal compiler (FPC, etc.).



Algorithm:

Start loop (a) from 10..99 (all numbers with two digits)

Start loop (b) from (a+1..99)

Get product of a * b

Invert a and b

compare products of a * b

store if same

next b

next a





---- listing ----

program permute;



var a,b,c: Integer; {forward a,b and product}

d,e,f: Integer; {backward a,b and product}



function InvertNumber(Value: Integer; var Res: Integer): Boolean;

var s,o: string;

    c: integer;

begin

  o:=''; Str(Value,S);

  for c:=Length(s) downto 1 do

    o:=o+s[c];

  Val(o,Res,c);

  InvertNumber:=(c=0);

end; {InvertNumber}



begin

  for a:=10 to 99 do

    for b:=a+1 to 99 do

    begin

      c:=(a*b);

      if not InvertNumber(a,d) then

        WriteLn('failed to invert number ',a);

      if not InvertNumber(b,e) then

        WriteLn('failed to invert number ',b);

      f:=(d*e);

      if c=f then

        WriteLn('(',a,' * ',b,')=(',d,' * ',e,')=',f);

    end; {for}

end.
Sam0316
2011-05-23 01:22:37 UTC
Well, here is the code in Java.





public class Permutable {



//Returns true if the two numbers have the same product when the numbers are reversed and multplied.

public boolean permutable (int a, int b) {

if (a * b == reversed (a) * reversed (b))

return true;

return false;

}



//Returns the values of integers with the digits reversed.

public int reversed (int a) {

return ((a%10) * 10) + (a/10);

}



//Runs the program checking every two digit pair.

public static void main (String[] args) {

for (int i = 10; i < 100; i ++) {

for (int j = 10; j < 100; j++) {

if (permutable (i,j)) {

System.out.println (i + " and " + j + " or " + reversed (i) + " and " + reversed(j));

}

}

}

}

}
NecroBox
2011-05-22 19:49:43 UTC
I could do this in C but I don't know anything about pascal. The way I would do it is to convert the integers to strings and switch the numbers around, then convert them back to integers to do the test. I know that's probably the hard way.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...