Thursday, October 1, 2015

Letters Patterns in Java

A simple program that I wrote that will create a letter patterns in Java using for loop statements.

If you  have some questions please send me an email at jake.r.pomperada@gmail.com and jakerpomperada@yahoo.com. My mobile number here in the Philippines is 09173084360.


Program Listing


// pattern10.java
// Written By: Mr. Jake R. Pomperada, BSCS, MAED-IT
// August 1, 2015   Saturday
// Create a Java Program to print the following shape
//  AAAAA
//  BBBBB
//  CCCCC
//  DDDDD
//  EEEEE

import java.lang.*;

public class pattern10 {

    public static void main(String[] args) {
           for (char i='A'; i<='E'; i++) {
            for (char j='A'; j<='E'; j++) {
              System.out.print(i);
            }
           System.out.println();
       }
       System.out.println();
      System.out.print("\t THANK YOU FOR USING THIS PROGRAM");
      System.out.println("\n\n");
   }
}

Payroll System Using Structure in C++

I this article I would like to share with you guys a sample program that I wrote a long time ago in my C++ programming class in college using structure as our data structure. I called this program payroll system that will compute the salary of an employee in the company. To give you some idea structure is also known as records in other programming language such as Pascal it can contains several variables and different data type but still belong with the same entity. 

If you  have some questions please send me an email at jake.r.pomperada@gmail.com and jakerpomperada@yahoo.com. My mobile number here in the Philippines is 09173084360.



Program Listing


#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

struct employee {
    string name, department;
    int age;
    char gender;
    float days, rate;
};

main() {

    float solve=0.00;
    string sex;
    employee user;

     cout << "\t\t XYZ Payroll Solver 1.0";
     cout << "\n\n";
     cout << "\t Created By: Mr. Jake R. Pomperada, MAED-IT";
     cout << "\n\n";
     cout << "Enter the Name of the Employee    : ";
     getline(cin,user.name);
     cout << "Enter the Name of Department      : ";
     getline(cin,user.department);
     cout << "Enter the Gender of the Employee  : ";
     user.gender = getchar();
     cout << "Enter the Number of Days Worked   :  ";
     cin >> user.days;
     cout << "Enter the Daily Rate              :  ";
     cin >> user.rate;

     solve = (user.days * user.rate);
     if (user.gender == 'M' || user.gender == 'm')
        {
          sex = "Male";
        }
     else if (user.gender == 'F' || user.gender == 'f')
        {
          sex = "Female";
        }
    cout << fixed;
    cout << setprecision(2);
     cout << "\n\n";
     cout << "\t ===== GENERATED REPORT =====";
     cout << "\n\n";
     cout << "\nEmployee Name : " << user.name;
     cout << "\nDeparment     : " << user.department;
     cout << "\nGender        : " << sex;
     cout << "\nSalary        : $" << solve;
     cout << "\n\n";
      system("pause");
}

Friday, September 25, 2015

Prime Number Checker in JavaScript

A simple program that I wrote to check if the number given by the user is a prime number or not. I also added a functionality that will check if the given value is an invalid entry like letters or special characters.

If you  have some questions please send me an email at jake.r.pomperada@gmail.com and jakerpomperada@yahoo.com. My mobile number here in the Philippines is 09173084360.






Sample Program Output


Program Listing

<html>
<head>
<style>
body {
  background-color:lightgreen;
  font-family:arial;
  font-size:25px;
  color:blue;
  }
  label {
    float: left;
    width: 30%;
    text-align: left;
    margin-right: 1em;

    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    -o-text-overflow: ellipsis;
}
.form-field-no-caption {
    margin-le

input[type="radio"]{
 float:left;
}
</style>
<meta charset="UTF-8">
<title>Prime Number Checker in JavaScript</title>

 <script type="text/javascript">

        function check_prime() {
        var input = document.getElementById("userInput").value;
              var number = parseInt(input);
                    
            if (isNaN(number)) {
                alert("Please enter a number.");
                document.getElementById("userInput").value="";
                document.getElementById("result").innerHTML =  "";
                document.getElementById("userInput").focus();
                
            }
                     
            else if (input.length === 0) {
                alert("Please enter a valid input");
                document.getElementById("userInput").focus();
               
            }
           
           else if (!isNaN(number)) {
                            if (isPrime(number)) {
                                document.getElementById("result").innerHTML = number + " is a PRIME number.";
                            }
                            else {
                                document.getElementById("result").innerHTML = number + " is NOT a PRIME number.";
                            }
                        }
                        else {
                            document.getElementById("result").innerHTML = "Please enter a number.";
                        }
                    }
                   
                   function isPrime(n_value) {
                      
                        if (n_value < 2) {
                            return false
                           }
                        if (n_value != Math.round(n_value)) {
                           return false
                           }
                       
                        var isPrime = true;

                       for (var i = 2; i <= Math.sqrt(n_value); i++) {
                            if (n_value % i == 0) {
                            isPrime = false
                            }
                        }

                       return isPrime;       

                      }
        
        function clear_textbox(){
          document.getElementById("result").innerHTML =  "";
          document.getElementById("userInput").value="";
          document.getElementById("userInput").focus();
         
        }
    </script>
</head>
<body>
<h2> Prime Number Checker in JavaScript </h2>
  Enter a Number 
    <input type="text" id="userInput" autofocus value = "" />
    <input type="submit" value="Check" 
    title="Click here to check if the given number is an prime number or not."
    onclick="check_prime()" />
    <input type="submit" value="Clear" 
     title="Click here to clear the text box."
    onclick="clear_textbox()" />
<br><br>
  <p id="result"></h1>
</body>
</html>



Perfect Number Checker in JavaScript

This program if the given number value by the user is perfect number or not. I added a function for checking invalid entry values.

If you  have some questions please send me an email at jake.r.pomperada@gmail.com and jakerpomperada@yahoo.com. My mobile number here in the Philippines is 09173084360.






Sample Program Output

Program Listing

<html>
<head>
<style>
body {
  background-color:lightgreen;
  font-family:arial;
  font-size:25px;
  color:blue;
  }
  label {
    float: left;
    width: 30%;
    text-align: left;
    margin-right: 1em;

    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    -o-text-overflow: ellipsis;
}
.form-field-no-caption {
    margin-le

input[type="radio"]{
 float:left;
}
</style>
<meta charset="UTF-8">
<title>Prime Number Checker in JavaScript</title>

 <script type="text/javascript">

        function check_prime() {
        var input = document.getElementById("userInput").value;
              var number = parseInt(input);
                    
            if (isNaN(number)) {
                alert("Please enter a number.");
                document.getElementById("userInput").value="";
                document.getElementById("result").innerHTML =  "";
                document.getElementById("userInput").focus();
                
            }
                     
            else if (input.length === 0) {
                alert("Please enter a valid input");
                document.getElementById("userInput").focus();
               
            }
           
           else if (!isNaN(number)) {
                            if (is_perfect(number)) {
                                document.getElementById("result").innerHTML = number + " is a PERFECT number.";
                            }
                            else {
                                document.getElementById("result").innerHTML = number + " is NOT a PERFECT number.";
                            }
                        }
                        else {
                            document.getElementById("result").innerHTML = "Please enter a number.";
                        }
                    }
                   
                   
        function is_perfect(number)  
        {  
        var temp = 0;  
           for(var i=1;i<=number/2;i++)  
             {  
                 if(number%i === 0)  
                  {  
                    temp += i;  
                  }  
             }  
             
             if(temp === number)  
                {  
                return true;  
                }   
             else  
                {  
               return false;
                }     
         }   
        
        function clear_textbox(){
          document.getElementById("result").innerHTML =  "";
          document.getElementById("userInput").value="";
          document.getElementById("userInput").focus();
         
        }
    </script>
</head>
<body>
<h2> Perfect Number Checker in JavaScript </h2>
  Enter a Number 
    <input type="text" id="userInput" autofocus value = "" />
    <input type="submit" value="Check" 
    title="Click here to check if the given number is an perfect number or not."
    onclick="check_prime()" />
    <input type="submit" value="Clear" 
     title="Click here to clear the text box."
    onclick="clear_textbox()" />
<br><br>
  <p id="result"></h1>
</body>
</html>





Count Words in a Sentence in JavaScript

This program will allow the user to enter a sentence and then the program will count how many words in a given sentence by the user.

If you  have some questions please send me an email at jake.r.pomperada@gmail.com and jakerpomperada@yahoo.com. My mobile number here in the Philippines is 09173084360.




Sample Program Output


Program Listing

<html>
<head>
<style>
body {
  background-color:lightgreen;
  font-family:arial;
  font-size:25px;
  color:blue;
  }
  label {
    float: left;
    width: 30%;
    text-align: left;
    margin-right: 1em;

    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    -o-text-overflow: ellipsis;
}
.form-field-no-caption {
    margin-le

input[type="radio"]{
 float:left;
}
</style>
<meta charset="UTF-8">
<title> Count Words in a Sentence </title>

 <script type="text/javascript">

        function count_words() {
        var input = document.getElementById("userInput").value;
              var number = parseInt(input);
                
                 count_word=input.split(" ")
                 document.getElementById("result").innerHTML =
                 "Number of words in a given sentence is " + 
                 count_word.length + ".";
                 }
        
        function clear_textbox(){
          document.getElementById("result").innerHTML =  "";
          document.getElementById("userInput").value="";
          document.getElementById("userInput").focus();
         
        }
    </script>
</head>
<body>
<h2> Count Words in a Sentence </h2>
Enter a sentence
  <br>
    <textarea cols="40" id="userInput" autofocus></textarea>
    <br><br>
    <input type="submit" value="Check" 
    title="Click here to count the number of words in a sentence."
    onclick="count_words()" /> &nbsp; &nbsp; 
    <input type="submit" value="Clear" 
     title="Click here to clear the text box."
    onclick="clear_textbox()" />
<br><br><br>
  <p id="result"></h1>
</body>
</html>




Leap Year Checker in Javascript

This sample program that I wrote using JavaScript as my programming language will check if the given year by the user is a leap year or not. Leap year occurs every four year just like we having our Olympic games held. I added a function to clear the text field and checking for invalid entry values. The code is very simple and easy to understand by people that are new in JavaScript programming.

If you  have some questions please send me an email at jake.r.pomperada@gmail.com and jakerpomperada@yahoo.com. My mobile number here in the Philippines is 09173084360.







Sample Program Output


Program Listing

<html>
<head>
<style>
body {
  background-color:lightgreen;
  font-family:arial;
  font-size:20px;
  color:blue;
  }
  label {
    float: left;
    width: 30%;
    text-align: left;
    margin-right: 1em;

    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    -o-text-overflow: ellipsis;
}
.form-field-no-caption {
    margin-le

input[type="radio"]{
 float:left;
}
</style>
<meta charset="UTF-8">
<title> Leap Year Checker in JavaScript </title>

 <script type="text/javascript">

        function count_words() {
        var input = document.getElementById("userInput").value;
              var year  = parseInt(input);
               
               if (isNaN(year)) {
                  alert("Enter a valid year");
                   document.getElementById("result").innerHTML =  "";
                   document.getElementById("userInput").value="";
                   document.getElementById("userInput").focus();
                   }
                 else  if  ((year & 3) == 0 && ((year % 25) != 0 || (year & 15) == 0))
                {
                document.getElementById("result").innerHTML = 
                "The year " + year + " is a LEAP year";
                }
                else {
                  document.getElementById("result").innerHTML = 
                "The year " + year + " is NOT A LEAP year";
                 }
                
                 }
        
        function clear_textbox(){
          document.getElementById("result").innerHTML =  "";
          document.getElementById("userInput").value="";
          document.getElementById("userInput").focus();
         
        }
    </script>
</head>
<body>
<h2> Leap Year Checker in Javascript </h2>
Enter desired year
  <br>
    <input type="text"  id="userInput" size="4" maxlength="4" autofocus>
    <br><br>
    <input type="submit" value="Check" 
    title="Click here to check for leap year."
    onclick="count_words()" /> &nbsp; &nbsp; 
    <input type="submit" value="Clear" 
     title="Click here to clear the text box."
    onclick="clear_textbox()" />
<br><br><br>
  <p id="result"></h1>
</body>
</html>