Skip to content

Commit 940d639

Browse files
authored
Add files via upload
1 parent f99a262 commit 940d639

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1564
-0
lines changed

Bit_Manipulation/BitOperations.java

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package Bit_Manipulation;
2+
class BitOperations{
3+
4+
public static void main(String[] args){
5+
6+
int a=15; //0000 1111
7+
int b=21; //0001 0101
8+
9+
//AND Operation
10+
/*
11+
0000 1111
12+
& 0001 0101
13+
--------------
14+
0000 0101
15+
*/
16+
int f=(a&(a-1));
17+
System.out.println("Power of 2 Operation : "+f+" Binary value: "+Integer.toBinaryString(f));
18+
19+
int and=a&b;
20+
System.out.println("AND Operation : "+and+" Binary value: "+Integer.toBinaryString(and));
21+
//OR Operation
22+
/*
23+
0000 1111
24+
| 0001 0101
25+
--------------
26+
0001 1111
27+
*/
28+
int or=a|b;
29+
System.out.println("OR Operation : "+or+" Binary value: "+Integer.toBinaryString(or));
30+
//XOR Operation
31+
/*
32+
0000 1111
33+
& 0001 0101
34+
--------------
35+
0001 1010
36+
*/
37+
int xor=a^b;
38+
System.out.println("XOR Operation : "+xor+" Binary value: "+Integer.toBinaryString(xor));
39+
int x=7;
40+
//NOT Operation
41+
int not=~x;
42+
System.out.println("NOT Operation : "+not+" Binary value: "+Integer.toBinaryString(not));
43+
44+
}
45+
}

Bit_Manipulation/BitShift.java

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package Bit_Manipulation;
2+
3+
import java.util.Scanner;
4+
5+
class BitShift{
6+
7+
public static void main(String[] args){
8+
9+
Scanner sc=new Scanner(System.in);
10+
System.out.print("Enter a decimal number: ");
11+
int x=sc.nextInt();
12+
int a=15; //0000 1111
13+
int b=22; //0001 0101
14+
int c=24;
15+
System.out.println("Your numbers "+x+" binary value: "+Integer.toBinaryString(x));
16+
System.out.println(a+" Binary value: "+Integer.toBinaryString(a));
17+
System.out.println(b+" Binary value: "+Integer.toBinaryString(b));
18+
System.out.println(c+" Binary value: "+Integer.toBinaryString(c));
19+
System.out.println("------------------------------------------");
20+
int lshift=a<<1;
21+
System.out.println("left shift Operation : "+lshift+"\t\t Binary value: "+Integer.toBinaryString(lshift));
22+
23+
int rshift=b>>1;
24+
System.out.println("Right shift Operation : "+rshift+"\t\t Binary value: "+Integer.toBinaryString(rshift));
25+
26+
int zshift=c>>>1;
27+
System.out.println("Right shift zero fill Operation : "+zshift+"\t Binary value: "+Integer.toBinaryString(zshift));
28+
29+
System.out.println("------------------------------------------");
30+
int l=x<<1;
31+
System.out.println("left shift Operation on your number: "+l+"\t\t Binary value: "+Integer.toBinaryString(l));
32+
int r=x>>1;
33+
System.out.println("Right shift Operation on your number: "+r+"\t\t Binary value: "+Integer.toBinaryString(r));
34+
int rz=x>>>1;
35+
System.out.println("Right shift zero fill Operation on your number: "+rz+"\t\t Binary value: "+Integer.toBinaryString(rz));
36+
System.out.println("-------------------------------------------");
37+
38+
sc.close();
39+
}
40+
}

Bit_Manipulation/PowerOfNumber.java

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package Bit_Manipulation;
2+
3+
import java.util.Scanner;
4+
5+
public class PowerOfNumber {
6+
7+
public static void main(String[] args){
8+
Scanner sc=new Scanner(System.in);
9+
// System.out.print("Enter the Number: ");
10+
// int n=sc.nextInt();
11+
System.out.print("Upto: ");
12+
int x=sc.nextInt();
13+
for(int i=0;i<=x;i++){
14+
int t=2<<i;
15+
System.out.print(t+" ");
16+
}
17+
sc.close();
18+
}
19+
}

Bit_Manipulation/checPositiveNum.java

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package Bit_Manipulation;
2+
3+
import java.util.Scanner;
4+
5+
public class checPositiveNum {
6+
7+
public static void main(String[] args) {
8+
9+
Scanner sc = new Scanner(System.in);
10+
System.out.print("Enter the Number: ");
11+
int n = sc.nextInt();
12+
if (n >> 31 == 0)
13+
System.out.println("Positive Number!");
14+
else
15+
System.out.println("Negative Number!");
16+
sc.close();
17+
}
18+
}

Bit_Manipulation/checkBitSet.java

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package Bit_Manipulation;
2+
3+
import java.util.Scanner;
4+
5+
public class checkBitSet {
6+
7+
public static void main(String... args){
8+
Scanner sc=new Scanner(System.in);
9+
System.out.print("Enter the Number: ");
10+
int num=sc.nextInt();
11+
System.out.print("Position to check: ");
12+
int pos=sc.nextInt();
13+
System.out.println("Your number is: "+Integer.toBinaryString(num));
14+
if(!checkBit(num,pos)){
15+
System.out.println("The "+pos+" bit is set(or 1). ");
16+
}else{
17+
System.out.println("The "+pos+" bit is not set(or 0).");
18+
}
19+
sc.close();
20+
}
21+
static boolean checkBit(int n,int p){
22+
boolean res=((n&(1<<p))==0)?true:false;
23+
return res;
24+
}
25+
26+
}

Bit_Manipulation/countNumOfOne.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package Bit_Manipulation;
2+
3+
import java.util.Scanner;
4+
5+
public class countNumOfOne {
6+
public static void main(String... args){
7+
Scanner sc=new Scanner(System.in);
8+
System.out.println("Enter the Number: ");
9+
int num=sc.nextInt();
10+
System.out.print("The Number of one's in : "+num+" ("+Integer.toBinaryString(num)+")");
11+
int result=countOne(num);
12+
System.out.println(" is : "+result);
13+
sc.close();
14+
}
15+
static int countOne(int n){
16+
int count=0;
17+
while(n>0){
18+
n=n&(n-1);
19+
count++;
20+
}
21+
return count;
22+
}
23+
}

Bit_Manipulation/lowerToUpper.java

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package Bit_Manipulation;
2+
3+
import java.util.Scanner;
4+
5+
public class lowerToUpper {
6+
7+
public static void main(String[] args){
8+
Scanner sc=new Scanner(System.in);
9+
System.out.print("Enter the lowercase Character: ");
10+
char ch=sc.next(".").charAt(0); //it will take only one character(Strict)
11+
// char c=sc.findInLine(".").charAt(0); //it will take first line from the characters
12+
int n=(int)ch;
13+
n=(n&'_');
14+
System.out.println("Your char '"+ch+"' in UpperCase : "+(char)n);
15+
16+
sc.close();
17+
18+
}
19+
}

Bit_Manipulation/oddEven.java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package Bit_Manipulation;
2+
3+
import java.util.Scanner;
4+
5+
public class oddEven {
6+
public static void main(String[] args){
7+
Scanner sc=new Scanner(System.in);
8+
System.out.print("Enter a number to check: ");
9+
int x=sc.nextInt();
10+
if(checkOddEven(x)){
11+
System.out.println(x+" is a Even Number. ");
12+
}else{
13+
System.out.println(x+" is a Odd Number. ");
14+
}
15+
sc.close();
16+
}
17+
static boolean checkOddEven(int num){
18+
return ((num & 1)==0);
19+
}
20+
21+
}

Bit_Manipulation/powerOfTwo.java

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package Bit_Manipulation;
2+
3+
import java.util.Scanner;
4+
5+
public class powerOfTwo {
6+
public static void main(String[] args){
7+
Scanner sc=new Scanner(System.in);
8+
System.out.print("Enter a number to check: ");
9+
int x=sc.nextInt();
10+
11+
if(isPowerOfTwo(x)){
12+
System.out.println("Power of two!");
13+
14+
}else{
15+
System.out.println("Not a power of two!!");
16+
}
17+
sc.close();
18+
}
19+
static boolean isPowerOfTwo(int n){
20+
return (n>0)&&((n&(n-1))==0);
21+
}
22+
23+
24+
}

Bit_Manipulation/subsetOfSet.java

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package Bit_Manipulation;
2+
3+
import java.util.Scanner;
4+
5+
/*
6+
* Generate possible Subsets for given set.
7+
*
8+
* */
9+
10+
public class subsetOfSet {
11+
public static void main(String... args) {
12+
Scanner sc = new Scanner(System.in);
13+
// char ch[]={'a','b','c','d'};
14+
System.out.print("How many element in set? ");
15+
int n = sc.nextInt();
16+
char[] ch = new char[n];
17+
System.out.print("Enter the char elements: ");
18+
for (int k = 0; k < n; k++) {
19+
char c = sc.next().charAt(0);
20+
ch[k] = c;
21+
}
22+
subsetOfSet.possibleSubset(ch, n);
23+
sc.close();
24+
}
25+
26+
static void possibleSubset(char[] c, int n) {
27+
28+
for (int i = 0; i < (1 << n); ++i) {// here i loop until it reaches
29+
// 2powN (1<<n). bcz
30+
// there are 2powN subset for a set
31+
// with N element.
32+
System.out.print("{");
33+
for (int j = 0; j < n; j++) {
34+
if ((i & (1 << j)) != 0) {
35+
System.out.print(c[j] + " ");
36+
}
37+
}
38+
System.out.print("}, ");
39+
}
40+
}
41+
42+
}

Bit_Manipulation/upperToLower.java

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package Bit_Manipulation;
2+
3+
import java.util.Scanner;
4+
5+
public class upperToLower {
6+
7+
public static void main(String[] args){
8+
Scanner sc=new Scanner(System.in);
9+
System.out.print("Enter the UpperCase Character: ");
10+
char ch=sc.next(".").charAt(0); //it will take only one character(Strict)
11+
int n=(int)ch;
12+
n=(n|' ');
13+
System.out.println("Your char '"+ch+"' in Lowercase : "+(char)n);
14+
sc.close();
15+
}
16+
}

Patterns/Mantry.java

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package Patterns;
2+
3+
public class Mantry {
4+
5+
static void patternPrint(int n) {
6+
int c=1;
7+
for(int i=0;i<n;i++)
8+
System.out.print(n);
9+
System.out.println();
10+
for(int i=0;i<n;i++) {
11+
for(int j=0;j<n;j++) {
12+
if(j==n/2) {
13+
System.out.print(c++);
14+
}else {
15+
System.out.print(n);
16+
}
17+
}
18+
System.out.println();
19+
}
20+
}
21+
22+
public static void main(String[] args) {
23+
patternPrint(5);
24+
}
25+
26+
}

Patterns/Pascal.java

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package Patterns;
2+
import java.util.Scanner;
3+
4+
public class Pascal {
5+
6+
public static void main(String[] args) {
7+
int bin, p, q, r, x;
8+
Scanner s = new Scanner(System.in);
9+
System.out.println("How Many Row Do you want to input: ");
10+
r = s.nextInt();
11+
bin = 1;
12+
q = 0;
13+
System.out.println("Pascal's Triangle: ");
14+
while (q < r) {
15+
for (p = 40 - 3 * q; p > 0; --p)
16+
System.out.print(" ");
17+
for (x = 0; x <= q; ++x) {
18+
if ((x == 0) || (q == 0))
19+
bin = 1;
20+
else
21+
bin = (bin * (q - x + 1)) / x;
22+
System.out.print(" ");
23+
System.out.print(bin);
24+
}
25+
26+
System.out.println("");
27+
++q;
28+
}
29+
s.close();
30+
}
31+
}

Patterns/Pattern01sc.java

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package Patterns;
2+
3+
import java.util.Scanner;
4+
/*
5+
* 1
6+
0 1
7+
1 0 1
8+
0 1 0 1
9+
1 0 1 0 1
10+
0 1 0 1 0 1
11+
1 0 1 0 1 0 1
12+
*
13+
*/
14+
public class Pattern01sc {
15+
16+
public static void main(String[] args) {
17+
Scanner sc = new Scanner(System.in);
18+
System.out.println("How many rows you want in this pattern?");
19+
int rows = sc.nextInt();
20+
String str = "";
21+
for (int i = 1; i <= rows; i++) {
22+
str = (i % 2) + " " + str;
23+
System.out.println(str);
24+
}
25+
sc.close();
26+
}
27+
}

0 commit comments

Comments
 (0)