Sidebar Ads

Monday, March 16, 2020

Calculator using perl script

Hello everyone, Today I will show you how to make a simple program to make a calculator using perl script,

#!/usr/bin/perl
use strict;
use warnings;
use v5.14;         #/ To use say function instead of print.
use feature "switch"; #/ To use switch function as we used in c language.
while(1)  #/To make this calculator function often working, In this way a user could use it as much as they want and when they want they could exit by pressing @, because @ is the sign which used to exit while loop as shown in below code.
{
say"(1)Press (@) to exit!!\n(2)Press (+) to add numbers!!\n(3)Press (-) to substract numbers!!\n(4)Press (/) to divide numbers!!\n(5)Press (*) to multiply numbers!!\n(6)Press (%) to find percentage of number!!\n(7)Press (^) to find power of numbers!!";
my $func=<>;       
chomp($func); #/Whenever we take user input we need to use chomp/chop function to remove new line character.
given($func)
{
when("@"){exit;} #/Here we use when as we used case in c language.
when("+"){say"Enter two numbers that you want to add!!";
          my $x=<>;
          chomp($x);
          my $y=<>;
          chomp($y);
          my $z=$x+$y;
          say"The sum is $z\n";}
when("-"){say"Enter two numbers that you want to substract!!";
          my $x=<>;
          chomp($x);
          my $y=<>;
          chomp($y);
          my $z=$x-$y;
          say"The substraction is $z\n";}
when("/"){say"Enter two numbers that you want to divide!!";
          my $x=<>;
          chomp($x);
          my $y=<>;
          chomp($y);
          if($x>$y){my $z=$x/$y;
                    say"The div is $z\n";}
           else{say"Error\n";}}
when("*"){say"Enter two numbers that you want to multiply!!";
          my $x=<>;
          chomp($x);
          my $y=<>;
          chomp($y);
          my $z=$x*$y;
          say"Mul is $z\n";}
when("%"){say"Enter number you got from total number!!";
          my $x=<>;
          chomp($x);
          say"Enter total number!!";
          my $y=<>;
          chomp($y);
          if($y>$x){my $z=$x*100.0/$y;
          say"The percent is $z%\n";}
          else{say"Error\n";}}
when("^"){say"Enter a number!!";
          my $x=<>;
          chomp($x);
          say"Enter a power of number!!";
          my $y=<>;
          chomp($y);
          my $z=$x**$y;
          say"$x^$y is equal to $z\n";
}
}
}





Thank you for visiting

0 comments:

Post a Comment