Tuesday, April 18, 2017

Sum of Digits in Turbo Pascal

Here is a sample program that will ask the user to give a series of numbers and then it will count the total sum of the number given by our user. For example the user gives 123 our program will count the total sum of 6. I wrote the code using Turbo Pascal 7 for DOS running in DOSBOX emulator because I am using 64 Bit operating system Windows 7. I hope you will find my work useful. Thank you.

My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.






Sample Program Output


Program Listing

Program sum_of_digits_pascal;

Uses Crt;

function SumOfDigitBase(n:longint;base:longint): longint;
var
  tmp: longint;
  digit,sum : longint;
Begin
  digit := 0;
  sum   := 0;
  While n > 0 do
  Begin
    tmp := n div base;
    digit := n-base*tmp;
    n := tmp;
    inc(sum,digit);
  end;
  SumOfDigitBase := sum;  
end;

Var Num,Process : Integer;
Begin
  Clrscr;
  Num := 0;
  Process := 0;
  writeln;
  Writeln;
  Write('Sum of Digits in Turbo Pascal');
  writeln;
  Writeln;
  Write('Enter a Number : ');
  Readln(Num);

  Process := SumOfDigitBase(Num,10);
  
  writeln;
  writeln('The total number of digits is  ',process,'.'); 
  Writeln;
  Write('End of Program');
  Writeln;
  Readln;

end.


No comments:

Post a Comment