[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 441
  • Last Modified:

how you do a square and cubes table?

I need a program that will print a square and cubes table. It would like this:

Number    Square    Cube
     1          1      1

Once you get the square, couldn't you get the cube by multiplying the number *square? Or do you have to do a cube?
It should also print out the sum of the squares, the sum of the cubes, how many cubes were greater than 500, a message stating whether or not the sum of the squares exceeded 2,000.
Shouldn't it go something like this?

program SquareCubeTable;
var i, number, square, cube, sumsquare, sumcube: integer;

begin
   for i := 1 to 15 do
      begin
       square := i * i;
        cube:= number * square;
I don't know how to align it.  
     You would do the sum of the square and cubes like this?

begin
   sum:= 0;
   for i:= 1 to 15 do
   sum := i*i + i;
   writeln('sum of squares is ', sum)
      if sumsquares > 2,000 then
        writeln('Sum of squares was over 2,000 ')

begin
   sum:= 0;
    for I:= 1 to 15 do
     sum:= number * square + 1;
    writeln('sum of cubes is ', sum)

I don't how do this all in one program.
I wouldn' know about the greater than part
0
kazooie21
Asked:
kazooie21
  • 5
  • 3
  • 2
1 Solution
 
kazooie21Author Commented:
I hate when you try to post something in a table! It screws it all up! All the numbers in all three columns should be right-aligned.
0
 
dbruntonCommented:
begin
  sumsquare := 0;
  sumcube := 0;
  cubesgreater := 0;
   for i := 1 to 15 do
    begin
      square := i * i;
      cube :=  i * square;
      sumsquare := sumquare + square;
      cubesquare := cubesquare + cube;
      if (cubesquare > 500) then
        inc(cubesgreater);
    end;
end.

This will do most of it for you.  You need to print out the statements and tidy the code up.

Note that you must initialise your values before you use them.
0
 
sumant032199Commented:
This is the simplest code which I can offer.
{
  In this program f is an integer variable which is used to format the
  output. Formatting the output means much more than just allignment
  of output. The expression :f:0 tells comuter(if f=13) to
  reserve total length of 13 characters for number and 0 spaces for
  numbers after decimal point. 0 because we know that cubes and squares of integers
  are never in fractions. Try and see how different outputs come by
  changing :f:0. If values of cubes and square grows larger simply
  increase the value of f to format perfectly.

}
program square_cube;
uses crt;

var n,bigger_500,f : integer;
    cube, square, sumcube, sumsquare : real;

begin
    clrscr;
    cube := 0; square := 0;
    sumcube := 0; sumsquare := 0;
    bigger_500 := 0; f := 13;
    writeln('Number      Square        Cube');
    for n := 1 to 15 do
    begin
        square := n * n;
        cube := n * square;
        sumsquare := sumsquare + square;
        sumcube := sumcube + cube;
        if cube > 500 then bigger_500 := bigger_500 + 1;
        writeln(n:3,square:f:0,cube:f:0);
    end;
    writeln('--------------------------------');
    writeln('Total',sumsquare:f-2:0,sumcube:f:0);

    writeln;
    if sumsquare > 2000 then writeln('The sum of squares is greater than 2000.')
    else  writeln('The sum of squares is not greater then 2000.');

    writeln('The number of cubes greater than 500 = ',bigger_500);
    writeln;
    writeln('Press ENTER to continue.....');
    readln;
end.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
sumant032199Commented:
Thanks for your assessment.
0
 
kazooie21Author Commented:
It works fine, but I can't get it to print.  In fact, the table and the payroll
programs won't print either. I've used the 'uses printer' command in all of them.
0
 
dbruntonCommented:
Are you doing this under Windows 95?

If so you may have told Windows 95 that you do not print MS-DOs programs and hence Windows won't let the Pascal program print.

Solution.  Save output to a text file and then retrieve the file into a wordprocessor program,  change font to Courier and print.
0
 
kazooie21Author Commented:
I'm doing it in Turbo Pascal V7.0 for DOS.
0
 
kazooie21Author Commented:
How do you do that in DOS? Everyone else has been able to print on their computer. I've tried 3 different computers and it still won't print!
0
 
dbruntonCommented:
You need code similar to the following

uses Printer;

Write(Lst, 'This is output');

This should work.  Try it and report back.  This is straight out of the TP 5.5 manual and assumes that your printer is connected to your parallel port LPT1.
0
 
kazooie21Author Commented:
I just wasn't logged into the network. I feel sheepish!!
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 5
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now