PASCAL PROGRAMMING

Tools

To write your code you can use any of the following platform

  • IDE: Lazarus, Delphi (Download and install on your computer)
  • Internet: Type Pascal Online click https://www.onlinegdb.com
  • Mobile App: Visit Play Store, download and install DCoder (There are other mobile app you can use also)


Application of selection statement on pascal program for a user to select his/her gender


phase 1: Accept input from the user without considering repetition

program Hello;
  Var
        gender :  integer;
        
Begin
   
  writeln ('Your are welcome');
  writeln ('Enter 1/2 to display your gender:: ');
  writeln ('1 Male');
  writeln ('2 Female');
  readln(gender);
  
  //test
  if gender = 1 then writeln('Male')
  else writeln('Female');

end.


Application of selection statement and loop statement on pascal program for a user to select his/her gender


phase 2: Accept input from the user and considering repetition

program Hello;

  Var

        gender :  integer;        

Begin

   writeln ('Your are welcome');

  writeln ('1 Male');

  writeln ('2 Female');

  readln(gender);  

  repeat

           writeln ('Enter value from (1-2) to display your gender:: ');

           readln (gender);

           if (gender > 0) and (gender < 1) then writeln ('Re-enter number');

        //aliter

          // if (gender < 0) and (gender > 2) then writeln ('Re-enter number');

 until (gender >= 1) and (gender <= 2);

    //test

  if gender = 1 then writeln('Male')

  else writeln('Female');

end.


pascal program to guess a number


In under to grasp this program, you need to be familiar with the following constructs:

- Selection / Decision / Conditional statement like if...else if...else 

- Loop / Iteration statement like repeat

- Pascal inbuilt function like random()


{

    Basically a random number is generated and the user must guess the number

within 5 chances.

}


program GuessNoProgram;

  Var

        guess, randno, xcount : integer;        

Begin

   writeln ('For this game you need to guess the correct number within 6 tries');

   //randomize;

   randno := random(10);

   xcount := 0;

   repeat

       repeat

          begin

           write ('Enter your guess from 0 to 9:: ');

           readln (guess);

           if (guess < 0) and (guess > 9) then writeln ('Illegal number,re-enter');

          end;

       until (guess >= 0) and (guess <= 9);

      xcount := xcount + 1;   //count += 1

      if guess = randno then 

        writeln ('Your guess is correct')

      else if guess > randno then 

        writeln ('Your guess is too large')

      else if guess < randno then

        writeln ('Your guess is too small');

   until (guess = randno) or (xcount = 6);

end.

Comments

Popular posts from this blog