Tuesday 26 November 2013

JAVASCRIPT : Factorial of a Number

JavaScript : Factorial of a Number :

Here is the code for the Factorial of a number in JavaScript code :

<script type = "text/javascript">
            var n = parseInt(window.prompt("Enter Number To Find Its Factorial:"));
            var result = fact(n);
            window.alert("Factorial of the given number " + result);
            function fact(n)
            {
                if(n == 0)
                    return 1;
                else 
                    return (n*fact(n-1));
            }
</script>

Output :

Enter Number To Find Its Factorial: 3
3 ! =  6 

PYTHON : Factorial of a Number

Factorial of a Number :

Here is the code for the Factorial of a number in Python code :


def Fact(n):
   if( n <= 1):
        return 1
   return n * Fact(n - 1)


val = 0
print " Enter Number To Find Its Factorial: ",
val = input()
print val, " !=", Fact(val)

Output :

Enter Number To Find Its Factorial:  3
3 ! =  6    

C++ : Factorial of a Number

Factorial of a Number :

Here is the code for the Factorial of a number in C++ code :

#include<iostream>
using namespace std;
int main()
{
    int num,factorial=1;
    cout<<" Enter Number To Find Its Factorial:  ";
    cin>>num;
    for(int a=1;a<=num;a++)
    {
        factorial=factorial*a;
    }
cout<<"Factorial of Given Number is ="<<factorial<<endl;
    return 0;
}
  

Output :

Enter Number To Find Its Factorial: 3

0! = 1
1! = 1
2! = 2
3! = 6


Friday 22 November 2013

ASSEMBLY LANGUAGE : Fibonacci Series


Fibonacci Series :

Here is the code for the Fibonacci Series in the Assembly Language program :

2000  MVI D,07 H
2002  MVI B,00 H
2004  MVI C,01 H
2006  MOV A,B
2007  ADD C
2008  MOV B,C
2009  MOV C,A
200A DCR D
200B JNZ 2006
200E END


Output :

1
1
2
3
5
8
13

To know about the Fibonacci Series in detail and also to know the codes of the Fibonacci Series in various other programming languages Click Here

If the above link doesn't work go to this URL :

PERL : Fibonacci Series


Fibonacci Series :

The below code explains the Fibonacci series in PERL programming language :

use warnings;
use strict;

my $f1=0;
my $f2=1;
my ( $n, $i, $f3 ) ;

print "\n*** FIBONACCI SERIES ***\n";
chomp ( $n = <STDIN> ) ;

printf ("$f1\t$f2") ;

for ( $i = 0 ; $i<$n-2 ; $i++ )
{
         $f3 = $f1+$f2 ;
         $f1 = $f2 ;
         $f2 = $f3 ;
         printf ("\t$f3");
}

Output :

1
1
2
3
5
8
13

To know about the Fibonacci Series in detail and also to know the codes of the Fibonacci Series in various other programming languages Click Here

If the above link doesn't work go to this URL :

PHP : Fibonacci Code

Fibonacci Series :

$a = 0;
$b = 1;
$term = 7;
$i = 0;

echo $a." ".$b." ";
for($i; $i < $term -1 ; $i++)
{
$c = $b +$a;
echo $c." ";
$a = $b;
$b = $c;
}

Output :

1
1
2
3
5
8
13

To know about the Fibonacci Series in detail and also to know the codes of the Fibonacci Series in various other programming languages Click Here

If the above link doesn't work go to this URL :

Thursday 21 November 2013

Fibonacci Series


About the Fibonacci Series :

The Fibonacci series of numbers are found by adding the two numbers before it.

         0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...

We can compute Fibonacci numbers with recursion. This can be a bit slow. It is also possible to use iteration in computing Fibonacci Series.To compute a Fibonacci number at a certain position N, we have to loop through all previous numbers starting at position 0.

Logic :                                                                                                                                                        
prevnextsum
shifted to prevshifted to next
112
123
235
358
5813
813...
13......

Applications :

  1. Financial Markets
  2. Natural Phenomena
  3. Music , etc .,

Fibonacci Codes in various programming languages :

RUBY : Fibonacci Series

Fibonacci Series :

Here is the Fibonacci Series for the Ruby code :

def  fibonacci(n)
    n1,n2=0,1
    while n1 <=n
         print “#{n1}\n”
        n1,n2=n2,n1+n2
    end
end
fibonacci(7)

Output :

1
1
2
3
5
8
13


To know about the Fibonacci Series in detail and also to know the codes of the Fibonacci Series in various other programming languages Click Here

If the above link doesn't work go to this URL :
http://gappcode.blogspot.in/2013/11/fibonacci-series.html

JAVASCRIPT : Fibonacci Series

Fibonacci Series :


The following code describes about the Fibonacci series implemented in JavaScript with the JavaScript code implemented inside the HTML Tags :

<html>
<body>
<script type="text/javascript">
var a=0,b=1,c;
document.write("Fibonacci");
while (b<=7)
{
c=a+b;
a=b;
b=c;
document.write(c);
document.write("<br/>");
}
</script>
</body>
</html>

Output :

1
1
2
3
5
8
13


To know about the Fibonacci Series in detail and also to know the codes of the Fibonacci Series in various other programming languages Click Here

If the above link doesn't work go to this URL :
http://gappcode.blogspot.in/2013/11/fibonacci-series.html

JAVA : Fibonacci Series

Fibnacci Series :

Here is the Java code for the Fibonacci Series :
class Fibonacci
{
 public static void main(String args[])
 {  
  int prev, next, sum, n;
  prev=next=1
  for(n=1;n<=7;n++)
  {
   System.out.println(prev);
   sum=prev+next;
   prev=next;
   next=sum;
  }
 }
}

Output :

1
1
2
3
5
8
13


To know about the Fibonacci Series in detail and also to know the codes of the Fibonacci Series in various other programming languages Click Here

If the above link doesn't work go to this URL :
http://gappcode.blogspot.in/2013/11/fibonacci-series.html

PYTHON : Fibonacci Series

FIBONACCI SERIES :


Here is the python code for the Fibonacci series :

# Defining Functions
def fib(n):    # write Fibonacci series up to n
    """Print a Fibonacci series up to n."""
    a, b = 0, 1
    while b < n:
        print b,
        a, b = b, a+b
 
# Now call the function we just defined:
fib(7)

Output :

1 1 2 3 5 8 13
To know about the Fibonacci Series in detail and also to know the codes of the Fibonacci Series in various other programming languages Click Here

If the above link doesn't work go to this URL :
http://gappcode.blogspot.in/2013/11/fibonacci-series.html


Wednesday 20 November 2013

C# : Fibonacci Code

The below code gives the Fibonacci series of numbers in C# for n number of terms :
using System;
class Program
{
    public static int Fibonacci(int n)
    {
 int a = 0;
 int b = 1;
 // In N steps compute Fibonacci sequence iteratively.
 for (int i = 0; i < n; i++)
 {
     int temp = a;
     a = b;
     b = temp + b;
 }
 return a;
    }

    static void Main()
    {
 for (int i = 0; i < 15; i++)
 {
     Console.WriteLine(Fibonacci(i));
 }
    }
}

Output :

1 1 2 3 5 8 13

To know about the Fibonacci Series in detail and also to know the codes of the Fibonacci Series in various other programming languages Click Here
If the above link doesn't work go to this URL :
http://gappcode.blogspot.in/2013/11/fibonacci-series.html

C++ : Fibonacci Series


The below code finds the Fibonacci series for the entered number of terms :  

#include<iostream>
using namespace std;
main()
{
   int n, c, a = 0, b = 1, temp;
   cout << "Enter the number of terms" << endl;
   cin >> n;
   cout << "The Fibonacci series are :- " << endl;
   for ( c = 1 ; c < n ; c++ )
   {
      if ( c <= 1 )
         temp = c;
      else
      {
         temp = a + b;
         a = b;
         b = temp;
      }
      cout << temp << endl;
   }
   return 0;
}

Input :

Enter the number of terms : 7

Output :

f = 
f = 
1 1 
f =
1 1 2 
f =
1 1 2 3 
f =
1 1 2 3 5 
f =
1 1 2 3 5 8
f =
1 1 2 3 5 8 13

To know about the Fibonacci Series in detail and also to know the codes of the Fibonacci Series in various other programming languages  Click Here

If the above link doesn't work go to this URL :
http://gappcode.blogspot.in/2013/11/fibonacci-series.html

Tuesday 19 November 2013

MATLAB : PALINDROME


The following code demonstrates the 

% We iterate five times
for i = 1 : 5
    % Let's ask the user to enter digits one-by-one
    str = ['Give me digit number ' num2str(i) ': '];
    s(i) = input(str,'s');
    % and we reverse the digits, starting by the end
    p(6 - i) = s(i);
end
 
% Compare the entered string with the reversed one
if s == p
    % and answer accordingly
    disp ('WOW! Those digits happen to be a palindrome!')
else
    disp ('They are NOT a palindrome, sorry...')
end 

MATLAB CODE : Factorial of a Number

Read the following books to get the basic knowledge in MATLAB


FACTORIAL OF A NUMBER :
The below code computes the factorial of a number in MATLAB :


function y = fact(n)
% The function to calculate the factorial of a number
y = n

if n == 0
    y = 1

else
    % All the integers are multiplied one at a time
    y = y * fact(n-1)
end 

The input number should be given as an argument to the function fact()

Output :

0! = 1
1! = 1
2! = 2
3! = 6

MATLAB : Prime Number or Not

Read these books to get more advanced knowledge in MATLAB


The following code gives whether the number entered is a prime number or not :

x = input ('Input a number: ')
p=1;

for n = 2:sqrt(x)
   if x~=n && mod (x, n) == 0
    p=0;
   break
   end
end

if p == 0
    sprintf (' The input %d is NOT a prime number %d ', x)
else
    sprintf ('Input %d is a prime number', x)
end

Examples:
>> prime2
Input a number: 199

x =

   199


ans =

Input 199 is a prime number

>> prime2
Input a number: 1234567

x =

     1234567


ans =

 The input 1234567 is NOT a prime number 

Friday 8 November 2013

MATLAB BASIC FUNDAMENTALS - PART 3

Read the following books to get the basic knowledge in MATLAB

MATLAB CLASSES :


NUMERIC CLASSES :

Integer Classes :



Double-Precision Floating Point :



Single-Precision Floating Point :


LOGICAL CLASS :

Function commands that identify logical arrays:


Functions that return a logical result :


CHARACTERS AND STRINGS :

Functions to identify characters in a string :








Wednesday 6 November 2013

MATLAB BASIC FUNDAMENTALS - PART2


Array Operations :

Samples :
Addition of two vectors 

A=[1 1 1] ; B=1:3

A+B

Ans = [2 3 4]

Scalar  and Matrix Multiplication :

A=[1 0 2 ; 3 1 4]

3.*A

ans = [
3 0 6 ; 9 3 12]

Matrix Operations :

Samples :

Product of two matrices :
A = [1 3 ; 2 4]

B = [3 0 ; 1 5]

A*B

Ans = [
6 15 ; 10 20]



Element-wise product of matrices :


A.*B

ans = [
3 0 ; 2 20]



For array multiplication 

Relational Operators :

Samples :

A = [2 7 6 ; 9 0 5 ; 3 0.5 6] ;
B = [8 7 0 ; 3 2 5 ; 4 -1 7] ;

A==B

Ans = [ 0 1 0 ; 0 0 1 ; 0 0 0]

Short-Circuit Operators :

Samples :



To avoid attempting divide by zero and  

x = (b ~= 0) && (a/b > 18.5)

Operator Precedence :

A=[ 3 9 5 ] ;

B=[ 2 1 5 ] ;

C = (A./B).^2

C= [ 2.2500  81.0000  1.0000 ]

MATLAB Special Values :

Samples :

x=2*pi
x =
6.2832


MATLAB BASIC FUNDAMENTALS- PART 1

Read the following book to get more knowledge in MATLAB.

Creating Variables :
Assigning values to variables :

x = 5.71;

A = [1 2 3; 4 5 6; 7 8 9];

Create Numeric Arrays :

Creating a single element, 1-by-1 arrays

A = 100;

Creating a matrix(a two-dimensional, rectangular array of numbers)

B = [12, 62, 93, -8, 22; 16, 2, 87, 43, 91; -4, 17, -72, 95, 6]

columns - seperated by comma , rows - seperated by semicolon

Creating a Vector ( 1-by-norn-by-1 array)

C = [1, 2, 3]

D = [10; 20; 30]

Using Ellipses (...) :

Continue Long Statements on Multiple Lines

s = 1 - 1/2 + 1/3 - 1/4 + 1/5 ...
- 1/6 + 1/7 - 1/8 + 1/9;

Calling a MATLAB Functions :

Inputs to functions enclosed in parentheses:

max(A)

Multiple inputs separated by commas:

max(A,B)

Storing output from a function to a variable:

maxA = max(A)

Enclosing multiple outputs in square brackets:

[maxA, location] = max(A)

Displaying a text:

disp('hello world')

Ignoring Functions Outputs :


Ignore the first output using a tilde (~).

[~,name,ext] = fileparts(helpFile);

Exist or Which Function 


Check whether a name is already in use with the exist or which function. 

exist checkname
ans = 0

exist returns 0 if there are no existing variables or functions

Case and Space Sensitivity :

MATLAB code is case sensitive and insensitive to blank spaces except when defining arrays.

Function Syntax :

[output1, ..., outputM] = functionName(input1, ..., inputN)

Command Syntax :

functionName input1 ... inputN

Errors in MATLAB Function Calling :

Error: <functionName> was previously used as a variable, conflicting with its use here as the name of a function or command.

where <function Name >is the name of the function.


Thursday 10 January 2013

Working with Files in Python


# work with files

#open file for write

f=open('c:/TEMP/workpy.txt','w')

print f

f.write("aaaaaaaaaaaaaaaaaaa\n")
f.write("bbbbbbbbbbbbbb");



# work with files #open file for read f=open('c:/TEMP/workpy.txt','r') # line reading s=f.readline() print s f.close()
# work with files #open file for read f=open('c:/TEMP/workpy.txt','r') # pieces reading s1=f.read(5) print s1 s2=f.read(19) print s2 s2=f.read(25) print s2 f.close()
# work with files #open file for read f=open('c:/TEMP/workpy.txt','r') # pieces reading s1=f.read(5) print s1 print f.tell() s2=f.read(19) print s2 print f.tell() s2=f.read(25) print s2 print f.tell() f.close()
# work with files # seek f=open('c:/TEMP/workpy.txt','r+') f.write('0123456789abcdef') f.seek(5) # Go to the 6th byte in the file print f.read(1) f.seek(-3, 2) # Go to the 3rd byte before the end print f.read(1)

MATLAB Fibonacci Code

Read these books to get the knowledge on MATLAB more better.

 MatLab Fibonacci Sequence :

Code :

n = input('n (number of Fibonacci numbers to compute): ');
f(1) = 1; f(2) = 1;
for i = 3:n
f(i) = f(i-1) + f(i-2)
end

Input :
n (number of Fibonacci numbers to compute): 7

Output :

f = 
1 1 2
f = 
1 1 2 3
f =
1 1 2 3 5
f =
1 1 2 3 5 8
f =
1 1 2 3 5 8 13

Here is also the code for the fibonacci series in C++ :
http://gappcode.blogspot.in/2013/11/c-fibonacci-series.html

Read these books to get the knowledge on MATLAB more better.