Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Delphi
  4. This output makes no sense in my program

This output makes no sense in my program

Scheduled Pinned Locked Moved Delphi
2 Posts 1 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 12941702
    wrote on last edited by
    #1

    I'm doing a program that computes the sum and product of the integers from 1 to 5. Weird thing is when I run the program I get the correct answer for the sum. The sum says 15. Correct. But I get the wrong answer for product. I get 6. I cannot figure out where I'm going wrong:

    // This program computes the sum and product of the integers from 1 to 5

    program SumAndProduct;

    uses
    System.SysUtils;

    var
    i : integer;
    sum : integer;
    product : integer;

    begin
    sum := 0;
    product := 1;

    begin
    for i := 1 to 5 do
    sum := sum + i;
    product := product * i;
    end;

    WriteLn('The sum is ' + IntToStr(sum));
    WriteLn('The product is ' + IntToStr(product));

    WriteLn;
    WriteLn('Press Enter to quit...');
    ReadLn;
    end.

    U 1 Reply Last reply
    0
    • U User 12941702

      I'm doing a program that computes the sum and product of the integers from 1 to 5. Weird thing is when I run the program I get the correct answer for the sum. The sum says 15. Correct. But I get the wrong answer for product. I get 6. I cannot figure out where I'm going wrong:

      // This program computes the sum and product of the integers from 1 to 5

      program SumAndProduct;

      uses
      System.SysUtils;

      var
      i : integer;
      sum : integer;
      product : integer;

      begin
      sum := 0;
      product := 1;

      begin
      for i := 1 to 5 do
      sum := sum + i;
      product := product * i;
      end;

      WriteLn('The sum is ' + IntToStr(sum));
      WriteLn('The product is ' + IntToStr(product));

      WriteLn;
      WriteLn('Press Enter to quit...');
      ReadLn;
      end.

      U Offline
      U Offline
      User 12941702
      wrote on last edited by
      #2

      When I comment out the sum := sum + i line then my program does say product is 120. I'm still trying to figure this out though. Okay I figured it out. I needed to include another begin and end. I guess those are like nested parentheses.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups