Monday, 26 May 2014

How to swap 2 numbers using pointers.

#iclude"stdio.h"
#include"conio.h"
void main()
{
int a=4,b=8;
void swap(int *,int *); // Prototype  for the function
clrscr();
printf("\nNumbers before swap : a = % d, b = %d",a,b); // print variable value before swap
swap(&a,&b);
printf("\nNumbers after swap : a = % d, b = %d",a,b); / print variable value after swap
getch();
 }
void swap(int *p,int *q)
{
int t; // Temporary variable to store the value before swapping
t=*p;
*p=*q;
*q=t;
}

How to copy string using pointer in C Programming

#include"stdio.h"
#include"conio.h"
void main(int argc,char *argv[])
{
void stringcopy(char *,char *);
char target[10];
clrscr();
fflush(stdout);
if(argc<2)
{
printf("\nWrong number of argument parameter");
exit(0);
}
stringcopy(target,argv[1]); // target, &target[0]
printf("\nSource string %s, Target String %s",argv[1],target);
getch();
}
void stringcopy(char *trg,char *source)
{
while(*trg++=*source++);
//Alternative way pretty adhoc process
/*while(*source)
{
*trg = *source;
trg++;
source++;
}
*trg=*source;*/
}

Monday, 12 May 2014

How to create a view in Oracle

A view is a sql statement which can be stored in Oracle database as SQL statement. It never stored as data but as a sql statement.

Syntax to create a view

CREATE OR REPLACE VIEW VIEW_NAME AS
SELECT * FROM TABLE_NAME;

Syntax to see a view content

DESC VIEW_NAME

Syntax to delete a view

DROP VIEW VIEW_NAME

You can use a view as a table i.e. add where clause in view or use it in an inline view.

SELECT * FROM VIEW_NAME WHERE COLUMN_NAME = FIELD_VALUE

Example

Suppose you have a sql based on 2 table employee having 2 columns ecode, ename
and salary having 2 columns month_year, net_pay. Now the sql is to get ecode, ename,month_year
and sum of ney_pay. sql statement is given below.

select ee.ecode, ee.ename,sl.month_year, sum(sl.ney_pay) tot_net_pay
from employee ee,salary sl
where sl.emp_id = ee.emp_id
group by ee.ecode, ee.ename,sl.month_year,
order by 1,3

sl is the alias for table salary, ee is the alias for table employee, emp_id is primary key in employee
and reference key in salary. order by clause uses index of columns in select clause.

Now to create the view we write

CREATE OR REPLACE VIEW EMP_SAL_VIEW AS
select ee.ecode, ee.ename,sl.month_year, sum(sl.ney_pay) tot_net_pay
from employee ee,salary sl
where sl.emp_id = ee.emp_id
group by ee.ecode, ee.ename,sl.month_year,
order by 1,3

Now if you write a select query on this view it will be

SELECT ECODE,ENAME,MONTH_YEAR,NET_PAY FROM EMP_SAL_VIEW;

You can also use where clause in this view.

SELECT ECODE,ENAME,MONTH_YEAR,NET_PAY FROM EMP_SAL_VIEW
WHERE NET_PAY<=1000;

Sunday, 11 May 2014

How to shutdown a Windows machine from command line.

Open command prompt.

1. To simply shutdown type
     shutdown -s

2.  To forcefully shutdown the machine (Close all application forcefully before shutdown)
     shutdown -f -s

3. To restart type
    shutdown -r

4. To forcefully restart the machine (Close all application forcefully before restart)
    shutdown -f -r

5.  To set countdown before shutting down your machine.
   
     shutdown -s -t 60  (60 is value in seconds if you want to shutdown after 1 hour value will be 3600)

6. To shutdown a machine remotely from another computer.

    shutdown -s -m \\computer name or ipaddress (Be sure the machine has permission to shutdown
    remotely.

Some useful shortcut key in Windows 7 and 8

1. To open, hide or show run dialog Press Windows key + R together.

2. To select any of the taksbar pinned program Press [Window + program position in taskbar] together.
    Say your Firefox is in 4th position from the left then press Window key + 4. If the program is running and
    in minimized state or not an active window then pressing this buttons will make it active window or
   maximized if it is minimized.

3. To open search window on a particular folder. Select the folder from the explorer and press
    Windows key + F.

4. To minimize all window press Window key + D.

5. I hope you know about the second screen window, it is used if you want to extend your monitor to a
    projector,  or make a duplicate screen to another monitor etc. To open the second screen window, press
    Window key + P.

6. To minimize all screen Press Window key + M.

7. To open the explorer window press Window key + E.

8. To open the start menu press Window key, or press Control key + Escape key.


How to add Quick Launch toolbar in Windows 8

Right click on empty space in task bar.

Go to Toolbar.
Select new toolbar from Toolbar submenu.
Set this path into select folder dialog

C:\Users\[You user folder]\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch
Click on Select Folder
You are all done.

Quick launch toolbar will be visible on right corner of the taskbar just left to the datatime.

How to start and stop Oracle database service in Linux.

You can do this task from both server and client.

1. If you do it from server then

  i. Make sure you are logged in as oracle user i.e. user which Oracle is installed and user who has all the
     rights.
 ii. Open terminal
iii. type sqlplus / as sysdba
iv. You will be logged in sql prompt. In the sql prompt just type startup. This will start your Oracle database
     service.
v.  type exit to exit from sql prompt.
vi. Now type service start lsnrctl. This will start your listener service.

You are all done.

2. If you are in  a client / remote machine then you need to have putty ssh client. Log in to oracle user
    through putty and do the same steps as mentioned above.

How to start and stop Oracle database service in Windows.

Suppose your instance name of Oracle is ORA.

You will be having 2 services which need to be started.

Step 1:
You can configure Windows service to start this service at startup, if it is not started.

  i. Press window key + R to open run dialog. type services.msc and press enter.
 ii. Search for service OracleServiceOra.
iii. Right click on it and click on properties from popup menu.
iv.From the startup type select Automatic if you want to start the service when windows start, or select
   manual if you want to start the service manually, this will save you windows loading time and save memory
  also.
v. Click on start button if you want to start the service now.
vi. Click on apply.
vii/ Search for Oracle[OracleHomeName]TNSListener (Oracle home name is your oracle installed path).
viii. Do the same process as you have done with OracleServiceORA.

Step 2.

If you have set your service as mentioned above to be started manually then you can use command prompt
to start your service.

  i.  Create a batch file named say StartOracleService.bat
 ii.  In the batch file add these following line.
      @echo off
       net start OracleServiceOra
       net start Oracle[OracleHomeName]TNSListener
       REM OracleHomeName is you Oracle installed path
       exit
iii.   To stop the service create a batch file named say StopOracleService.bat
       In the file add these following line.
      @echo off
       net stop OracleServiceOra
       net stop Oracle[OracleHomeName]TNSListener
       REM OracleHomeName is you Oracle installed path
       exit


Monday, 7 April 2014

Check for prime number in Objective-C

#import <Foundation/Foundation.h>

int isPrime(int n)
{
int i;
if(n==1||n==2)
return 0;
for(i=3;i<=n/2;i++)
if(n%i==0)
return 0;
return 1;
}

int main (int argc, const char * argv[])
{
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   int i;
   for(i=1;i<=100;i++)
   {
   if(isPrime(i)==1)
    NSLog (@"%d is Prime number,",i);
   else
    NSLog (@"%d is not Prime number,",i);
   }
 [pool drain];
   return 0;
}

How to print Fibonacci series in Objective-C

#import <Foundation/Foundation.h>

int fibonacci(int n)
{
if(n<=2)
return(1);
return fibonacci(n-1) + fibonacci(n-2);
}

int main (int argc, const char * argv[])
{
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   int i;
   for(i=1;i<=8;i++)
   {
   int fib=fibonacci(i);
   NSLog (@"%d,",fib);
   }
 [pool drain];
   return 0;
}

Find Factorial of a number recursively in Objective-C

#import <Foundation/Foundation.h>

float factorial(int n)
{
if(n<=1)
return(1);
return n*factorial(n-1);
}

int main (int argc, const char * argv[])
{
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   long fact=factorial(8);
   NSLog (@"Factorial of 8 is %ld",fact);
 [pool drain];
   return 0;
}

Sunday, 6 April 2014

How to format Date using SimpleDateFormat in JSP

<%@ page import="java.io.*,java.util.*" %>
<%@ page import="javax.servlet.*,java.text.*" %>
<html>
<head>
<title>Current Date & Time</title>
</head>
<body>
<center>
<h1>Display Current Date & Time</h1>
</center>
<%
   Date currDt = new Date( );
   SimpleDateFormat sdf =  new SimpleDateFormat ("dd/MM/yyyy hh:mm:ss");
   out.print( "<h1 align=\"center\">" + sdf.format(currDt) + "</h1>");
%>
</body>
</html>

How to open last edited file in Linux

Suppose you have edited last three following files using vi
user1@localhost$ vi file1.txt
user1@localhost$ vi file2.txt
user1@localhost$ vi file3.txt

Now to open the last edited file command is

user1@localhost$ vi ls -t | head -1

Here ls -t sort the file in order of last modified file and head -1 picks up the first file.

So here the output of the command executed above will be file1.txt file will be opened in vi for editing.

How to connect to Oracle 11g using PYTHON

Python is an indent based language. Does not contain any begin or end like if / end if, loop / end loop.
Beginning of scope is in hanging indent and the entire scope is indented.

Like
>>if(codition)
>>     staterment 
>>     staterment

Just look into the indent for the statement below if condition.

Now about oracle client library in python.
You can download oracle client cx_Oracle  from here : https://pypi.python.org/pypi/cx_Oracle/5.1.3.

cx_Oracle-5.1.3-11g.win-amd64-py2.7.exe (md5) MS Windows installer 2.7 2014-05-25 323KB
cx_Oracle-5.1.3-11g.win-amd64-py3.3.exe (md5) MS Windows installer 3.3 2014-05-25 322KB
cx_Oracle-5.1.3-11g.win-amd64-py3.4.exe (md5) MS Windows installer 3.4 2014-05-25 322KB
cx_Oracle-5.1.3-11g.win32-py2.7.exe (md5) MS Windows installer 2.7 2014-05-25 288KB
cx_Oracle-5.1.3-11g.win32-py3.3.exe (md5) MS Windows installer 3.3 2014-05-25 283KB
cx_Oracle-5.1.3-11g.win32-py3.4.exe (md5) MS Windows installer 3.4 2014-05-25 282KB
cx_Oracle-5.1.3-12c.win-amd64-py2.7.exe (md5) MS Windows installer 2.7 2014-05-25 323KB
cx_Oracle-5.1.3-12c.win-amd64-py3.3.exe (md5) MS Windows installer 3.3 2014-05-25 322KB
cx_Oracle-5.1.3-12c.win-amd64-py3.4.exe (md5) MS Windows installer 3.4 2014-05-25 322KB
cx_Oracle-5.1.3-12c.win32-py2.7.exe (md5) MS Windows installer 2.7 2014-05-25 288KB
cx_Oracle-5.1.3-12c.win32-py3.3.exe (md5) MS Windows installer 3.3 2014-05-25 283KB
cx_Oracle-5.1.3-12c.win32-py3.4.exe (md5) MS Windows installer 3.4 2014-05-25 282KB
cx_Oracle-5.1.3.tar.gz (md5) Source
2014-05-25 102KB

Just choose according to your version of python installed.

Install it in your machine if you are using Windows 32 bit download win32-py else if you are using Windows 64 bit  download win-amd64-py.

Here is the sample code below which will help you to connect to Oracle.

#---------------------------------------------------------------------------------------------------
import cx_Oracle
# con = cx_Oracle.connect('username/password@[ipaddress or hostname]/SID')
# for example con = cx_Oracle.connect('scott/tiger@127.0.0.1/orcl')
# Above 2 lies are format of connection string and sample connection string.
# Below is the actual connection string I have used. orcl.localdomain is the service name you have configured with net config assistant in oracle databsae server.

con = cx_Oracle.connect('hr/hr@192.168.0.109/orcl.localdomain')
cur = con.cursor()
cur.execute('select section_code,section_name from section_master order by 2')

for row in cur:

     print(row)
#closing all opened objects
file.close()
cur.close()
con.close()
print("File successfully exported")

#---------------------------------------------------------------------------------------------------

Please comment if you have any queries.
You can also find video tut0rialfor this lesson inhttps://www.youtube.com/watch?v=w3WVqn3WySs
Also you can subscribe to my youtube channel https://www.youtube.com/subhro190776

Manipulating string variable in Python

#!/usr/local/bin/python2.7
   
#Concatenate string
a="This "
b="Python "
c="program "
d="is "
e="about "
f="variable "
resultstr=a+b+c+d+e+f
print(resultstr)

#Repeat string n number of times
str="I will not stop.." * 4
print("\n" + str)
#This is the output : I will not stop..I will not stop..I will not stop..I will not stop..

#Concatenation of Number and string

n=4
m=4.5

s="We can repeat string " + repr(n) + " times but not " + repr(m) + " times"

Different methods used in array in PYTHON

#!/usr/local/bin/python2.7
# pleases follow the indention as it is most important in python. python don't use begin/end, if/end if or loop/
#end loop so python identify statement by indention.

from array import *
my_array = array('i', [1,2,3,4,5,6,7,8,9])

#inserting in nth position .insert(position,value)
my_array.insert(0,12)

#appending value to to the last position
my_array.append(10)

#extending python array using extend method

my_extnd_array = array('i', [7,8,9,10])
my_array.extend(my_extnd_array) # extend(array object)

#add item from list into array using fromlist
lst = [13,14,15]
my_array.fromlist(lst)

#check the number of occurance of an element in array
print('\n9 occured %d times'%my_array.count(9))

#convert any array element to list using tolist() method
c=my_array.tolist()

#remove element from the list from any position
my_array.remove(4) # remove element from 4th position

#reverse elements in a python array using reverse method
my_array.reverse()

#remove element from the last position using pop method
my_array.pop()

#Search any element by index using index method
print('\nElement in 6th position is %d'%my_array.index(6))

#print array list
for i in my_array:
  print(i)

How to check prime number in using Python.

#!/usr/local/bin/python2.7
# pleases follow the indention as it is most important in python. python don't use begin/end, if/end if or loop/
#end loop so python identify statement by indention.

def PrimeNumber(n):
  if n==1 or n==2:
    return 0
  for i in range(3,n/2+1):
    if n%i == 0:
      return 0
  return 1

n = input ('Enter the number to check prime: ')
if PrimeNumber(n)==1:
  print('\n%d is Prime Number'%n)
else:
  print('\n%d is not Prime Number'%n)

How to find factorial of a number using Python in both iteration and recursive method

#!/usr/local/bin/python2.7
# pleases follow the indention as it is most important in python. python don't use begin/end, if/end if or loop/
#end loop so python identify statement by indention.

def Factorial(n):
    fact=1
    for i in range(1,n+1):
       fact=fact*i

    return fact


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

n = input ('Enter the number to find factorial: ')
print('\nFactorial of a number %d is %d'%(n,Factorial(n)))

print('Factorial (recursive) of a number %d is %d'%(n,FactorialRecursive(n)))

Saturday, 5 April 2014

How to read a file from script using PERL

#!/usr/bin/perl
use strict;
use warnings;

use Path::Class;
use autodie; # die if error in reading file

my $dir = dir("/tmp"); # /tmp

my $file = $dir->file("sample_file.txt");

# Read in the whole contents of the file 
 
my $stuff = $file->slurp();

# openr() returns an IO::File object to read from 
 
my $file_handle = $file->openr();

# Read one line at a time
 
 while( my $line = $file_handle->getline() ) {
        print $line;
}

How to write to a file using PERL script

#!/usr/bin/perl
use strict;
use warnings;

use Path::Class;
use autodie; # die if writing a file fails

my $dir = dir("/tmp"); # /tmp

my $file = $dir->file("file.txt"); # /tmp/file.txt

# Get a File Handle (IO::File object) you can write to
my $file_handle = $file->openw();

my @arr = ('This', 'is', 'my', 'first',"Perl","Program");

foreach my $line ( @arr ) {
    # Add the line to the file
    $file_handle->print($line . "\n");