C program to find prime numbers between given range


#include<stdio.h>
void main()
{
int i, prime, upper, lower, n;

printf("\n\n\t ENTER THE LOWER LIMIT => ");
scanf("%d", &lower);
printf("\n\n\t ENTER THE UPPER LIMIT => ");
scanf("%d", &upper);
printf("\n\n\t PRIME NUMBERS ARE");
for(n=lower+1; n<upper; n++)
{
prime = 1;
for(i=2; i<n; i++)
if(n%i == 0)
{
prime = 0;
break;
}
if(prime)
printf("\n\n\t\t\t%d", n);
}
}

Output :-

ENTER THE LOWER LIMIT => 0
ENTER THE UPPER LIMIT =>  10

PRIME NUMBERS ARE

1

2

3

5

7

code to check whether a no is Armstrong no or not



#include<stdio.h>
int main(){
    int num,r,sum=0,temp;

    printf("Enter a number: ");
    scanf("%d",&num);

    temp=num;
    while(num!=0){
         r=num%10;
         num=num/10;
         sum=sum+(r*r*r);
    }
    if(sum==temp)
         printf("%d  It is an Armstrong number",temp);
    else
         printf("%d It is not an Armstrong number",temp);
    return 0;
}

Output:-

Enter a number: 153
It is an Armstrong number

Finding duplicate element from 1-100 numbers JAVA



import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class FindDuplicateNumber {

public static void main(String args[]){
int a[]={1,2,3,4,5,6,7,8,9,5};

System.out.println("Using arrays =>"+usingArrayDup(a));
}

// Finding duplicates using double arrays
private static int usingArrayDup(int[] a) {
//Finding duplicate number using two arrays
for(int i=0;i<a.length;i++){
for(int j=i+1;j<a.length;j++){
if(a[i]==a[j])
return a[i];
}
}
return 0;
}//Double arrays
}

Output:-

Using arrays =>5

Find missing number from array 0-100



public class MissingNumberInArray {

public static void main(String args[]){

int a[]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19};
int asum = 0,sum = 0;
for(int i=1;i<=a.length;i++)
sum=sum+i;
for(int i=0;i<a.length;i++)
asum=asum+a[i];
int missnum=sum-asum;
System.out.println("Missing Number"+missnum);
}
}

Output:-

Missing Number =>20

find out longest palindrome in a given string JAVA


public class LongestPalindrom {
    public static void main(String args[]) {
        String str = " hello . you uoy nitin nitin era woh..";
        char[] a = str.toCharArray();
        int low = Integer.MAX_VALUE;
        int upper = Integer.MIN_VALUE;
        for (int i = 0; i < str.length(); i++) {
            int start = i;
            int end = i;
            while (start >= 0 && end < str.length()) {
                if (a[start] == a[end]) {
                    if (end - start > upper - low) {
                        upper = end;
                        low = start;
                    }
                    end++;
                    start--;
                } else {
                    break;
                }

            }

        }
        for (int i = 0; i < str.length(); i++) {
            int start = i;
            int end = i + 1;
            while (start >= 0 && end < str.length()) {
                if (a[start] == a[end]) {
                    if (end - start > upper - low) {
                        upper = end;
                        low = start;
                    }
                    end++;
                    start--;
                } else {
                    break;
                }
            }

        }
        for (int i = low; i <= upper; i++) {
            System.out.print(a[i]);
        }
    }
}

Output:-

LongestPalindrom
nitin nitin

Find all permutations of String using Recursion


# include <stdio.h>
# include <string.h>

/* Function to swap values at two pointers */
void swap (char *x, char *y)
{
    char temp;
    temp = *x;
    *x = *y;
    *y = temp;
}
 
/* Function to print permutations of string */
void permute(char *a, int i, int n)
{
   int j;
   if (i == n)
     printf("%s\n", a);
   else
   {
        for (j = i; j <= n; j++)
       {
          swap((a+i), (a+j));
          permute(a, i+1, n);
          swap((a+i), (a+j)); //backtrack
       }
   }
}

int main()
{
   char a[] = "ABC";
   int l=strlen(a);
   printf("String length => %d",l);
   permute(a, 0, l-1);
   getchar();
   return 0;
}

Output:-

String length => 3
ABC
ACB
BAC
BCA
CBA
CAB

Find permutations using Recursion


#include <stdio.h>

void print(const int *v, const int size)
{
  if (v != 0)   {
     int i;
    for(i = 0; i < size; i++)     {
      printf("%d", v[i] );
    }
    printf("\n");
  }
} // print

void visit(int *Value, int N, int k)
{
  static level = -1;
  int i;
  level = level+1; Value[k] = level;

  if (level == N)
    print(Value, N);
  else
   
    for(i = 0; i < N; i++)
      if (Value[i] == 0)
        visit(Value, N, i);

  level = level-1; Value[k] = 0;
}

main()
{
  const int N = 4;
  int Value[N];
  int i;
  for (i = 0; i < N; i++)
  {
    Value[i] = 0;
  }
  visit(Value, N, 0);
}

Output:-

1234
1243
1324
1423
1342
1432
2134
2143
3124
4123
3142
4132
2314
2413
3214
4213
3412
4312
2341
2431
3241
4231
3421
4321

Remove any given character from a String

public class RemoveChar4mString {

    public static void main (String[] args)
    {
        System.out.println (removeChar4mString ("My blog name is Sourcecode-kk",  'o'));
    }

    public static String removeChar4mString (String str, char charToBeRemoved)
    {
        if (str == null)
            return "";

        StringBuilder strBuild = new StringBuilder ();

        for (int i = 0; i < str.length (); i++)
        {
            char ch = str.charAt (i);
            if (ch == charToBeRemoved)
                continue;
            strBuild.append (ch);
        }
        return strBuild.toString ();
    }
}

output:-

My blg name is Surcecde-kk

this code removes all "o" character from given string "My Blog name is Sourcecode-kk"


convert number to words in php



<?php

$one= array(
        0                   => ' ',
        1                   => 'one',
        2                   => 'two',
        3                   => 'three',
        4                   => 'four',
        5                   => 'five',
        6                   => 'six',
        7                   => 'seven',
        8                   => 'eight',
        9                   => 'nine',
        10                  => 'ten',
        11                  => 'eleven',
        12                  => 'twelve',
        13                  => 'thirteen',
        14                  => 'fourteen',
        15                  => 'fifteen',
        16                  => 'sixteen',
        17                  => 'seventeen',
        18                  => 'eighteen',
        19                  => 'nineteen'
        );
       
    $ten = array (
        0    =>    '',
        1    =>    '',
        2    =>    'twenty',
        3    =>    'thirty',
        4    =>    'fourty',
        5    =>    'fifty',
        6    =>    'sixty',
        7    =>    'seventy',
        8    =>    'eighty',
        9    =>    'ninety'
        );
   
function num2word($n)
{
    echo "Enter no between 1 to 999999999 \n";
    if($n<=0 && $n>999999999)
        echo "please enter valid no";
    else
 {
                  pw((($n/10000000)%100)," crore ");
                  pw((($n/100000)%100)," lakh ");
                  pw((($n/1000)%100)," thousand ");
                  pw((($n/100)%10)," hundred ");
                  pw(($n%100)," ");
 }   
}       

function pw($n,$ch)
{
 global $one;
 global $ten;

 echo ($n>19) ? ($ten[$n/10]." ".$one[$n%10]) : $one[$n];
 if($n) echo $ch;
 }

echo "\n". num2word(123456789);       
echo "\n". num2word(123477);
echo "\n". num2word(1000);
echo "\n". num2word(999);

?>       

Output :-

twelve crore thirty four lakhs fifty six thousand seven hundreds eighty nine

one lakhs twenty three thousand four hundreds seventy seven

one thousand

nine hundreds ninety nine

C Program for Factorial using Recursive Function


#include<stdio.h>
#include<conio.h>
long int factorial(int n);
void main()
{
int n;
clrscr();
printf("Enter the number:\n\n");
scanf("%d",&n);
printf("Factorial of %d is %ld",n,factorial(n));
getch();
}
long int factorial(int n)
{
if(n<=1)
{
return(01);
}
else
{
n=n*factorial(n-1);
return(n);
}
}

Output:- 

Enter the number
5
Factorial of 5 is 120

C Program for Base Conversion from Binary to Decimal


#include<stdio.h>
#include<conio.h>
int power (int);
void main()
{
long int n;
int x,s=0,i=0,flag=1;
clrscr();
printf("Input a Binary Number\n\n");
scanf("%ld",&n);
while(flag==1)
{
x=n%10;
s=s+x*power(i);
i=i+1;
n=n/10;
if(n==0)
flag=0;
}
printf("\nDecimal Equivalent = %d",s);
getch();
}
power(int i)
{
int j,p=1;
for(j=1;j<=i;j++)
p = p*2;
return(p);
}

C program for fibonacci series


#include<stdio.h>
#include<conio.h>
void main()
{
 int n;
 long int i;
 long int fibo(int n);
 clrscr();
 printf("Enter the limit:\n");
 scanf("%d",&n);
   i=fibo(n);
 printf("\nThe %dth Fibonacci number is %ld",n,i);
 getch();
}

long int fibo(int n)
{
 int old_no,currnt_no,sum,i;
   i=1;
   old_no=0;
   currnt_no=1;
   while(i<=n)
   {
      sum=old_no+currnt_no;
      old_no=currnt_no;
      currnt_no=sum;
      i++;
      printf("\n%d",sum);
   }
 return(sum);
}

Output:-

Enter the limit:

5

1
2
3
5
8
The 5th Fibonacci number is 8

C program for printing Pascal's Triangle


#include <stdio.h>
#include <conio.h>
void main()
{
int p[10][10];
int i,j,k;
clrscr();
printf("\nPascal's Triangle\n");
for(i=0;i<10;i++)
{
j=1;
p[i][0]=1;
p[i][i]=1;
while(j<i)
{
p[i][j]=p[i-1][j-1]+p[i-1][j];
j++;
}
}
for(i=0;i<10;i++)
{
j=10;
while(j>i)
{
printf("  ");
j--;
}
for(k=0;k<=i;k++)
{
printf("%4d",p[i][k]);
}
printf("\n\n");
}
getch();
}
Output:-

Pascal's Triangle
                    1                                                       
                  1   1                                                     
                1   2   1                                                   
             1   3   3   1                                                 
          1   4   6   4   1                                               
        1   5  10  10   5   1
      1   6  15  20  15   6   1                                           
    1   7  21  35  35  21   7   1                                         
  1   8  28  56  70  56  28   8   1                                       
1   9  36  84 126 126  84  36   9   1                                     

Eclipse shortcut keys



An integrated development environment (IDE) is a software application that provides comprehensive facilities to computer programmers for software development. An IDE normally consists of a source code editor, build automation tools and a debugger.
IDEs are designed to maximize programmer productivity by providing tight-knit components with similar user interfaces

Editors are an integral part of a programmer’s life. If you have good proficiency in using an editor thats a great advantage. For java developers there is huge list and some opular are Eclipse , Netbeans, MyEclipse. 
I use Eclipse as a my IDE. It is the leading development environment for Java

This post gives you information about various shortcut keys in Eclipse

You can use following keys for listing all shortcuts
CTRL + SHIFT + L

1.    Manage Files and Projects

Ctrl+N
Create new project using the Wizard
Ctrl+Alt+n
Create new project, file, class, etc.
Alt+f, then .
Open project, file, etc.
Ctrl+Shift+r
Open Ressource (file, folder or project)
Alt+Enter
Show and access file properties
Ctrl+s
Save current file
Ctrl+Shift+s
Save all files
Ctrl+w
Close current file
Ctrl+Shift+w
Close all files
F5
Refresh content of selected element with local file system

2.    Editor Window
Focus/ cursor must be in Editor Window for these to work.

F12
Jump to Editor Window
Ctrl+Page Down/Ctrl+Page Up
Switch to next editor / switch to previous editor
Ctrl+m
Maximize or un-maximize current Editor Window (also works for other Windows)
Ctrl+e
Show list of open Editors. Use arrow keys and enter to switch
Ctrl+F6/Ctrl+Shift+F6
Show list of open Editors. Similar to ctrl+e but switches immediately upon release of ctrl
Alt+Arrow Left/Alt+Arrow Right
Go to previous / go to next Editor Window
Alt+-
Open Editor Window Option menu
Ctrl+F10
Show view menu (features available on left vertical bar: breakpoints, bookmarks, line numbers, …)
Ctrl+F10, then n
Show or hide line numbers
Ctrl+Shift+q
Show or hide the diff column on the left (indicates changes since last save)

Difference between Spring and Struts framework



Spring is a light weight and open source framework created by Rod Johnson in 2003. Spring is a complete and a modular framework, Spring framework can be used for all layer implementations for a real time application or spring can be used for the development of particular layer of a real time application.

Struts is an open-source web application framework for developing Java EE web applications. It uses and extends the Java Servlet API to encourage developers to adopt a model–view–controller (MVC) architecture. It was originally created by Craig McClanahan and donated to the Apache Foundation in May, 2000.

Listed below is the comparison chart of difference between Spring and Strut Framework

Spring
Struts
Spring is a full-stack application framework that provides an MVC Framework
(Spring MVC) as one of its many modules.
Struts is a web framework which can be used to develop web applications using MVC pattern.

Spring Framework is said to be a non-invasive means it doesn’t force a programmer to extend or implement their class from any predefined class or interface given by Spring API
struts is said to be invasive .In struts we used to extend Action Class.
It forces the programmer that, the programmer class must extend from the base class provided by struts API
Spring implements IOC Design Pattern and addresses AOP Cross cutting concerns.
Struts implement MVC Design Patten
Spring is light weight framework
Struts is heavy weight framework
Spring is loosely coupled
Struts is tightly coupled
Spring is a Layered Architecture
Struts is a not a Layered Architecture
Spring provides easy integration with technologies like JDBC, Hibernate
We have to hard code to use technologies
like hibernate, jdbc, etc.
Spring framework not only allows JSPs but also
other views such as Velocity, Freemaker etc.
Struts allows only JSP as a view component.

Spring MVC provides more handler mappings.
Struts doesn’t have specific handler mappings but uses Action Mappings.
It also provides more model transfer.
It has fewer model transfer.
Spring offers complete Decoupling between layers in MVC.

Struts offers limited Decoupling between layers in MVC.

Spring offers IOC (inversion of control) and AOP (Aspect Oriented Programming).
Struts uses IOC internally but doesn’t provide to the programmer / developer.
Spring clear module division between Model, View and Controller.
Struts mixes Controller and Model.
Spring MVC just provides tags for basic HTML form tags
Struts 2 provides many out-of-box JSF-styled complex and composite tags
Such as: Ajax DOJO tags
Spring has spring webFlow for the same purpose, which is part of spring Framework.
Struts will be Generally used as a Front End Frame work.
Advantages of spring Framework:-
- Transaction management
- support for Messaging
- support and Integration with Other Frame works.
(Eg: Hibernate, Struts, Tapestry.. etc)
Advantages of Struts Framework:-
-Excellent support for Tag Library, which has wide industry acceptance.
-Easy to integrate with other client side technologies.