IQ

Ad SpeedyAds

Wednesday, October 3, 2012

Java porgram to check whether a number is Armstrong number

//program for checking Armstrong Number
//An Armstromg number is one which is equal to the sum of the cubes of its digits
//Eg. 153=1³+5³+3³=1+125+27=153...
import java.io.*;
class armstrong
{
 public static void main(String aa[]) throws IOException
{
 BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
int a, b=0, c, d;
System.out.println("Enter a number to check for Armstong");
String S=br.readLine();
a=Integer.parseInt(S);
c=a;
while(c!=0)
{
 d=c%10;
 c=c/10;
 b+=d*d*d;
}
if(a==b)
System.out.println(a+" is an Armstrong no.");
else
System.out.println(a+" is NOT an Armstrong no.");
}
}

Friday, March 9, 2012

Java program to print the words of a sentence in reverse

import java.io.*;
class abc
{
public static void main(Stringa aa[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a sentence");
String S=br.readLine();
S=S.trim();
S=" "+S+" ";
int L=S.length(),a=L;
for(int i=L-1; i>=0; i--)
{
if(S.charAt(i)==32)
{
System.out.print(S.substring(i+1,b)+" ");
b=i;
}
}
}
}

Thursday, March 8, 2012

Javaprogram to print the first and last letters of all words in a program

import java.io.*;
class abc
{
public static void main(Stringa aa[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a sentence");
String S=br.readLine();
S=S.trim();
S=" "+S+" ";
int a=1, L=S.length();
for(int i=1; i {
if(S.charAt(i)==32)
{
System.out.print(S.charAt(a));
if((i-1)!=a)
System.out.print(S.charAt(i-1));
System.out.print(" ");
a=++i;
}
}
}
}

Thursday, February 16, 2012

Console based Playing Cards Trick In Java

import java.io.*;
class card
{
static char[]Suits=new char[21];
static char[]Number=new char[21];
static char[][]StacksS=new char[3][7];
static char[][]StacksN=new char[3][7];
public static void ReAdjust(int a, int b)
{
for(int i=0; i<=6; i++)
{
char A=StacksS[a][i];
StacksS[a][i]=StacksS[b][i];
StacksS[b][i]=A;
A=StacksN[a][i];
StacksN[a][i]=StacksN[b][i];
StacksN[b][i]=A;
}
}
public static void D2Stacks()
{
int k=0;
for(int i=0; i<=6; i++)
{
for(int j=0; j<3; j++)
{
StacksS[j][i]=Suits[k];
StacksN[j][i]=Number[k++];
}
}
}
public static void D2Pile()
{
int k=0;
for(int i=0; i<3; i++)
{
for(int j=0; j<7; j++)
{
Suits[k]=StacksS[i][j];
Number[k++]=StacksN[i][j];
}
}
}
public static void Display(int a)
{
for(int i=0; i<=6; i++)
{
for(int j=0; j<3; j++)
{
for(int v=0; v<=888888; v++)
System.out.print("");
System.out.print(""+StacksS[j][i]+""+StacksN[j][i]+" ");
}

}
System.out.println();
System.out.println("(Please note: C=Club, H=Heart, D=Diamond, S=Spade, T=10)");
}
public static void Display()
{
System.out.println("Stack-1 Stack-2 Stack-3");
for(int i=0; i<=6; i++)
{
for(int a=0; a<=888888; a++)
System.out.print("");
for(int j=0; j<3; j++)
{
System.out.print(""+StacksS[j][i]+""+StacksN[j][i]+" ");
}
System.out.println("");
}
}
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
char[][]A=new char[4][13];
char[]B={'H', 'S', 'D', 'C'};
char[]C={'A',50,51,52,53,54,55,56,57,'T','J','Q','K'};
for(int i=0; i<=3; i++)
for(int j=0; j<=12; j++)
A[i][j]=C[j];
int q, b, c=0;
for(c=0; c<=20; c++)
{
do{
q=(int)(Math.random()*4);
b=(int)(Math.random()*13);
}while(A[q][b]==' ');
Suits[c]=B[q];
Number[c]=A[q][b];
A[q][b]=' ';
}
D2Stacks();
System.out.println("Select a card from the following cards in your mind: ");
for(int a=0; a<=888888; a++)
System.out.print("");
Display(1);
System.out.println("\n\nNow press enter: ");
String S=br.readLine();
D2Pile();
D2Stacks();
System.out.println("\n\nSelect the stack in which your card lies this time around: ");
Display();
System.out.println("\n\nNow type the stack number and press enter: ");
int a=Integer.parseInt(br.readLine());
if(a!=2)
ReAdjust(1, a-1);
for(int p=0; p<=888888; p++)
System.out.print("");
D2Pile();
D2Stacks();
System.out.println("\n\nSelect the stack in which your card lies for the last time now: ");
Display();
System.out.println("\n\nNow type the stack number and press enter: ");
a=Integer.parseInt(br.readLine());
if(a!=2)
ReAdjust(1, a-1);
for(int p=0; p<=888888; p++)
System.out.print("");
D2Pile();
D2Stacks();
System.out.println("\n\nSelect the stack in which your card lies: ");
Display();
System.out.println("\n\nNow type the stack number and press enter: ");
a=Integer.parseInt(br.readLine());
if(a!=2)
ReAdjust(1, a-1);
for(int p=0; p<=888888; p++)
System.out.print("");
System.out.print("You selected .");
for(int i=1; i<=399999999; i++)
if(i%122222222==0)
System.out.print(" .");
System.out.print(" "+StacksS[1][3]+""+StacksN[1][3]);
}
}

Saturday, February 11, 2012

Java program to find the divisibility by 2 without using mod(%)

/**to find divisibility by 2 without using "%"
*/
public class Shivam
{
public static boolean check(int a)
{
if(a/2.0-a/2==0)
return true;
return false;

}
}

Recursive function in java to print all natural numbers from 1 upto (n-1)

// recursive func to print a series of natural nos. upto (x-1)
import java.io.*;
public class RecFUNCseries
{
public static void main(String args[]) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter an integer: ");
int x=Integer.parseInt(br.readLine());
series(x);
}
public static void series(int x)
{
if(x>0)
series(--x);
if(x!=0)
System.out.print(x+" ");
}
}

Java programs to find the coprimes of a number

// coprimes of any number n are numbers bw 1 and n, which are not divisible by any factors of n
public class Coprimes
{

public static void main(String args[])
{
int sum=0;
int p=0;
int y;
for(int i=18; i<=23; i++)
{
sum=0;
p=0;
int[]a=new int[i/2];
for(int j=2; j<=i/2; j++)
{
if(i%j==0 && IsPrime(j)==true)
a[p++]=j;
}

System.out.println("For "+i+", the co-primes are:");
for(int x=1; x {
for(y=0; y {
if(x%a[y]==0)
break;
}
if(y==p)
{
sum+=x;
System.out.println(x);
}
}
System.out.println("Their sum is "+sum);
System.out.println("");
System.out.println("");
}
}

public static boolean IsPrime(int a)
{
a=Math.abs(a);
for(int i=2; i<=a/2; i++)
if(a%i==0)
return false;
return true;
}
}

Java Program to print the initials of a name with last name written in full

public class Initials
{
public static void initials(String S)
{
int[]A=new int[S.length()];
int sp=0;
for(int i=0; i if(S.charAt(i)==' ')
A[sp++]=i;

if(sp==0)
System.out.println(S);
else
{

System.out.print(S.charAt(0)+". ");

for(int i=0; i
System.out.print(S.charAt(A[i]+1)+". ");


System.out.print(S.substring(S.lastIndexOf(' ')+1, S.length()));
}
}
}

Friday, February 10, 2012

Java program To remove duplicates from an array of integers so that each number may appear only once

// to delete the duplicates in n array so that a number may appear only once in the array import java.io.*;
class remDup
{
    public static void main(String args[]) throws IOException
    {
       BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
       System.out.print("Enter the length of the array: ");
       int l=Integer.parseInt(br.readLine());
       int[]a=new int[l];
       for(int i=0; i<l; i++)
        {
          System.out.print("Enter the element number "+(i+1)+" of the array: ");
          a[i]=Integer.parseInt(br.readLine());
         }
      
      //now the real work begins
       
       for(int i=0; i<l; i++)
       {
           for(int j=i+1; j<l; j++)
          {
             while(a[i]==a[j])
             {
                for(int A=j; A<l-1; A++)
                 a[A]=a[A+1];
                 l--;
              }
           }
         }
// real work is done now!!!!

for(int i=0; i<l; i++)
        {
          System.out.println(a[i]);
        }
}// end of main
}// end of class
      
 

Console based two-player Cross and Noughts game in Java

import java.io.*;
public class CrossZero
{
static char[][] A=new char[3][3];




public static void main(String args[])throws IOException

{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Player 1: X");
System.out.println("Player 2: O");
for(int i=0; i<=2; i++) for(int j=0; j<=2; j++) A[i][j]='*'; Print(); for(int i=1; i<=9; i++) { System.out.println("Player "+((i%2)==1?1:2)); System.out.println("Enter Row Number: "); int r=Integer.parseInt(br.readLine()); System.out.println("Enter Column Number: "); int c=Integer.parseInt(br.readLine()); if(i%2==1) A[r][c]='X'; else A[r][c]='O'; Print(); if(i>4)
{
if(check('X')==true||check('O')==true)
{
System.out.println("Congrats, You've Won");
break;
}
if(i==9)
System.out.println("Sorry dudes! No one won..");
}

}
}

public static void Print()
{
System.out.print(" ");
for(int i=0; i<=2; i++)
System.out.print(" "+i+" ");
System.out.println("");
System.out.println("");
for(int i=0; i<=2; i++)
{ for(int j=0; j<=2; j++)
if(j==0)
System.out.print(i+" "+A[i][j]);
else
System.out.print(" "+A[i][j]);
System.out.println("");
System.out.println("");


}
System.out.println("");

System.out.println("");
System.out.println("");

}

public static boolean check(char a)
{
for(int i=0; i<=2; i++)
{
if(A[i][0]==a&&A[i][1]==a&&A[i][2]==a)
return true;

if(A[0][i]==a&&A[1][i]==a&&A[2][i]==a)
return true;


}

if(A[0][0]==a&&A[1][1]==a&&A[2][2]==a)
return true;

if(A[0][2]==a&&A[1][1]==a&&A[2][0]==a)
return true;
return false;
}
}

java program To print a Name in big letters/ To convert a name into large Letters

import java.io.*;
class better1
{
  public static void main(String args[]) throws IOException
{
 BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
 System.out.println("Enter a name:");
String S=br.readLine();
S=S.toLowerCase();
String D[][]=new String[26][8];
FileReader a=new FileReader("C:/MyFile.txt");//MyFile sent as an attachment

BufferedReader b=new BufferedReader(a);
for(int i=0; i< 26;i++)
for(int j=0; j< 8;j++)
D[i][j]=b.readLine();
for(int i=0; i<= 7; i++)
{
for(int j=0; j< S.length(); j++)
{
char ch=S.charAt(j);
int c=ch-97;
if(ch==' ')
System.out.print("--------");
else
System.out.print(D[c][i]+"-");
}
System.out.println("");
}
b.close();
a.close();
}
}


Copy the following text as it is and save it in a file named MyFile.txt in C drive.
Then run the program...

---AA---
--AAAA--
-AA--AA-
AA----AA
AAAAAAAA
AA----AA
AA----AA
AA----AA
BBBBBB--
BB---BB-
BB----BB
BB---BB-
BBBBBB--
BB----BB
BB---BB-
BBBBBB--
---CCCC-
-CC---CC
CC------
CC------
CC------
CC     
-CC---CC
---CCCC-
DDDDD---
DD--DD--
DD---DD-
DD----DD
DD----DD
DD---DD-
DD--DD--
DDDDD---
EEEEEEEE
EE------
EE------
EEEEEEE-
EE------
EE------
EE------
EEEEEEEE
FFFFFFFF
FF------
FF------
FFFFFFF-
FF------
FF------
FF------
FF------
---GGGG-
--GG--GG
-GG-----
GG------
GG--GGGG
-GG---GG
--GG--GG
---GGGG-
HH----HH
HH----HH
HH----HH
HHHHHHHH
HHHHHHHH
HH----HH
HH----HH
HH----HH
IIIIIIII
---II---
---II---
---II---
---II---
---II---
---II---
IIIIIIII
JJJJJJJJ
---JJ---
---JJ---
---JJ---
---JJ---
J--JJ---
JJ-JJ---
-JJJJ---
KK----KK
KK---KK-
KK--KK--
KKKK----
KKKK----
KK--KK--
KK---KK-
KK----KK
LL------
LL------
LL------
LL------
LL------
LL------
LL------
LLLLLLLL
MM----MM
MM----MM
M-M--M-M
M--MM--M
M------M
M------M
M------M
M------M
NN-----N
NN-----N
N-N----N
N--N---N
N---N--N
N----N-N
N-----NN
N-----NN
--OOOO--
-OO--OO-
OO----OO
OO----OO
OO----OO
OO----OO
-OO--OO-
--OOOO--
PPPPPP--
PP---PP-
PP----PP
PP---PP-
PPPPPP--
PP------
PP------
PP------
--QQQQ--
-QQ--QQ-
QQ----QQ
QQQ---QQ
QQ-Q--QQ
QQ--Q-QQ
-QQ--QQ-
--QQQQ-Q
RRRRRR--
RR---RR-
RR----RR
RR---RR-
RRRRRR--
RR---RR-
RR----RR
RR----RR
--SSSS--
-SS---S-
SS------
--SSSS--
------S-
------SS
SS---SS-
-SSSSS--
TTTTTTTT
---TT---
---TT---
---TT---
---TT---
---TT---
---TT---
---TT---
UU----UU
UU----UU
UU----UU
UU----UU
UU----UU
UU----UU
UUU--UUU
-UUUUUU-
V----VV-
V----VVV
V-----V-
V-----V-
V-----V-
-V---V--
--V-V---
---V----
WW----WW
WW----WW
WW----WW
WW----WW
WW----WW
WW-WW-WW
WWW--WWW
WW----WW
XX----XX
-XX--XX-
--XX-X--
---XX---
--XX-XX-
-XX---XX
X-----XX
X------X
YY----YY
-YY--YY-
-YY--YY-
--YY-Y--
---YY---
---YY---
---YY---
---YY---
ZZZZZZZZ
------Z-
-----Z--
----Z---
---Z----
--Z-----
-Z------
ZZZZZZZZ

Wednesday, February 8, 2012

java program To find the coprimes of a number

// coprimes of any number n are numbers bw 1 and n, which are not divisible by any factors of n
public class Coprimes
{

public static void main(String args[])
{
int sum=0;
int p=0;
int y;
for(int i=18; i<=23; i++)
{
sum=0;
p=0;
int[]a=new int[i/2];
for(int j=2; j<=i/2; j++)
{
if(i%j==0 && IsPrime(j)==true)
a[p++]=j;
}

System.out.println("For "+i+", the co-primes are:");
for(int x=1; x {
for(y=0; y {
if(x%a[y]==0)
break;
}
if(y==p)
{
sum+=x;
System.out.println(x);
}
}
System.out.println("Their sum is "+sum);
System.out.println("");
System.out.println("");
}
}

public static boolean IsPrime(int a)
{
a=Math.abs(a);
for(int i=2; i<=a/2; i++)
if(a%i==0)
return false;
return true;
}
}

java program: Recursive function to multiply two numbers

// to multiply 2 nos. using recursive functions
import java.io.*;
class recursiveMultiplication
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a number: ");
String s=br.readLine();
System.out.print("Enter another number: ");
String S=br.readLine();
int a=Integer.parseInt(s);
int b=Integer.parseInt(S);
if((a<0&&b>0)||(a>0&&b<0))
System.out.print(-Mult(Math.abs(a),Math.abs(b)));
else System.out.print(Mult(Math.abs(a),Math.abs(b)));
}
public static int Mult(int a, int b)
{
if(a==0||b==0)
return 0;
else if(b==1)
return a;

return(a+Mult(a,(b-1)));
}

}

java program To print a String in reverse order using recursion

// to print a string in reverse order using recursion
import java.io.*;
class reverseString
{
public static void main(String args[])throws IOException
{
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter a String: ");

String s=br.readLine();
reverse(s, s.length());
}

public static void reverse(String S, int a)throws IOException
{
System.out.print(S.charAt(--a));
if(a>0)
reverse(S,a);
}
}

Thursday, January 12, 2012

To Convert A Number Into Words

import java.io.*;
public class NumberNames
{
static String A[]={"","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
static String B[]={"","","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};
static String ConvertToName(int number, int divisionR, int divisionC)
{
if(number==0)return "";
String div[][]={{"","thousand","lakh","crore","arab","hundred arab"},{"","thousand","million","billion","trillion","quadrillion"}};
String s;
if(number< 20) return removeXtraSpc(A[number]+" "+div[divisionR][divisionC]+" "); s=Integer.toString(number); while(s.length()< 3) s="0"+s; int x=s.charAt(2)-48; int y=s.charAt(1)-48; int z=s.charAt(0)-48; if(y> 1) s=B[y]+" "+A[x]+" ";
else s=A[10*y+x]+" ";
if(z!=0)
s=A[z]+" Hundred "+((x!=0||y!=0)?" And ":"")+s;
return removeXtraSpc(s+" "+div[divisionR][divisionC]);
}

public static String removeXtraSpc(String s)
{
int a=0;
while(a< s.length()-1) { if(s.charAt(a)==s.charAt(a+1)&&s.charAt(a)==' ') { String S1=s.substring(0,a); String S2=s.substring(a+1,s.length()); s=S1+S2; continue; } a++; } return s; } public static void main(String aa[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String s=null; int x=0; do{ try{ System.out.println("Enter a valid Positive Integer: "); s=br.readLine(); if(s.length()>18) continue;
x=Integer.parseInt(s);
}catch(Exception e)
{
s=null;
}
}while(s==null||x< 0); if(x==0) s="Zero"; else if(x< 1000) s=ConvertToName(x,0,0); else { char c='.'; do{ System.out.println("Enter a choice: "); System.out.println("1: For Name in Indian System"); System.out.println("2: For Name in International System"); s=br.readLine(); if(s.length()!=1) continue; c=s.charAt(0); }while(c< '1'||c> '2');
s=Integer.toString(x);
c=(char)(c-49);
int a=(c==0?2:3);
int b=1;
String S=s.substring(s.length()-3,s.length());
String t=ConvertToName(Integer.parseInt(S),0,0);
if(Integer.parseInt(S)< 100) t=" and "+t; c=(char)(s.length()-3); while(c-a> 0)
{
S=s.substring(c-a,c);
t=ConvertToName(Integer.parseInt(S),a-2,b++)+" "+t;
c=(char)(c-a);
}
if(c-a<=0)
{
S=s.substring(0,c);

t=ConvertToName(Integer.parseInt(S),a-2,b++)+" "+t;
}
s=t;
}//else closes here
s=removeXtraSpc(s);
System.out.println(s);
}//main() closes here
}//class closes here