Binary to Decimal

import java.util.Scanner;

public class ConvertBinary2Decimal
{
 public static int power(int num ,int pwr)
 {
 int res = 1;
 for (int i = 1; i <= pwr; i++) 
 {
 res = res * num;
 }
 return res;
 }
 public static int B2D(int num)
 {
 int count = 0;
 int output = 0;
 while (num > 0)
 {
 int rem = num % 10;
 output = output+rem*power(2,count++);
 num = num / 10;
 }
 return output;
 }
 public static void main(String[] args)
 {
 Scanner sc = new Scanner(System.in);
 System.out.println("enter the number = ");
 int num = sc.nextInt();
 int res = B2D(num);
 System.out.println("the decimal equivalent of "+num+" = "+res);
 sc.close();
 }

}
Advertisements

Hexadecimal to Decimal Conversion

import java.util.Scanner;

public class ConvertHexa2Decimal
{
 
 public static int power(int num,int pwr)
 {
 int res=1;
 for (int i = 0; i <=pwr; i++) 
 {
 res= res*num;
 }
 return res;
 }
 
 public static int hexToDecimal(String s1)
 {
 int c= s1.length()-1;
 int out=0;
 int count =0;

 while(c>0)
 {
 char ch = s1.charAt(c);
 if(ch>=65 && ch<=70)
 {
 out = out + (ch-55)*power(16, count++);
 }
 else if(ch>=97 && ch<=102)
 {
 out = out + (ch-87)*power(16, count++);
 
 }
 else if(ch>=48 && ch<=57)
 {
 out = out + (ch-48)*power(16, count++);
 
 }
 else
 {
 System.out.println("Invalid character");
 return -1;
 } 
 }
 return out;
 }
  public static void main(String[] args) 
 {
 Scanner sc = new Scanner(System.in);
 System.out.println("Enter the number");
 String s1 = sc.next();
int out = hexToDecimal(s1);
 System.out.println(out);
 sc.close();
 }

}

Strings: Reverse Whole word

import java.util.Scanner;

public class Strings_reverseWholeWord 
{
 public static String reverse(String s1)
 {
 String s2 = "";
 char ch[] = s1.toCharArray();
 for (int i = 0; i < ch.length; i++) 
 {
 String res = "";
 while (i < ch.length && ch[i] != ' ') 
 {
 res = res+ch[i];
 i++;
 }
 s2 =res+" "+s2;
 }
 return s2;
 }

public static void main(String[] args) 
 {
 Scanner sc = new Scanner(System.in);
 System.out.println("Enter the String : ");
 String s1 = sc.nextLine(); 
 System.out.println(reverse(s1));
 sc.close();
 }
 }

Output:

Enter the String :
Welcome to the programming blog
blog programming the to Welcome

Strings: Reverse Each Word

import java.util.Scanner;

public class Strings_reverseEachWord
 {
 public static String reverse(String s1)
 {
 String s2 = "";
 char ch[] = s1.toCharArray();
 for (int i = 0; i < ch.length; i++)
 {
 String res = "";
 while (i < ch.length&& ch[i] != ' ' )
 {
 res = ch[i]+res;
 i++;
 }
 s2 =s2+res+" ";
 }
 return s2;
 }

public static void main(String[] args)
 {
 Scanner sc = new Scanner(System.in);
 System.out.println("Enter the String : ");
 String s1 = sc.nextLine();

String s2 = reverse(s1);
 System.out.println(s2);
 sc.close();
 }
 }

Output:

Enter the String :
welcome to programming blog!! hava a nice day
emoclew ot gnimmargorp !!golb avah a ecin yad

Patterns

public class Trianglepattern2 {

public static void main(String[] args) {
 int n=5;
 for(int row=0;row<n;row++)
 {
 for (int col=0; col<n;col++ )
 {
 if (row+col>=n-1)
 
 {
 System.out.print("* ");

}
 else 
 {
 System.out.print(" ");
 }
 
 }
 System.out.println();

}
 }
}

Output:-

        * 
      * * 
    * * * 
  * * * * 
* * * * *

patterns

public class Trianglepattern1 {

public static void main(String[] args) {
 int n=5;
 for(int row=0;row<n;row++)
 {
 for (int col=0; col<n;col++ )
 {
 if (row<=col)
 
 {
 System.out.print("* ");

}
 else 
 {
 System.out.print(" ");
 }
 
 }
 System.out.println();

}
 }
}

Output:-

*  *  *  *  *
*  *  *  *
*  *  *
*  *
*

Program to convert Number into words

import java.util.Scanner;

public class Strings_NumtoWords 
{
static String [] s1 = {"","","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
 
static String [] s2 = {"","one","two","three","four","five","six","seven","eight","nine","ten",
"eleven","twleve","thirteen","fourteen","fifteen","sixteen","seventeen","eightteen","nineteen"};
 
public static void concat(int num , String str)
{
 
if(num > 19) 
System.out.print(s1[num/10]+" "+s2[num%10]);
else
System.out.print(s2[num]);
if(num>0)
System.out.print(str);
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Number :- ");
int n = sc.nextInt();
concat(n/10000000 , " crore ");
concat((n/100000)%100 , " lakh ");
concat((n/1000)%100 , " thousand ");
concat((n/100)%10 , " hundred ");
concat((n)%100 , " ");
sc.close();
}

}

Program for ANAGRAM

import java.util.Scanner;

public class Strings_AnaGrams_removeSpace_sort_LowerCase_Compare
{
public static String removeSpace(String s1)
{
String res = "";
char[] ch = s1.toCharArray();
for (int i = 0; i < ch.length; i++) 
{
if (ch[i] != ' ')
{
res = res+ch[i];
}
}
return res;
}
 
public static String toLowerCase(String s1) 
{
String res = "";
char[] ch = s1.toCharArray();
for (int i = 0; i < ch.length; i++) 
{
if (ch[i] >= 65 && ch[i] <= 90) 
{
ch[i] = (char)(ch[i]+32);
}
res = res + ch[i];
}
return res;
}
 
public static String Sort(String s1)
{
char[] ch = s1.toCharArray();
String res = "";
for (int i = 0; i < ch.length-1; i++) 
{
for (int j = i+1; j < ch.length; j++) 
{
if (ch[i] > ch[j]) 
{
 
char temp = ch[i]; 
ch[i] = ch[j];
ch[j] = temp;
}
}
res = res + ch[i];
}
return res;
}
 
public static boolean compare (String s1 , String s2)
{
 
char[] c1 = s1.toCharArray();
char[] c2 = s2.toCharArray();
for (int i = 0; i < c2.length; i++) 
{
if (c1[i] != c2[i])
{
return false;
}
}
return true;
}
 
public static void main(String[] args) 
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the First String :-");
String s1 = sc.nextLine();
System.out.println("Enter the Second String :-");
String s2 = sc.nextLine();
 
System.out.println("The given Strings are :- ");
System.out.println("s1 = "+s1);
System.out.println("s2 = "+s2);
 
s1 = removeSpace(s1);
s2 = removeSpace(s2);
 
System.out.println("The Strings after removing the spaces are :- ");
System.out.println("s1 = "+s1);
System.out.println("s2 = "+s2);
 
if (s1.length() == s2.length())
{
s1 = toLowerCase(s1);
s2 = toLowerCase(s2);
//logic for Anagram
System.out.println("The Strings after Converting :- ");
System.out.println("s1 = "+s1);
System.out.println("s2 = "+s2);
 
s1 = Sort(s1);
s2 = Sort(s2);
 
System.out.println("The Strings after Sorting :- ");
System.out.println("s1 = "+s1);
System.out.println("s2 = "+s2);
 
boolean res = compare(s1,s2);
if(res)
{
System.out.println("Strings are ANAGRAM");
}
else System.out.println("Strings are not ANAGRAM");
} 
else 
{
System.out.println("The given Strings are not ANAGRAM");
}
}

}

 

Program to count Number of Alphabets ; Numbers & Special Characters!

import java.util.Scanner;
public class Strings
{
 public static void count(String s1)
 {
 int countAlpha=0; 
 for (int i = 0; i < s1.length(); i++) // avoid space at last
 {
 if(s1.charAt(i)>=65 && s1.charAt(i)<=122)
 countAlpha++; 
 }
 System.out.println("the no. of Alphabet is = "+countAlpha);
 }
 public static void countNum(String s1)
 {
 int countNume=0;
 
 for (int i = 0; i < s1.length(); i++) // avoid space at last
 {
 if(s1.charAt(i)>=48 && s1.charAt(i)<=57)
 
 countNume++; 
 }
 System.out.println("the no. of Numeric is = "+countNume);
 } 
 public static void countSpe(String s1)
 {
 int countS=0;
 
 for (int i = 0; i < s1.length(); i++) // avoid space at last
 {
 if(s1.charAt(i)>=32 && s1.charAt(i)<=47 || (s1.charAt(i)>=58 
 && s1.charAt(i)<=64) )
 countS++; 
 }
 System.out.println("the no. of Special characters is = "+countS); 
 } 
 public static void main(String[] args) 
 {
 Scanner sc = new Scanner(System.in);
 System.out.println("Enter the string");
 String str = sc.nextLine();
 count(str);
 countNum(str);
 countSpe(str);
 }

}

Output:-

Enter the string
111Welcome@**Programming**123
the no. of Alphabet is = 18
the no. of Numeric is = 6
the no. of Special characters is = 5

Program for ‘SWAP ROWS’

import java.util.Scanner;

public class Array_2D_SwapRows
{
public static int[][] swaprows(int[][] ar)
{
for (int i = 0; i < ar.length/2; i++)
{
for (int j = 0; j < ar[i].length; j++)
{
int temp = ar[i][j]; //swap the rows with the help of i and j value...
ar[i][j] = ar[ar.length-1-i][j];
ar[ar.length-1-i][j] = temp;
}
}
return ar;
}
 
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Outer Array size : ");
int a = sc.nextInt();
System.out.println("Enter the Inner Array size : ");
int b = sc.nextInt();
 
int ar[][] = new int[a][b];
for (int i = 0; i < ar.length; i++) 
{
for (int j = 0; j < ar[i].length; j++) 
{
System.out.println("enter the value ar "+i+"th row and"+j+"the column");
ar[i][j] = sc.nextInt();
}
}
 
System.out.println("The array elements are :-");
for (int i = 0; i < ar.length; i++) 
{
for (int j = 0; j < ar[i].length; j++) 
{
System.out.print(ar[i][j]+" ");
}
System.out.println();
}
int nar[][] = swaprows(ar);
System.out.println("The array elements after SWAPPING :-");
for (int i = 0; i < nar.length; i++) 
{
for (int j = 0; j < nar[i].length; j++) 
{
System.out.print(nar[i][j]+" ");
}
System.out.println();
}
}
}

Output:-

Enter the Outer Array size :
3
Enter the Inner Array size :
3
enter the value ar 0th row and0the column
1
enter the value ar 0th row and1the column
2
enter the value ar 0th row and2the column
3
enter the value ar 1th row and0the column
4
enter the value ar 1th row and1the column
5
enter the value ar 1th row and2the column
6
enter the value ar 2th row and0the column
7
enter the value ar 2th row and1the column
8
enter the value ar 2th row and2the column
9
The array elements are :-
1 2 3
4 5 6
7 8 9
The array elements after SWAPPING :-
7 8 9
4 5 6
1 2 3