Wednesday, November 2, 2011
Monday, October 17, 2011
Encode Image to BASE64 and Decode it- ANDROID (JAVA)
As of my Personal Experience, I am attaching a Base 64 file with my this post to convert Image to BASE64 and decode back. I have not written this Base 64 file but have taken a reference from other.
Download here
Download here
Thursday, September 29, 2011
To check number is odd or even without using OPERATOR( C++)
Hint: When we do conversion from decimal to binary then you have noticed that last digit of binary number is '0' if number is even ELSE '1'.
Using same logic we will solve this program.
#include "iostream.h"
#include "conio.h"
void main()
{
int number, last_digit;
cout<<"Enter the Number"; cin>>number;
last_digit=number & 1; //Give the last digit of Binary number
if(last_digit==0)
cout<<"Number is EVEN";
else
cout<<"Number is ODD";
}
Using same logic we will solve this program.
#include "iostream.h"
#include "conio.h"
void main()
{
int number, last_digit;
cout<<"Enter the Number"; cin>>number;
last_digit=number & 1; //Give the last digit of Binary number
if(last_digit==0)
cout<<"Number is EVEN";
else
cout<<"Number is ODD";
}
Sunday, July 10, 2011
To change case of String
INPUT: JavA MaDe eASY
OUTPUT: jAVa mAdE Easy
class StringDemochangecase
{
static String changecase(String s)
{
String s1="";
for(int i=0;i
{
char ch;
if(s.charAt(i)<91)
{
int c=s.charAt(i)+32;
ch=(char)c;
}
else
{
int c=s.charAt(i)-32;
ch=(char)c;
}
s1=s1+ch;
}
return s1;
}
public static void main(String args[])
{
String s="JavA MaDe eASY";
String s1=changecase(s);
System.out.println(s1);
}
}
OUTPUT: jAVa mAdE Easy
class StringDemochangecase
{
static String changecase(String s)
{
String s1="";
for(int i=0;i
char ch;
if(s.charAt(i)<91)
{
int c=s.charAt(i)+32;
ch=(char)c;
}
else
{
int c=s.charAt(i)-32;
ch=(char)c;
}
s1=s1+ch;
}
return s1;
}
public static void main(String args[])
{
String s="JavA MaDe eASY";
String s1=changecase(s);
System.out.println(s1);
}
}
Saturday, July 9, 2011
To check whether String is Palindrome or Not
public class StringDemopalindrome
{
static boolean palindrome(String s)
{
String s1="";
int i, flag=0;
for(i=s.length()-1;i>=0;i--)
{
s1=s1+s.charAt(i);
}
for(i=0;i
{
if (s.charAt(i)!=s1.charAt(i))
flag =1;
}
if(flag==1)
return false;
else
return true;
}
public static void main(String args[])
{
String s="madam";
boolean b=palindrome(s);
System.out.println(b);
}
}
{
static boolean palindrome(String s)
{
String s1="";
int i, flag=0;
for(i=s.length()-1;i>=0;i--)
{
s1=s1+s.charAt(i);
}
for(i=0;i
if (s.charAt(i)!=s1.charAt(i))
flag =1;
}
if(flag==1)
return false;
else
return true;
}
public static void main(String args[])
{
String s="madam";
boolean b=palindrome(s);
System.out.println(b);
}
}
Calculate Length of String
public class StringDemolength
{
static int length(String s)
{
int i,count=0;
for(i=0;i
{
count++;
}
return count;
}
public static void main(String args[])
{
String s="India is a good country";
int count=length(s);
System.out.println(count);
}
}
{
static int length(String s)
{
int i,count=0;
for(i=0;i
count++;
}
return count;
}
public static void main(String args[])
{
String s="India is a good country";
int count=length(s);
System.out.println(count);
}
}
Print String in Reverse Order
INPUT: INDIA
OUTPUT: AIDNI
public class StringDemoReverse
{
static String reverse(String s)
{
int i;
String s1="";
for(i=s.length()-1;i>=0;i--)
{
s1=s1+s.charAt(i);
}
return s1;
}
public static void main(String args[])
{
String s="India";
String s1=reverse(s);
System.out.println(s1);
}
}
OUTPUT: AIDNI
public class StringDemoReverse
{
static String reverse(String s)
{
int i;
String s1="";
for(i=s.length()-1;i>=0;i--)
{
s1=s1+s.charAt(i);
}
return s1;
}
public static void main(String args[])
{
String s="India";
String s1=reverse(s);
System.out.println(s1);
}
}
Saturday, June 11, 2011
Swap Two Images on button click in JAVA
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class MyCanvas extends Canvas
{
Image i,j;
int flag;
MyCanvas()
{
Toolkit t= Toolkit.getDefaultToolkit();
i=t.getImage("f:\\hope.jpg"); //PATH OF FIRST IMAGE
j=t.getImage("f:\\Every_step.jpg"); //PATH OF SECOND IMAGE
}
public void paint(Graphics g)
{
if(flag==0)
{
g.drawImage(i,50,50,this);
g.drawImage(j,300,50,this);
}
if (flag==1)
{
g.drawImage(i,300,50,this);
g.drawImage(j,50,50,this);
}
}
}
class SwapImages implements ActionListener
{
MyCanvas m= new MyCanvas();
JFrame jf;
SwapImages()
{
jf=new JFrame("SWAP IMAGES");
jf.add(m);
JButton button1=new JButton("Swap");
Panel p=new Panel();
p.add(button1);
jf.add(p,BorderLayout.SOUTH);
button1.addActionListener(this);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(400,400);
jf.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(m.flag==0)
m.flag=1;
else
m.flag=0;
m.repaint();
}
public static void main(String s[])
{
new SwapImages();
}
}
import java.awt.event.*;
import javax.swing.*;
class MyCanvas extends Canvas
{
Image i,j;
int flag;
MyCanvas()
{
Toolkit t= Toolkit.getDefaultToolkit();
i=t.getImage("f:\\hope.jpg"); //PATH OF FIRST IMAGE
j=t.getImage("f:\\Every_step.jpg"); //PATH OF SECOND IMAGE
}
public void paint(Graphics g)
{
if(flag==0)
{
g.drawImage(i,50,50,this);
g.drawImage(j,300,50,this);
}
if (flag==1)
{
g.drawImage(i,300,50,this);
g.drawImage(j,50,50,this);
}
}
}
class SwapImages implements ActionListener
{
MyCanvas m= new MyCanvas();
JFrame jf;
SwapImages()
{
jf=new JFrame("SWAP IMAGES");
jf.add(m);
JButton button1=new JButton("Swap");
Panel p=new Panel();
p.add(button1);
jf.add(p,BorderLayout.SOUTH);
button1.addActionListener(this);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(400,400);
jf.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(m.flag==0)
m.flag=1;
else
m.flag=0;
m.repaint();
}
public static void main(String s[])
{
new SwapImages();
}
}
Thursday, May 19, 2011
Swap two numbers without using third variable
Example:
Before Swap
number1=45 number2=33
After Swap
number1=33 number2=45
class Swap {
public static void main(String[] args) {
int number1=45,number2=33;
number1 = number1 + number2;
number2 = number1 - number2;
number1 = number1 - number2;
System.out.println("After swapping, number1= " + number1 + " and number2= "
+ number2);
}
}
Before Swap
number1=45 number2=33
After Swap
number1=33 number2=45
class Swap {
public static void main(String[] args) {
int number1=45,number2=33;
number1 = number1 + number2;
number2 = number1 - number2;
number1 = number1 - number2;
System.out.println("After swapping, number1= " + number1 + " and number2= "
+ number2);
}
}
Tuesday, May 17, 2011
Summation of elements of upper triangle of matrix
Example:
1 2 3
0 4 5
0 0 6
public class arraydemotriangleBsum {
static void triangleBsum(int x[][])
{
int i,j,c=0;
for(i=0;i<3;i++) { for(j=0;j<3;j++) { if(i>=j)
{
c=c+x[i][j];
}
}
}
System.out.println("The Sum of triangle B is:"+c);
}
public static void main(String[] args)
{
triangleBsum(new int[][]{{1,2,3},{4,5,6},{7,8,9}});
}
}
1 2 3
0 4 5
0 0 6
public class arraydemotriangleBsum {
static void triangleBsum(int x[][])
{
int i,j,c=0;
for(i=0;i<3;i++) { for(j=0;j<3;j++) { if(i>=j)
{
c=c+x[i][j];
}
}
}
System.out.println("The Sum of triangle B is:"+c);
}
public static void main(String[] args)
{
triangleBsum(new int[][]{{1,2,3},{4,5,6},{7,8,9}});
}
}
To find minimum number in upper triangle of matrix
Example:
1 2 3
0 4 5
0 0 6
public class arraydemotriangleBmin {
static void triangleBmin(int x[][])
{
int i,j,min=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{min=x[0][0];
if(i<=j)
{
if(x[i][j]
{
min=x[i][j];
}
}
}
}
System.out.println("The Min Value in triangle B is:"+ min);
}
public static void main(String[] args)
{
triangleBmin(new int[][]{{1,2,3},{4,5,6},{7,8,9}});
}
}
1 2 3
0 4 5
0 0 6
public class arraydemotriangleBmin {
static void triangleBmin(int x[][])
{
int i,j,min=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{min=x[0][0];
if(i<=j)
{
if(x[i][j]
min=x[i][j];
}
}
}
}
System.out.println("The Min Value in triangle B is:"+ min);
}
public static void main(String[] args)
{
triangleBmin(new int[][]{{1,2,3},{4,5,6},{7,8,9}});
}
}
To find maximum number in upper triangle of matrix
Example:
1 2 3
0 4 5
0 0 6
public class arraydemotriangleBmax {
static void triangleBmax(int x[][])
{
int i,j,max=0;
for(i=0;i<3;i++) { for(j=0;j<3;j++) {max=x[0][0]; if(i<=j) { if(x[i][j]>max)
{
max=x[i][j];
}
}
}
}
System.out.println("The Max Value in triangle B is:"+ max);
}
public static void main(String[] args)
{
triangleBmax(new int[][]{{1,2,3},{4,5,6},{7,8,9}});
}
}
1 2 3
0 4 5
0 0 6
public class arraydemotriangleBmax {
static void triangleBmax(int x[][])
{
int i,j,max=0;
for(i=0;i<3;i++) { for(j=0;j<3;j++) {max=x[0][0]; if(i<=j) { if(x[i][j]>max)
{
max=x[i][j];
}
}
}
}
System.out.println("The Max Value in triangle B is:"+ max);
}
public static void main(String[] args)
{
triangleBmax(new int[][]{{1,2,3},{4,5,6},{7,8,9}});
}
}
Summation of elements of lower triangle of matrix
Example:
1 0 0
2 3 0
4 5 6
public class arraydemotriangleAsum {
static void triangleAsum(int x[][])
{
int i,j,c=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i<=j)
{
c=c+x[i][j];
}
}
}
System.out.println("The Sum of triangle A is:"+c);
}
public static void main(String[] args)
{
triangleAsum(new int[][]{{1,2,3},{4,5,6},{7,8,9}});
}
}
1 0 0
2 3 0
4 5 6
public class arraydemotriangleAsum {
static void triangleAsum(int x[][])
{
int i,j,c=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i<=j)
{
c=c+x[i][j];
}
}
}
System.out.println("The Sum of triangle A is:"+c);
}
public static void main(String[] args)
{
triangleAsum(new int[][]{{1,2,3},{4,5,6},{7,8,9}});
}
}
To find minimum number in lower triangle of matrix
Example:
1 0 0
2 3 0
4 5 6
public class arraydemotriangleAmin {
static void triangleAmin(int x[][])
{
int i,j,min=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{min=x[0][0];
if(i<=j)
{
if(x[i][j]
{
min=x[i][j];
}
}
}
}
System.out.println("The Min Value in triangle A is:"+ min);
}
public static void main(String[] args)
{
triangleAmin(new int[][]{{1,2,3},{4,5,6},{7,8,9}});
}
}
1 0 0
2 3 0
4 5 6
public class arraydemotriangleAmin {
static void triangleAmin(int x[][])
{
int i,j,min=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{min=x[0][0];
if(i<=j)
{
if(x[i][j]
min=x[i][j];
}
}
}
}
System.out.println("The Min Value in triangle A is:"+ min);
}
public static void main(String[] args)
{
triangleAmin(new int[][]{{1,2,3},{4,5,6},{7,8,9}});
}
}
To find maximum number in lower triangle of matrix
Example:
1 0 0
2 3 0
4 5 6
public class arraydemotriangleAmax {
static void triangleAmax(int x[][])
{
int i,j,max=0;
for(i=0;i<3;i++) { for(j=0;j<3;j++) {max=x[0][0]; if(i<=j) { if(x[i][j]>max)
{
max=x[i][j];
}
}
}
}
System.out.println("The Max Value in triangle A is:"+ max);
}
public static void main(String[] args)
{
triangleAmax(new int[][]{{1,2,3},{4,5,6},{7,8,9}});
}
}
1 0 0
2 3 0
4 5 6
public class arraydemotriangleAmax {
static void triangleAmax(int x[][])
{
int i,j,max=0;
for(i=0;i<3;i++) { for(j=0;j<3;j++) {max=x[0][0]; if(i<=j) { if(x[i][j]>max)
{
max=x[i][j];
}
}
}
}
System.out.println("The Max Value in triangle A is:"+ max);
}
public static void main(String[] args)
{
triangleAmax(new int[][]{{1,2,3},{4,5,6},{7,8,9}});
}
}
Sort of single dimension array
public class arraydemosortsingle
{
public static void sort(int []x)
{
int i;
for( i=0;i=0;i--)
{
System.out.println(x[i]);
}
}
public static void main(String[] args)
{
sort(new int[] {44,22,77,55,22});
}
}
{
public static void sort(int []x)
{
int i;
for( i=0;i
{
System.out.println(x[i]);
}
}
public static void main(String[] args)
{
sort(new int[] {44,22,77,55,22});
}
}
To find minimum number in single dimensional array
public class arraydemominsingle
{
public static int min(int []x)
{
int min=x[0];
for(int i=1;i
{
if(x[i]
{
min=x[i];
}
}
return min;
}
public static void main(String[] args)
{
System.out.println("Minimum value is"+min(new int[]{45,33,56,32}));
}
}
{
public static int min(int []x)
{
int min=x[0];
for(int i=1;i
if(x[i]
min=x[i];
}
}
return min;
}
public static void main(String[] args)
{
System.out.println("Minimum value is"+min(new int[]{45,33,56,32}));
}
}
To find minimum number in double dimensional array
public class arraydemomindouble
{
public static int min(int [][]x)
{
int min=x[0][0];
for(int i=0;i<3;i++)
{
for(int j=0;j<2;j++)
{
if(x[i][j]
{
min=x[i][j];
}
}
}
return min;
}
public static void main(String[] args)
{
System.out.println("Minimum value is"+min(new int[][]{{45,33},{56,32},{53,67}}));
}
}
{
public static int min(int [][]x)
{
int min=x[0][0];
for(int i=0;i<3;i++)
{
for(int j=0;j<2;j++)
{
if(x[i][j]
min=x[i][j];
}
}
}
return min;
}
public static void main(String[] args)
{
System.out.println("Minimum value is"+min(new int[][]{{45,33},{56,32},{53,67}}));
}
}
Merge Sort
public class arraydemomergesort
{
public static void mergesort(int x[], int y[])
{
int i,j=0,z[];
z=new int [x.length+y.length];
for(i=0;i=0;i--)
{
System.out.println(z[i]);
}
}
public static void main(String[] args)
{
mergesort(new int[] {44,22,77,55,22},new int[]{22,59,64,77});
}
}
{
public static void mergesort(int x[], int y[])
{
int i,j=0,z[];
z=new int [x.length+y.length];
for(i=0;i
{
System.out.println(z[i]);
}
}
public static void main(String[] args)
{
mergesort(new int[] {44,22,77,55,22},new int[]{22,59,64,77});
}
}
To find maximum number in single dimensional matrix
public class arraydemomaxsingle
{
public static int max(int []x)
{
int max=x[0];
for(int i=1;imax)
{
max=x[i];
}
}
return max;
}
public static void main(String[] args)
{
System.out.println("Maximum value is"+max(new int[]{45,33,56,32}));
}
}
{
public static int max(int []x)
{
int max=x[0];
for(int i=1;i
{
max=x[i];
}
}
return max;
}
public static void main(String[] args)
{
System.out.println("Maximum value is"+max(new int[]{45,33,56,32}));
}
}
To find Maximum number in Double dimensional matrix
public class arraydemomaxdouble
{
public static int max(int [][]x)
{
int max=x[0][0];
for(int i=0;i<3;i++)
{
for(int j=0;j<2;j++)
{
if(x[i][j]
{
max=x[i][j];
}
}
}
return max;
}
public static void main(String[] args)
{
System.out.println("Maximum value is"+max(new int[][]{{45,33},{56,32},{53,67}}));
}
}
{
public static int max(int [][]x)
{
int max=x[0][0];
for(int i=0;i<3;i++)
{
for(int j=0;j<2;j++)
{
if(x[i][j]
max=x[i][j];
}
}
}
return max;
}
public static void main(String[] args)
{
System.out.println("Maximum value is"+max(new int[][]{{45,33},{56,32},{53,67}}));
}
}
Summation of Matrices
public class arraydemomatrixsum {
static void matrixsum(int x[][])
{
int i,j,c,b=0,a=0;
for(i=0;i<3;i++)
{ c=0;
System.out.print(" ");
for(j=0;j<3;j++)
{
System.out.print(x[i][j]+" ");
c=c+x[i][j];
if(i==j)
{
b=b+x[i][j];
}
if(i+j==2)
{
a=a+x[i][j];
}
}
System.out.print(c);
System.out.println("");
}
System.out.print(a);
for(j=0;j<3;j++)
{ c=0;
for(i=0;i<3;i++)
{
c=c+x[i][j];
}
System.out.print(c+" ");
}
System.out.print(b);
}
public static void main(String[] args)
{
matrixsum(new int[][]{{1,2,3},{4,5,6},{7,8,9}});
}
}
static void matrixsum(int x[][])
{
int i,j,c,b=0,a=0;
for(i=0;i<3;i++)
{ c=0;
System.out.print(" ");
for(j=0;j<3;j++)
{
System.out.print(x[i][j]+" ");
c=c+x[i][j];
if(i==j)
{
b=b+x[i][j];
}
if(i+j==2)
{
a=a+x[i][j];
}
}
System.out.print(c);
System.out.println("");
}
System.out.print(a);
for(j=0;j<3;j++)
{ c=0;
for(i=0;i<3;i++)
{
c=c+x[i][j];
}
System.out.print(c+" ");
}
System.out.print(b);
}
public static void main(String[] args)
{
matrixsum(new int[][]{{1,2,3},{4,5,6},{7,8,9}});
}
}
Multiplication of matrices
public class arraydemomatrixmultiply {
static void matrixadd(int x[][],int y[][])
{
int i,j,k;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print(x[i][j]+" ");
}
System.out.print(" ");
for(k=0;k<3;k++)
{
System.out.print(y[i][k]+" ");
}
System.out.println("");
}
int z[][];
z=new int[3][3];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
z[i][j]=0;
for(k=0;k<3;k++)
{
z[i][j]= z[i][j]+x[i][k]*y[k][j];
}
System.out.print(z[i][j]+" ");
}
System.out.println("");
}
}
public static void main(String[] args)
{
matrixadd(new int[][]{{1,2,3},{4,5,6},{7,8,9}},new int[][]{{1,2,3},{4,5,6},{7,8,9}});
}
}
static void matrixadd(int x[][],int y[][])
{
int i,j,k;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print(x[i][j]+" ");
}
System.out.print(" ");
for(k=0;k<3;k++)
{
System.out.print(y[i][k]+" ");
}
System.out.println("");
}
int z[][];
z=new int[3][3];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
z[i][j]=0;
for(k=0;k<3;k++)
{
z[i][j]= z[i][j]+x[i][k]*y[k][j];
}
System.out.print(z[i][j]+" ");
}
System.out.println("");
}
}
public static void main(String[] args)
{
matrixadd(new int[][]{{1,2,3},{4,5,6},{7,8,9}},new int[][]{{1,2,3},{4,5,6},{7,8,9}});
}
}
Addition of two matrics
public class arraydemomatrixadd {
static void matrixadd(int x[][],int y[][])
{
int i,j,k;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print(x[i][j]+" ");
}
System.out.print(" ");
for(k=0;k<3;k++)
{
System.out.print(y[i][k]+" ");
}
System.out.println("");
}
int z[][];
z=new int[3][3];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
z[i][j]= x[i][j]+y[i][j];
System.out.print(z[i][j]+" ");
}
System.out.println("");
}
}
public static void main(String[] args)
{
matrixadd(new int[][]{{1,2,3},{4,5,6},{7,8,9}},new int[][]{{1,2,3},{4,5,6},{7,8,9}});
}
}
static void matrixadd(int x[][],int y[][])
{
int i,j,k;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print(x[i][j]+" ");
}
System.out.print(" ");
for(k=0;k<3;k++)
{
System.out.print(y[i][k]+" ");
}
System.out.println("");
}
int z[][];
z=new int[3][3];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
z[i][j]= x[i][j]+y[i][j];
System.out.print(z[i][j]+" ");
}
System.out.println("");
}
}
public static void main(String[] args)
{
matrixadd(new int[][]{{1,2,3},{4,5,6},{7,8,9}},new int[][]{{1,2,3},{4,5,6},{7,8,9}});
}
}
Intersection of two sets
public class arraydemointersect
{
static void intersect(int x[],int y[])
{
int i,j,k=0,z[];
z=new int[5];
for(i=0;i
{
for(j=0;j
{
if (x[i]==y[j]&&x[i]!=z[j])
{
z[k]=x[i];
System.out.println(z[k]);
k++;
}
}
}
}
public static void main(String[] args)
{
intersect(new int[] {44,22,77,55,22,33},new int[]{22,59,64,77});
}
}
{
static void intersect(int x[],int y[])
{
int i,j,k=0,z[];
z=new int[5];
for(i=0;i
for(j=0;j
if (x[i]==y[j]&&x[i]!=z[j])
{
z[k]=x[i];
System.out.println(z[k]);
k++;
}
}
}
}
public static void main(String[] args)
{
intersect(new int[] {44,22,77,55,22,33},new int[]{22,59,64,77});
}
}
Subscribe to:
Posts (Atom)