Certainly! Here's another example of a Pascal program that calculates the sum of
integers from 1 to N:
pascal
Copy code
program SumOfIntegers;
function CalculateSum(n: Integer): Integer;
var
i, sum: Integer;
begin
sum := 0;
for i := 1 to n do
sum := sum + i;
CalculateSum := sum;
end;
var
number, result: Integer;
begin
writeln('Enter a number: ');
readln(number);
result := CalculateSum(number);
writeln('The sum of integers from 1 to ', number, ' is ', result);
end.
In this example, the program defines a function CalculateSum that takes an integer n
as an input and calculates the sum of integers from 1 to n using a for loop. The
variable sum is initialized to 0, and the loop iterates from 1 to n. In each
iteration, the value of i is added to sum.
The var section declares two variables: number to store the user input, and result
to store the sum result.
The program prompts the user to enter a number, reads the input using readln, and
then calls the CalculateSum function with the number variable. The result is stored
in the result variable.
Finally, the program prints the sum result using writeln.
When you run this program and enter a number, it will calculate and display the sum
of integers from 1 to that number.
integers from 1 to N:
pascal
Copy code
program SumOfIntegers;
function CalculateSum(n: Integer): Integer;
var
i, sum: Integer;
begin
sum := 0;
for i := 1 to n do
sum := sum + i;
CalculateSum := sum;
end;
var
number, result: Integer;
begin
writeln('Enter a number: ');
readln(number);
result := CalculateSum(number);
writeln('The sum of integers from 1 to ', number, ' is ', result);
end.
In this example, the program defines a function CalculateSum that takes an integer n
as an input and calculates the sum of integers from 1 to n using a for loop. The
variable sum is initialized to 0, and the loop iterates from 1 to n. In each
iteration, the value of i is added to sum.
The var section declares two variables: number to store the user input, and result
to store the sum result.
The program prompts the user to enter a number, reads the input using readln, and
then calls the CalculateSum function with the number variable. The result is stored
in the result variable.
Finally, the program prints the sum result using writeln.
When you run this program and enter a number, it will calculate and display the sum
of integers from 1 to that number.