0

hello every1
i made a factorial program and the only thing i need to do is to validate:
Whether it is empty or it contains letters
Here's the code:

program factorial;
uses wincrt;

var
   counter:integer;
   number:integer;
   total:longint;

Begin
     write('Enter your number ');
     readln(number);
     total:=1;
     counter:=1;
     while (counter<=number) do
           Begin
                total:=(total*counter);
                counter:=(counter+1)
           end;
     Writeln('Factorial=',total);
end.

any help will be apreciated. Thanks

3
Contributors
6
Replies
7
Views
8 Years
Discussion Span
Last Post by mediastar
1

simple I/O checking... :D

program factorial;
uses wincrt;

var
   counter:integer;
   number:integer;
   total:longint;

Begin
     write('Enter your number ');
     {$I-}   {checking off}
     readln(number);
     {$I+}   {checking on}
     {if the IoResult is not zero then alert the user}
     If (IoResult <> 0)Then Begin
        Write('Wrong char,please just numbers!');
        ReadLn;
     End
     Else Begin
        total:=1;
        counter:=1;
        while (counter<=number) do
              Begin
                   total:=(total*counter);
                   counter:=(counter+1)
              end;
        Writeln('Factorial=',total);
     End;
     ReadLn;
end.
Votes + Comments
Looking good
0

a part not working
when i enter a letter, it displays Wrong char please enter a number! (that's great), but when i enter a number, the program stop working (inactive)

0

repeat..until loop?

program factorial;
uses wincrt;

var
   counter:integer;
   number:integer;
   total:longint;

Begin
     Repeat
        write('Enter your number: ');
        {$I-}   {checking off}
        readln(number);
        {$I+}   {checking on}
        total:=1;
        counter:=1;
        while (counter<=number) do
              Begin
                   total:=(total*counter);
                   counter:=(counter+1);
                   Write(total,',');
              end;
     WriteLn;
     Until IoResult<>0;
     ReadLn;
end.
0

its not working
when a letter is entered, an error occur and program terminate. The first one was better, just need to make it work the the message
WRONG CHAR, PLEASE JUST NUMBER! thats all

0

We need a condition,understand it please.Cause it will a neverending loop.... :'(

program factorial;
uses wincrt;

var
   counter:integer;
   number:integer;
   total:longint;
   n:Byte;

Begin
     n:=0;
     Repeat
        write('Enter your number: ');
        {$I-}   {checking off}
        readln(number);
        {$I+}   {checking on}
        If IoResult <> 0 Then Write('Wrong char,enter number please')
        Else
        total:=1;
        counter:=1;
        while (counter<=number) do
              Begin
                   total:=(total*counter);
                   counter:=(counter+1);
                   Write(total,',');
              end;
     WriteLn;
     n:=n+1;
     Until n=5;
     ReadLn;
end.
0

I think a TMaskEdit control in Delphi can easily solve your problem if your program is allowed to run as a standard Windows application.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.