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

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')
               
ver = con.version.split(".")
print ver
             
con.close()

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"