Two simple programs. Help!

I need help with these programs. Truth is, I've never used pascal to program something.

Text:

1. Write a program which will allow you to input 3 digits number and than will calculate and write sum of that numbers.

2.Write a program which will allow you to input natural number N and than will write sum of numbers from 1 to N.

Comments

  • : I need help with these programs. Truth is, I've never used pascal to
    : program something.
    :
    : Text:
    :
    : 1. Write a program which will allow you to input 3 digits number and
    : than will calculate and write sum of that numbers.
    [blue]
    First input the number N.

    The least significant digit of N will be N mod 10. If you will not be needing N later in the program you can get the subsequent digits by dividing N by 10 using integer division which throws the remainder away, i.e,. N := N div 10.

    For example, if N = 123 then N mod 10 = 3, the least significant digit.

    Now using integer division N div 10 = 12.

    The next digit is N mod 10 = 2

    N div 10 = 1

    N mod 10 = 1

    so the three digits that need to be added together are 3,2, and 1.

    [/blue]
    :
    : 2.Write a program which will allow you to input natural number N and
    : than will write sum of numbers from 1 to N.
    :
    [blue]
    The formula for this is

    Sum := N * (N + 1) div 2
    [/blue]
  • : : I need help with these programs. Truth is, I've never used pascal to
    : : program something.
    : :
    : : Text:
    : :
    : : 1. Write a program which will allow you to input 3 digits number and
    : : than will calculate and write sum of that numbers.
    : [blue]
    : First input the number N.
    :
    : The least significant digit of N will be N mod 10. If you will not
    : be needing N later in the program you can get the subsequent digits
    : by dividing N by 10 using integer division which throws the
    : remainder away, i.e,. N := N div 10.
    :
    : For example, if N = 123 then N mod 10 = 3, the least significant
    : digit.
    :
    : Now using integer division N div 10 = 12.
    :
    : The next digit is N mod 10 = 2
    :
    : N div 10 = 1
    :
    : N mod 10 = 1
    :
    : so the three digits that need to be added together are 3,2, and 1.

    :
    : [/blue]
    : : Ok. I solved the program number 2. But I can't make program number 1 to work. Is there any chance that you post a code?
    : : 2.Write a program which will allow you to input natural number N and
    : : than will write sum of numbers from 1 to N.
    : :
    : [blue]
    : The formula for this is
    :
    : Sum := N * (N + 1) div 2
    : [/blue]
    :

  • : : : 1. Write a program which will allow you to input 3 digits number and
    : : : than will calculate and write sum of that numbers.

    : : : Ok. I solved the program number 2. But I can't make program number 1 to work. Is there any chance that you post a code?

    [code]
    IN PSEUDOCODE

    Read N
    Sum := 0
    for i = 1 .. 3
    Sum = Sum + (N mod 10)
    N = N div 10
    end for
    Write Sum
    [/code]

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories