Pascal division without remainder - pascal

Write a program that will return 1 if m divides on n without remainder and 0 if not.
program Division;
var m, n:longint;
begin
readln(m, n);
writeln(5 mod 4);
writeln((1-(n mod m)div 2));
end.

program Division;
var n, m: integer;
begin
readln(n,m);
writeln (1 - (n mod m + 1) div 2 mod 2);
end.

Related

How to multiply odd numbers from an array?

I have a program that reads a N number of integers and push them into an array, then I need to multiply the odd numbers from the array.
Program p2;
type
tab = array[1..10] of integer;
var
a, c : tab;
n, i, prod : integer;
begin
writeln('n=');
readln(n);
prod := 1;
writeln('Enter array numbers:');
for i := 1 to n do
read(a[i]);
if (a[i] mod 2 = 1) then
prod := a[i] * prod;
writeln('The produs of the odd numbers is: ',prod);
end.
For example when you enter the n as 5, and the numbers: 1, 2, 3, 4, 5;
The result of multiplication of odd numbers should be 15. Can someone help me to fix it working properly.
First off, whitespace and indentation are not terribly significant in Pascal, but they can be your ally in understanding what your program is doing, so let's make your code easier to read.
Program p2;
type
tab = array[1..10] of integer;
var
a, c : tab;
n, i, prod : integer;
begin
writeln('n=');
readln(n);
prod := 1;
writeln('Enter array numbers:');
for i := 1 to n do
read(a[i]);
if a[i] mod 2 = 1 then
prod := a[i] * prod;
writeln('The produs of the odd numbers is: ', prod);
end.
With indentation, it should be apparent what's happening. Your conditional only runs once, rather than each time through your loop. As suggested in the comments, begin and end neatly solve this problem by creating a block where the conditional is checked each time the loop runs.
program p2;
type
tab = array[1 .. 10] of integer;
var
a, c : tab;
n, i, prod : integer;
begin
writeln('n=');
readln(n);
prod := 1;
writeln('Enter array numbers:');
for i := 1 to n do
begin
read(a[i]);
if a[i] mod 2 = 1 then
prod := a[i] * prod;
end;
writeln('The produs of the odd numbers is: ', prod);
end.

Pascal - Sum of odd numbers between 0 and X

I've beeng having some trouble with this code... I need to create an algorithm which makes the user input a number (X), and then the program calculates the sum of all the odd numbers below (x).
This what I've tried so far, but can't really wrap my head around the logic behind it:
Program odd_numbers;
Var
Num, Limite, Soma: integer;
Begin;
Soma := 0;
Writeln('Choose a limit:');
Readln(Limite);
While (Limite / 2 > 0) do
Begin;
Soma := ((Num < Limite) mod 2 > 0);
Writeln('The sum of odd numbers from 0 to ', Limite, ' é ', Soma);
End;
if (Limite mod 2 = 0) then
Begin;
Soma := ((Num < Limite) mod 2 = 0);
Writeln('The sum of odd numbers from 0 to ', Limite, ' é ', Soma);
End;
End.
*PS: Been writing the code with variables in Portuguese, so don't mind the variables appearing weird to understand. *
I see that everyone is happily looping, but this is not necessary. This is a simple arithmetic sequence, and the sum can be calculated without a loop.
Just think of the following:
1 + 3 = 2 * (1 + 3) / 2 = 2 * 2 = 4 ; limits 3 and 4
1 + 3 + 5 = 3 * (1 + 5) / 2 = 3 * 3 = 9 ; limits 5 and 6
1 + 3 + 5 + 7 = 4 * (1 + 7) / 2 = 4 * 4 = 16 ; limits 7 and 8
1 + 3 + 5 + 7 + 9 = 5 * (1 + 9) / 2 = 5 * 5 = 25 ; limits 9 and 10
1 + 3 + 5 + 7 + 9 + 11 = 6 * (1 + 11) / 2 = 6 * 6 = 36 ; limits 11 and 12
But not only that, you'll see that it is in fact always a perfect square: Sqr((n+1) div 2).
So just calculate:
program odd_numbers;
var
Num, Limite, Soma: Integer;
begin
Write('Choose a limit: ');
Readln(Limite);
Num := (Limite + 1) div 2;
Soma := Num * Num;
Writeln('The sum of odd numbers from 0 to ', Limite, ' is ', Soma);
end.
Looks a little simpler than what the others propose.
The loop While (Limite / 2 > 0) do ... uses real arithmetic and not integer arithmetic. I guess you mean While (Limite div 2 > 0) do ... And you should change Limite in the loop otherwise you get stuck because the exit condition can never be reached.
After you have asked the user to enter a number, Limite, you need to keep that unchanged, because you need it in the final message. You also need a loop where you go through all numbers from Limite towards 0.
You started with a while loop which is ok, you are just missing the loop control variable. That is a variable that eventually gets a terminating value which then stops the loop. Use for example the Num variable you already have declared. You can use the same variable to investigate the numbers between user input and 0, for being odd values.
num := limite-1; // give num a start value based on user input (-1 because of "... numbers below (x)")
while num > 0 do // stop the loop when 0 is reached
begin
// here you investigate if `num` is a odd number (e.g. using `mod` operator or
// possibly your pascal has a built in `function Odd(value: integer): boolean;`)
// and add it to `Soma` if it is
num := num - 1;// decrement num at every iteration
end;
Finally you need to consider changes to the above, to handle negative input from the user.
To test if an integer is an odd value, you could use following function:
function IsOdd( value : Integer) : Boolean;
begin
IsOdd := (value mod 2) <> 0;
end;
Many pascal compilers have a built-in function called Odd(), which you could use.
A while loop works well to solve this problem. If you start with lowest odd number above zero, i.e. one and continue upwards so long we do not exceed the limit value we have a simple start:
function GetOddSumBelowX( X : Integer) : Integer;
var
i,sum: Integer;
begin
i := 1; // Start with first odd number
sum := 0;
while (i < X) do begin // as long as i less than X, loop
if IsOdd(i) then begin
sum := sum + i; // add to sum
end;
i := i + 1; // Increment i
end;
GetOddSumBelowX := sum;
end;
Now, that was simple enough. Next step to simplify the loop is to increment the i variable by two instead, just to jump between all odd numbers:
function GetOddSumBelowX( X : Integer) : Integer;
var
i,sum: Integer;
begin
i := 1; // Start with first odd number
sum := 0;
while (i < X) do begin // as long as i less than X, loop
sum := sum + i; // add to sum
i := i + 2; // Increment to next odd number
end;
GetOddSumBelowX := sum;
end;

Optimize a perfect number check to O(sqrt(n))

Part of the program I have checks if an input number is a perfect number. We're supposed to find a solution that runs in O(sqrt(n)). The rest of my program runs in constant time, but this function is holding me back.
function Perfect(x: integer): boolean;
var
i: integer;
sum: integer=0;
begin
for i := 1 to x-1 do
if (x mod i = 0) then
sum := sum + i;
if sum = x then
exit(true)
else
exit(false);
end;
This runs in O(n) time, and I need to cut it down to O(sqrt(n)) time.
These are the options I've come up with:
(1) Find a way to make the for loop go from 1 to sqrt(x)...
(2) Find a way to check for a perfect number that doesn't use a for loop...
Any suggestions? I appreciate any hints, tips, instruction, etc. :)
You need to iterate the cycle not for i := 1 to x-1 but for i := 2 to trunc(sqrt(x)).
The highest integer divisor is x but we do not take it in into account when looking for perfect numbers. We increment sum by 1 instead (or initialize it with 1 - not 0).
The code if (x mod i = 0) then sum := sum + i; for this purpose can be converted to:
if (x mod i = 0) then
begin
sum := sum + i;
sum := sum + (x div i);
end;
And so we get the following code:
function Perfect(x: integer): boolean;
var
i: integer;
sum: integer = 1;
sqrtx: integer;
begin
sqrtx := trunc(sqrt(x));
i := 2;
while i <= sqrtx do
begin
if (x mod i = 0) then
begin
sum := sum + i;
sum := sum + (x div i) // you can also compare i and x div i
//to avoid adding the same number twice
//for example when x = 4 both 2 and 4 div 2 will be added
end;
inc(i);
end;
if sum = x then
exit(true)
else
exit(false);
end;

Divisibility of numbers in pascal

I want to write a pascal program that checks if particular number is divisible by 2, 3, 5, 7, 9 and 11 and whether the sum of the digits is even or odd. In the very end I want to write a statement like "This number is divisible by 5 and 9" and the sum of the numbers is even/odd. What should I do?
Use modulus:
program ModulusTest;
begin
if 8 mod 2 = 0 then
begin
write(8);
writeln(' is even');
end;
if 30 mod 5 = 0 then
begin
write(30);
writeln(' is divisible by 5');
end;
if 32 mod 5 <> 0 then
begin
write(32);
writeln(' is not divisible by 5');
end;
end.
Modulus is what remains after an integer division :)
This's my code, I separate into 2 sections :
program checkNumber;
var number : integer;
divider : string;
digit1, digit2, sum : integer;
begin
//First//
write('Number : '); readln(number);
if (number MOD 2 = 0) then divider := divider+'2, ';
if (number MOD 3 = 0) then divider := divider+'3, ';
if (number MOD 5 = 0) then divider := divider+'5, ';
if (number MOD 7 = 0) then divider := divider+'7, ';
if (number MOD 9 = 0) then divider := divider+'9, ';
if (number MOD 11 = 0) then divider := divider+'11, ';
write('This number is divisible by '); write(divider);
////////////////////////////////////////////////////////
//Second//
digit1 := number DIV 10;
digit2 := number MOD 10;
sum := digit1 + digit2;
write('and the sum of the numbers is ');
if (sum MOD 2 = 0) then write('even') else write('odd');
////////////////////////////////////////////////////////
end.
First part
You need MOD(modulus) operation to get the list of divider values:
write('Number : '); readln(number);
if (number MOD 2 = 0) then divider := divider+'2, ';
if (number MOD 3 = 0) then divider := divider+'3, '; //divider 2 3 5 7 9 11
.
.
Then save the divider into variable divider as string, and write it on monitor.
write('This number is divisible by '); write(divider);
Second part
You need to separate the digits into single variable using DIV(divide) and MOD(modulus) operation. In my code, I limit the number input for 2 digit (1 until 99):
digit1 := number DIV 10;
digit2 := number MOD 10;
sum := digit1 + digit2;
(You change the code use if..then.. function if you want input bigger number).
Then use MOD to check the number is even or odd:
if (sum MOD 2 = 0) then write('even') else write('odd');

Decimal to binary Free Pascal

I need a code to go from decimal to binary numbers, but my program shows them invert, example: needs to show 1011000 but it brings out 0001101.
+ I cant use massives and array in this program.
var
x,y,i:longint;
BEGIN
readln(y);
repeat
x:= y mod 2;
y:= y div 2;
write(x);
until y = 0;
END.
I think you can use recursion function. For example:
procedure dec2bin(y)
BEGIN
x := y mod 2;
y := y mod 2;
if y > 1 then
dec2bin(y)
end
write(x)
END
BEGIN
readln(y);
dec2bin(y)
END.
I'm not sure in correct syntax because I working with Pascal long time ago. But I think you can understand my idea and make this.
This should answer what you are looking for
function decimalToBinary(a:LongInt):String;
var d:Integer;
str:String;
Begin
str:='';
while a>0 do begin
d:=a mod 2;
str:=concat(IntToStr(d),str);
a:=a div 2;
end;
decimalToBinary:=str;
End;
This is my code. It works!
program convert;
var
number : integer;
procedure dec2bin(x : integer);
begin
// general case
if (x > 1) then dec2bin(x div 2);
// to print the result
if (x mod 2 = 0) then write('0')
else write('1');
end;
begin
write('Decimal: '); readln(number);
write('Binary : ');
dec2bin(number);
end.

Resources