Programmingbygaurav

Download as pdf or txt
Download as pdf or txt
You are on page 1of 59

//prime number #GAURAVMHASKE

public class primeno {


public static void main(String[] args) {
int a=10;
int count=0;
for(int i=1;i<=11;i++) {
if(a%i==0) {
count++;
}
}
if(count==2) {
System.out.println("prime number");
}
else if (count==1) {
System.out.println("neither prime nor composite");
}
else {
System.out.println("it is a composite number");
}
}
}

//fibonacci 1,2,3,5,8,13,21,34,55,89
public class fibonacci
{
public static void main(String[] args)
{
int a=1;
int b=2;
System.out.print(a+" "+b+" ");
for(int i=1;i<=8;i++)
{
int c=a+b;
System.out.print(c+" ");
a=b;
b=c;
}

}
}
//0,1,1,2,3,5,8.......
//0,1,2,3,4,5,6,7,8,9,10,11
//0,1,2,6,12,25........

public class fibonacciwithpos {


public static void main(String[] args)
{
int a=0;
int b=1;
int d=2;
System.out.print(a+" "+b+" ");
for(int i=1;i<=8;i++)
{
int c=a+b;
System.out.print(c*d+" ");
a=b;
b=c;
d++;
}

}
}

//lucas no.start with 2,1

public class locus


{
public static void main(String[] args)
{
int a=2;
int b=1;
System.out.print(a+" "+b+" ");
for(int i=1;i<=8;i++)
{
int c=a+b;
System.out.print(c+" ");
a=b;
b=c;
}
}
}
//perfect no.
public class perfect
{
public static void main(String[] args) {
int n=6;
int sum=0;
for(int i=1;i<6;i++)
{
if(n % i==0)
{
sum=sum+i;
}
}
if(sum==n) {
System.out.println("perfect no");
}
else
{
System.out.println("not a perfect no.");
}
}
}

//perfect no. 0 to 1000


public class perfectno
{
public static void main(String[] args)
{
int count=0;
for(int n=1;n<=1000;n++)
{

int sum=0;
for(int i=1;i<n;i++)
{
if(n%i==0)
{
sum=sum+i;
}
}

if(sum==n)
{
count++;

}
System.out.println(count);
}
}

//number with power like square,cube,....so on


public class square {
public static void main(String[] args)
{
int n=4;//number
int p=2;//power
int square=1;
for (int i=1;i<=p;i++)
{
square=square*n;
}
System.out.println(square);
}

//swapping with 3rd variable


public class swappingwith3
{
public static void main(String[] args)
{
int a=7;
int b=1;
System.out.println("before swapping");
System.out.println("a:"+a);
System.out.println("b:"+b);
int c=a;
a=b;
b=c;
System.out.println("after swapping");
System.out.println("a:"+a);
System.out.println("b:"+b);

}
}

//swapping without 3rd variable


public class swappingwithout3
{
public static void main(String[] args)
{
int a=7;
int b=1;
System.out.println("before swapping");
System.out.println("a:"+a);
System.out.println("b:"+b);
a=a+b;
b=a-b;
a=a-b;
System.out.println("after swapping");
System.out.println("a:"+a);
System.out.println("b:"+b);

}
}

//print all even digits of numbers


public class evendigitofnumber {
public static void main(String[] args)
{
int n=69388;
while(n != 0)
{
int d=n % 10;
if(d%2==0)
{
System.out.println(d);
}
n=n / 10;
}
}

}
//seperate all digit of number
public class digitno
{
public static void main(String[] args)
{
int n=69388;
while(n != 0)
{
int d=n % 10;
System.out.println(d);
n=n / 10;
}
}
}

//discount question discount=(sum of even digit)*(sum of odd digit)


public class discountquestion
{
public static void main(String[] args)
{
int n=2514795;
int sum1=0;
int sum2=0;
while(n!=0)
{
int d=n%10;
if(d%2==0)
{
sum1=sum1+d;
}
else
{
sum2=sum2+d;
}
n=n/10;
}
int discount=sum1*sum2;
System.out.println(discount);
}

//sum of all digits of a numbers


public class sumofdigit
{
public static void main(String[] args)
{
int n=694;
int sum=0;
while(n != 0)
{
int d=n % 10;
sum=sum+d;
n=n / 10;
}
System.out.println(sum);
}
}

//output 1
public class sumofdigituntilsingledigit
{
public static void main(String[] args)
{
int n=55;
int sum=0;
while(n!=0 || sum>9 )
{
if(n==0)
{
n=sum;
sum=0;
}
int d=n%10;
sum=sum+d;
n=n/10;
}
System.out.println(sum);
}
}

//reverse of number
public class reverse
{
public static void main(String[] args)
{
int n=123;
int rev=0;
while(n!=0)
{
int d=n%10;
rev=rev*10+d;
n=n/10;
}
System.out.println(rev);
}
}

//palindrome number is number if we reverse it then reverse no. equal to


original no.
public class palindrome
{
public static void main(String[] args) {
int n=121;
int rev=0;
int temp=n;
while(n!=0)
{
int d=n%10;
rev=rev*10+d;
n=n/10;
}
if(temp==rev)
{
System.out.println("no. is palindrome");
}
else
{
System.out.println("not a palindrome no.");
}
}
}

//largest number of digit


public class largestofdigit
{
public static void main(String[] args) {
int n= 123;
int c=0;
while(n!=0)
{
int d=n%10;
if(d>=c)
{
c=d;
}
n=n/10;
}
System.out.println(c);
}
}

//count all digits of no.


public class countalldigits
{
public static void main(String[] args)
{
int n=55555;
int count=0;
while(n!=0)
{
int d=n%10;
count++;
n=n/10;
}
System.out.println(count);
}
}

//strong number 145=1!+4!+5!


public class strongno
{
public static void main(String[] args)
{
int n=145;
int temp=n;
int sum=0;
while(n!=0)
{
int d=n%10;
int fact=1;
for(int i=1;i<=d;i++)
{
fact=fact*i;

}
sum=sum+fact;
n=n/10;
}
if(sum==temp)
{
System.out.println("strong no.");
}
else
{
System.out.println();
}
}
}

//strong number 145=1!+4!+5! From 1 to 1000

public class stronno1 {


public static void main(String[] args)
{
// int count=0;
for(int n=1;n<=1000;n++)
{
int temp=n;
int sum=0;
while(n!=0)
{
int d=n%10;
int fact=1;
for(int i=1;i<=d;i++)
{
fact=fact*i;

}
sum=sum+fact;
n=n/10;
}
if(sum==temp)
{
System.out.println(temp); //count++;
}
n=temp;
}
// System.out.println(count);
}}
//n%sum of digit==0 niven no.
public class nivenno
{
public static void main(String[] args) {
int n=18;
int temp=n;
int sum=0;
while(n!=0)
{
int d=n%10;
sum=sum+d;
n=n/10;
}
if(temp%sum==0)
{
System.out.println("niven no.");
}
else

{
System.out.println("not a niven no.");
}
}
}

An Armstrong number is a number such that the sum


! of its digits raised to the third power is equal to the number
! itself. For example, 371 is an Armstrong number, since
! 3**3 + 7**3 + 1**3 = 371.
//armstrong no 153=1
public class armstrong
{
public static int count(int n)
{
int count=0;
while(n!=0)
{
int d=n%10;
count++;
n=n/10;
}
return count;
}
public static int pow(int n,int p)
{
int res=1;
for(int i=1;i<=p;i++)
{
res=res*n;
}
return res;
}
public static void main(String[] args)
{
int n=153;
int temp=n;
int sum=0;
int count=count(n);
while(n!=0)
{
int d=n%10;
sum=sum+pow(d,count);
n=n/10;
}
if(temp==sum)
{
System.out.println("armstrong no.");
}
else
{
System.out.println("not an armstrong no.");
}
}
}

//armstrong 1 to 1000
public class armstrong1to1000
{

public static int count(int n)


{
int count=0;
while(n!=0)
{
int d=n%10;
count++;
n=n/10;
}
return count;
}
public static int pow(int n,int p)
{
int res=1;
for(int i=1;i<=p;i++)
{
res=res*n;
}
return res;
}

public static void main(String[] args)


{
for(int n=1;n<=1000;n++)
{
int temp=n;
int sum=0;
int count=count(n);
while(n!=0)
{
int d=n%10;
sum=sum+pow(d,count);
n=n/10;
}
if(temp==sum)
{
System.out.println(temp);
}
n=temp;
}
}
}

//decimal to binary
public class decitobi {
public static void main(String[] args)
{
int n=12;
String res="";
while(n != 0)
{
int r=n%2;
res=r+res;
n=n/2;
}
System.out.println(res );
}
}

//deci to octal
public class decitooct
{
public static void main(String[] args)
{
int n=12;
String res="";
while(n != 0)
{
int r=n%8;
res=r+res;
n=n/8;
}
System.out.println(res );
}
}

//convert decimal to hexadecial


public class decitohexa
{
public static void main(String[] args)
{
int n=12;
String res="";
while (n!=0)
{
int r=n%16;
if(n==10)
{
res="a"+res;
}
else if(n==11)
{
res="b"+res;
}
else if(n==12)
{
res="c"+res;
}
else if(n==13)
{
res="d"+res;
}
else if(n==14)
{
res="e"+res;
}
else if(n==15)
{
res="f"+res;
}
else
{
res=r+res;
}
n=n/16;
}
System.out.println(res);
}
}

//binary to decimal
public class binarytodecimal
{
public static int pow(int n,int p)
{
int res=1;
for(int i=1;i<=p;i++)
{
res=res*n;
}
return res;
}
public static void main(String[] args)
{
int n=1100;
int sum=0;
int k=0;
while(n!=0)
{
int d=n%10;
sum=sum+d*pow(2,k++);
n=n/10;
}
System.out.println(sum);

}
}

//Binary to octal
//binary to decimal
public class binarytodecimal
{
public static int pow(int n,int p)
{
int res=1;
for(int i=1;i<=p;i++)
{
res=res*n;
}
return res;
}
public static void main(String[] args)
{
int n=1100;
int sum=0;
int k=0;
while(n!=0)
{
int d=n%10;
sum=sum+d*pow(8,k++);
n=n/10;
}
System.out.println(sum);

}
}

//octal to decimal
public class octtodec
{
public static int pow(int n,int p)
{
int res=1;
for(int i=1;i<=p;i++)
{
res=res*n;
}
return res;
}
public static void main(String[] args)
{
int n=12;
int sum=0;
int k=0;
while(n!=0)
{
int d=n%10;
sum=sum+d*pow(8,k++);
n=n/10;
}
System.out.println(sum);

}
}

//binary to octal----->bi-->deci and deci--->oct


public class bitooct {
public static int pow(int n, int p) {
int res = 1;
for (int i = 1; i <= p; i++)
{
res = res * n;
}
return res;
}

public static void main(String[] args)


{
int n = 1100;
int sum = 0;
int k = 0;
while (n != 0) {
int d = n % 10;
sum = sum + d * pow(2, k++);
n = n / 10;
}
oct(sum);
}

public static void oct(int n) {


String res = "";
while (n != 0) {
int r = n % 8;
res = r + res;
n = n / 8;
}
System.out.println(res);
}
}

Array
//waptd all elements of array
public class basic
{
public static void main(String[] args)
{
int[] a= {10,20,30,40,50};
for(int i=0;i<a.length;i++)
{
System.out.println(a[i]);
}
}
}

public class basicsyntax2


{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("enter a size of an array");
int size=sc.nextInt();
int[] a=new int[size];
for (int i = 0; i < a.length; i++)
{
System.out.println("enter a element of an array");
a[i]=sc.nextInt();
}
System.out.println("array elements are");
for (int i = 0; i < a.length; i++)
{
System.out.println(a[i]);
}
}
}

//even no
public class evenno
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("enter a size of an array");
int size=sc.nextInt();
int[] a=new int[size];
for (int i = 0; i < a.length; i++)
{
System.out.println("enter a element of an array");
a[i]=sc.nextInt();
}
System.out.println("array elements are");
for (int i = 0; i < a.length; i++)
{
if(a[i]%2==0)
{
System.out.println(a[i]);
}
}
}
}

//sum of elements
public class sumofelements
{
public static void main(String[] args)
{
int[] a= {10,20,30,40,50};
int sum=0;
for(int i=0;i<a.length;i++)
{
sum=sum+a[i];
}
System.out.println(sum);
}}
// to find largest element of an array
public class largestarray
{
public static void main(String[] args)
{
int[] a= {10,80,30,60};
int large=a[0];
for (int i = 0; i < a.length; i++)
{
if(a[i]>large)
{
large=a[i];
}
}
System.out.println(large);
}
}

//find smallest element


public class smallarray
{
public static void main(String[] args)
{
int[] a= {10,80,30,60};
int small=a[0];
for (int i = 0; i < a.length; i++)
{
if(a[i]<small)
{
small=a[i];
}
}
System.out.println(small);
}
}

//secondlargerray

public class secondlargerray


{
public static void main(String[] args)
{
int[] a= {10,80,80,60,30,60};
int large=a[0];
int large2=a[0];

for (int i = 0; i < a.length; i++)


{
if(a[i]>large)
{
large=a[i];
}
for(int j=0;j<a.length;j++)
{
if(a[j]>large2 && a[j]<large )
{
large2=a[j];
}
}
}
System.out.println(large);
System.out.println(large2);
}
}

// secondlarg1

public class secondlarg1


{
public static void main(String[] args)
{
int[] a= {11,8,2,9,11,6};
int large=Integer.MIN_VALUE;
int slarge=Integer.MIN_VALUE;
for (int i = 0; i < a.length; i++)
{
if(a[i]>large)
{
slarge=large;
large=a[i];
}
else if(a[i]>slarge && a[i] != large)
{
slarge=a[i];
}
}
System.out.println(large);
System.out.println(slarge);
}
}

reverse
public class reverse
{
public static void main(String[] args)
{
int[] a= {10,20,30,40};
for(int i=a.length-1;i>=0;i--)
{
System.out.print(a[i]+" ");
}
}
}

reverse array
public class reversearray
{
public static void main(String[] args)
{
int[] a= {11,22,33,44,55};
for(int i=0;i<a.length/2;i++)
{
int temp=a[i];
a[i]=a[a.length-1-i];
a[a.length-1-i]=temp;
}
for(int i=0;i<a.length;i++)
{
System.out.print(a[i]+" ");
}
}
}

missingnoinarray
public class missingnoinarray
{
public static void main(String[] args)
{
int a[]= {6,1,5,4,2,7,9,8,10};
int n=a.length+1;
int sum1=n*(n+1)/2;
int sum2=0;
for (int i = 0; i < a.length; i++)
{
sum2=sum2+a[i];
}
System.out.println(sum1-sum2);
}
}

stringarray
public class stringarray {
public static void main(String[] args) {
String s[]= {"gaurav","hii","bye","world"};
for (int i = 0; i < s.length; i++)
{
for (int j = i+1; j < s.length; j++) {
if(s[i].compareTo(s[j])>0)//compare all element of aray to remaining ele if
result is greater than zero swap them
{
String temp=s[i];
s[i]=s[j];
s[j]=temp;

}
}
}
for (int i = 0; i < s.length; i++) {
System.out.print(s[i]+" ");
}
}
}

averageofarray
public class averageofarray
{

public static void main(String[] args)


{
int []a={1,2,3,4,5,6,7};
int sum=0;
for (int i = 0; i < a.length; i++)
{
sum=sum+a[i];
}
int avg=sum/a.length;
System.out.println(avg);
}
}

//total number of decrement required to make all the elemrnts of array divisible by
k
public class question
{
public static void main(String[] args)
{
int a[]= {5,9,12,16,25};
int k=6;
int sum=0;
for(int i=0;i<a.length;i++)
{
sum=sum+(a[i]%k);
}
System.out.println(sum);
}
}
// insert at start
package array;

public class insertatstart


{
public static void main(String[] args)
{
int a[]= {1,2,3,4};
a=add(5,a);
a=add(6,a);
a=add(7,a);
for(int i=0;i<a.length;i++)
{
System.out.print(a[i]+" ");
}
}
public static int[] add(int element,int[] a)
{
int b[]=new int[a.length+1];
b[0]=element;
for (int i = 0; i < a.length; i++)
{
b[i+1]=a[i];
}
return b;
}
}

// insert at last
public class isertatlast
{
public static void main(String[] args)
{
int a[]= {1,2,3,4};
a=add(5,a);
for (int i = 0; i < a.length; i++)
{
System.out.print(a[i]+" ");
}

}
public static int[] add(int element,int a[])
{
int b[]=new int[a.length+1];
b[0]=a[0];
for (int i = 0; i < a.length; i++)
{
b[i]=a[i];
}
b[b.length-1]=element;
return b;
}
}

// insert at middle
package array;

public class insertatmiddle


{
public static void main(String[] args)
{
int a[]= {1,2,3,4};
a=add(5,2,a);
a=add(9,6,a);
for (int i = 0; i < a.length; i++)
{
System.out.println(a[i]);
}
}
public static int[] add(int element,int index,int a[])
{

if(index<0 || index>a.length)
{
System.out.println("index is not present");
return a;
}
int b[]=new int[a.length+1];
b[index]=element;
for (int i = 0; i < a.length; i++)
{
if(i<index)
{
b[i]=a[i];
}
else
{
b[i+1]=a[i];
}
}
return b;
}
}

multidimensionalarray

public class basic


{
public static void main(String[] args)
{
int [][]a= {{1,2,3},{4,5,6},{7,8,9}};
System.out.println(a);
System.out.println(a[0]);
System.out.println(a[0][2]);
for (int i = 0; i < a.length; i++)
{
for (int j = 0; j < a.length; j++)
{
System.out.print(a[i][j]+" ");}}}}
public class basic2
{
public static void main(String[] args)
{
int [][]a= {{1,2},{3,4,5},{6,7,8}};
for (int i = 0; i < a.length; i++)
{
for (int j = 0; j < a[i].length; j++)
{
System.out.print(a[i][j]+" ");
}
}
}
}

//wap to print all the elements of array in matrix form.


public class matrixform
{
public static void main(String[] args)
{
int a[][]= {{1,2,3},{4,5,6},{7,8,9}};
for (int i = 0; i < a.length; i++)
{
for (int j = 0; j < a.length; j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
}

//question

public class question


{
public static void main(String[] args)
{
int a[][]= {{11,22,33},{44,55,66},{77,88,99}};
int x=55;
int R=0;
int C=0;
for (int i = 0; i < a.length; i++)
{
for (int j = 0; j < a[i].length; j++)
{
if(x==a[i][j])
{
R=i;
C=j;
}
}
}
int sum=0;
if((R+C)%2==0){
for (int i = 0; i < a.length; i++)
{
for (int j = 0; j < a[i].length; j++)
{
if(a[i][j]%2==0) {
sum=sum+sumofdigits(a[i][j]);
}
}
}
System.out.println(sum);
}else {
for (int i = 0; i < a.length; i++)
{
for (int j = 0; j < a[i].length; j++)
{
if(a[i][j]%2!=0) {
sum=sum+sumofdigits(a[i][j]);
}
}
}
System.out.println(sum);
}
}
public static int sumofdigits(int n)
{
int sum=0;
while(n!=0)
{
int d=n%10;
sum=sum+d;
n=n/10;
}
return sum;}}
largeelefromeacharray2d

public class largeelefromeacharray2d


{
public static void main(String[] args)
{
int a[][]= {{400,12,3},{9,25,16},{10,8,19}};
for (int i = 0; i < a.length; i++)
{
int large=a[i][0];
for (int j = 0; j < a[i].length; j++)
{
if(a[i][j]>large)
{
large=a[i][j];
}
}
System.out.println(large);
}
}
}

diagonalsum
public class diagonalsum
{
public static void main(String[] args)
{
int a[][]= {{1,2,3},{4,5,6},{7,8,9}};
int sum=0;
for (int i = 0; i < a.length; i++)
{
for (int j = 0; j < a[i].length; j++)
{
if(i==j)
{
sum=sum+a[i][j];
}
}
}
System.out.println(sum);
}
}
addlarge
public class addlarge
{
public static void main(String[] args)
{
int a[][]= {{1,2,3},{4,5,6},{7,8,9}};
int b[][]= {{1,2,1},{1,3,1},{1,0,0}};

for (int i = 0; i < b.length; i++)


{
int large=0;
for (int j = 0; j < b.length; j++)
{
int d=a[i][j]+b[i][j];
if(a[i][j]>0)
{
large=a[i][j];
}

}
System.out.println(large);
}
}
}

linearsearch
public class linearsearch
{
public static void main(String[] args)
{
int[] a = {5,4,3,9,2,1};
int search=1;
boolean b=false;
for (int i = 0; i < a.length; i++)
{
if (a[i]==search)
{
System.out.println("the element is present at index:" + i);
b=true;
break;
}

}
if(b==false)
{
System.out.println("the element is not present");
}
}
}

binarysearch
public class binarysearch
{
public static void main(String[] args)
{
int a[]= {1,2,3,4,5};
int search=11;
int LI=0;
int HI=a.length-1;
int MI=(LI+HI)/2;
while(LI<=HI)
{
if(search==a[MI])
{
System.out.println(MI);
break;
}
else if(search>a[MI])
{
LI=MI+1;
}
else if(search<a[MI])
{
HI=MI-1;
}
MI=(LI+HI)/2;
}
if(LI>HI)
{
System.out.println("the element is not present");
}
}
}
bubblesorting
public class bubblesorting
{
public static void main(String[] args)
{
int a[]= {5,3,1,2,4};
for (int i = 0; i < a.length; i++)
{
for (int j = i+1; j < a.length; j++)
{
if(a[i]>a[j])
{
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for (int i = 0; i < a.length; i++)
{
System.out.print(a[i]+" ");
}
}
}

//sort character according to ascending order


public class sortcharname
{
public static void main(String[] args)
{
char []a= {'g','a','u','r','a','v'};
for (int i = 0; i < a.length; i++)
{
for (int j = i+1; j<a.length; j++)
{
if(a[i]>a[j])
{
char temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for (int i = 0; i < a.length; i++)
{
System.out.print(a[i]+" ");
}
}
}

//wap to display string array in lexographical order


public class sortalphabetically
{
public static void main(String[] args)
{
String[] a= {"goa","manali","sikkim","kerala","maldives"};
for (int i = 0; i < a.length; i++)
{
for (int j =i+1; j < a.length; j++)
{
if(a[i].compareTo(a[j])>0)
{
String temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for (int i = 0; i < a.length; i++)
{
System.out.print(a[i]+" ");
}
}
}

//insertion order
public class insertionsorting
{
public static void main(String[] args)
{
int a[]= {4,2,1,5,3};
for (int i = 0; i < a.length; i++)
{
int temp=a[i];
int j=i;
while(j>0 && a[j-1]>temp)
{
a[j]=a[j-1];
j=j-1;
}
a[j]=temp;
}
for (int i = 0; i < a.length; i++)
{
System.out.print(a[i]+" ");
}
}
}

Selection sort
public class selectionsort
{
public static void main(String[] args)
{
int a[]= {3,5,1,2,4};
for (int i = 0; i < a.length; i++)
{
int min=i; //to store the index of smallest element
for (int j =i+1; j < a.length; j++)
{
if(a[j]<a[min])
{
min=j;
}
}
int temp=a[i];
a[i]=a[min];
a[min]=temp;
}
for (int i = 0; i < a.length; i++)
{
System.out.print(a[i]+" ");
}
}
}

//all the pairs of a number whose sum=given no.


public class pairsum
{
public static void main(String[] args)
{
int a[]= {5,3,1,4,2};
int k=5;
for (int i = 0; i < a.length; i++)
{
for (int j =i+1; j < a.length; j++)
{
if(a[i]+a[j]==k)
{
System.out.println(a[i]+" "+a[j]);
}
}
}
}
}

//occurence of all the numbers


public class occurence
{
public static void main(String[] args)
{
int a[]= {1,5,3,1,4,2,5,2};
for (int i = 0; i < a.length; i++)
{
int occ=1;
for (int j =i+1; j < a.length; j++)
{
if(a[i]==a[j])
{
occ++;
}
}

System.out.println("occurence of "+a[i]+" is "+occ);


}
}
}

reverseofstring
public class reverseofstring
{
public static void main(String[] args)
{
String s="apple";
String rev="";
for (int i =s.length()-1; i>=0; i--)
{
rev=rev+s.charAt(i);
}
System.out.println(rev);
}
}

palindrome
public class palindrome
{
public static void main(String[] args)
{
String s="level";
String rev="";
for (int i =s.length()-1; i>=0; i--)
{
rev=rev+s.charAt(i);
}
if(s.equals(rev))
{
System.out.println("string is a palindrome");
}
else {
System.out.println("string is not a palindrome");
}
}
}

//trim() will ignore the first and last space ....equalsignorecase() will ignore
case
//sensitiveness property
public class palindrome2
{
public static void main(String[] args)
{
String s="LeVEl ";
s=s.trim();
String rev="";
for (int i =s.length()-1; i>=0; i--)
{
rev=rev+s.charAt(i);
}
if(s.equalsIgnoreCase(rev))
{
System.out.println("string is a palindrome");
}
else {
System.out.println("string is not a palindrome");
}
}
}

//swap string
public class p1
{
public static void main(String[] args)
{
String a="Go";
String b="Away";
System.out.println("before swapping");
System.out.println("a-"+a);
System.out.println("b-"+b);
a=a.concat(b);
b=a.substring(0,a.length()-b.length());
a=a.substring(b.length());
System.out.println("after swapping");
System.out.println("a-"+a);
System.out.println("b-"+b);
}
}

//wap to count all the vowels in string 3


public class vowels
{
public static void main(String[] args)
{
String s="education is imp";
int count=0;
for (int i = 0; i < s.length(); i++)
{

if(s.charAt(i)=='a'||s.charAt(i)=='e'||s.charAt(i)=='i'||s.charAt(i)=='o'||s.charAt(i)=='u')
{
count++;
}
}
System.out.println(count);}}
//wap to find duplicate characters in a string
public class duplcharinstr
{
public static void main(String[] args)
{
String s="maharashtra";
char[] c=s.toCharArray();
boolean[] b=new boolean[c.length];
for (int i = 0; i < c.length; i++)
{
if(b[i]==false) {
int count=1;
for (int j =i+1; j < c.length; j++)
{
if(c[i]==c[j])
{
count++;
b[j]=true;
}
}
if(count>1)
{
System.out.print(c[i]);
}
}
}
}
}

//occurence of each char in string


public class occeachchar
{
public static void main(String[] args)
{
String s="maharashtra";
char[] c=s.toCharArray();
boolean[] b=new boolean[c.length];
for (int i = 0; i < c.length; i++)
{
if(b[i]==false) {
int count=1;
for (int j =i+1; j < c.length; j++)
{
if(c[i]==c[j])
{
count++;
b[j]=true;
}
}
System.out.println(c[i]+" "+count);
}
}
}
}

Convert low to upp


public class convertlowtoupp
{
public static void main(String[] args)
{
String s="jAva123...";
char[] c=s.toCharArray();
for (int i = 0; i < c.length; i++)
{
if(c[i]>='a' && c[i]<='z')
{
c[i]=(char)(c[i]-32);
}
}
s=new String(c);
System.out.println(s);
}
}

Convert low to upp vice versa


public class uptolowandlowtoup
{
public static void main(String[] args)
{
String s="jAva123...";
char[] c=s.toCharArray();
for (int i = 0; i < c.length; i++)
{
if(c[i]>='a' && c[i]<='z')
{
c[i]=(char)(c[i]-32);
}
else if(c[i]>='A' && c[i]<='Z')
{
c[i]=(char)(c[i]+32);
}

}
s=new String(c);
System.out.println(s);

}
}

SWAP WORD
public class SWAPWORD
{
public static void main(String[] args)
{
String s="welcome to thane";
String[] a=s.split(" ");
for (int i = a.length-1; i >=0;i--)
{
System.out.print(a[i]+" ");
}
}
}

//count all special character 4


public class question
{
public static void main(String[] args)
{
String s="Gaurav@**3";
int count=0;
for (int i = 0; i < s.length(); i++)
{
if((s.charAt(i)>='A' && s.charAt(i)<='Z') || (s.charAt(i)>='a' &&
s.charAt(i)<='z')
|| (s.charAt(i)>='0' && s.charAt(i)<='9')||s.charAt(i)==' ')
{
count++;
}
}
System.out.println(s.length()-count);
}
}
//waptf smallest string in an array
public class question1
{
public static void main(String[] args)
{
String s[]= {"goa","manali","ab","maldives"};
String min=s[0];
for (int i = 1; i < s.length; i++)
{
{
if(s[i].length()<min.length())
{
min=s[i];
}
}
}
System.out.println(min);
}
}

//waptf all duplicates words present in a string along with total duplicate in
the string
public class question3
{
public static void main(String[] args)
{
String s="java is a programming language it is used for programming";
s=s.toLowerCase();
String[] a=s.split(" ");
int duplicatevalue=0;
boolean[] b=new boolean[a.length];
for (int i = 0; i < b.length; i++)
{
if(b[i]==false) {
int count=1;
for (int j =i+1; j < b.length; j++)
{
if(a[i].equals(a[j])) {
count++;
b[j]=true;
}
}
if(count>1)
{
System.out.print(a[i]+" ");
duplicatevalue++;
}
}
}
System.out.println(duplicatevalue);
}
}

*****
*****
*****
*****
*****
public class p1
{
public static void main(String[] args)
{
int n=5;
for(int r=1;r<=n;r++)
{
for(int c=1;c<=5;c++)
{
System.out.print("* ");
}
System.out.println(" ");
}
}
}

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

public class p2
{
public static void main(String[] args)
{
for(int r=1;r<=5;r++)
{
for(int c=1;c<=5;c++)
{
if(true) {
System.out.print("* ");
}
else {
System.out.println(" ");
}
}
System.out.println();
}
}
}

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

public class p3
{
public static void main(String[] args)
{
for(int r=1;r<=5;r++)
{
for(int c=1;c<=5;c++)
{
if(r==1||r==5||c==1||c==5) {
System.out.print("* ");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
}
}
*
*
*****
*
*

public class p4
{
public static void main(String[] args)
{
for(int r=1;r<=5;r++)
{
for(int c=1;c<=5;c++)
{
if(r==3||c==3) {
System.out.print("* ");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
}
}

*****
* * *
*****
* * *
*****
public class p5
{
public static void main(String[] args)
{
for(int r=1;r<=5;r++)
{
for(int c=1;c<=5;c++)
{
if(r==1||r==3||r==5||c==1||c==3||c==5) {
System.out.print("* ");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
}
}

*
*
*
*
*

public class p6
{
public static void main(String[] args) {

for(int r=1;r<=5;r++)
{
for(int c=1;c<=5;c++)
{
if(r==c) {
System.out.print("* ");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
}
}
*****
** **
* * *
** **
*****
public class p7
{
public static void main(String[] args) {

for(int r=1;r<=5;r++)
{
for(int c=1;c<=5;c++)
{
if(r==1||r==5||r==c||r+c==6||c==1||c==5) {
System.out.print("* ");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
}
}

*******
* *
* *
*******
*
*
*
public class p8
{
public static void main(String[] args) {
int n=7;
for(int r=1;r<=n;r++)
{
for(int c=1;c<=n;c++)
{

if(r==1||r==n/2+1||(c==1&&r<=n/2+1)||(c==n&&r<=n/2+1)||(c==n/2+1&&r>=n/2+1)) {
System.out.print("* ");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
}
}

*
*
*
*******
* *
* *
*******
public class p9
{
public static void main(String[] args) {
int n=7;
for(int r=1;r<=n;r++)
{
for(int c=1;c<=n;c++)
{

if(r==n||r==n/2+1||(c==1&&r>=n/2+1)||(c==n&&r>=n/2+1)||(c==n/2+1&&r<=n/2+1))
{
System.out.print("* ");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
}
}

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

public class p10


{
public static void main(String[] args)
{
for(int r=1;r<=5;r++)
{
for(int c=1;c<=5;c++)
{
if(r>=c)
{
System.out.print("* ");
}
else {
System.out.print(" ");
}
}
System.out.println();
}
}
}

*****
****
***
**
*
public class p11 {
public static void main(String[] args)
{
for(int r=1;r<=5;r++)
{
for(int c=1;c<=5;c++)
{
if(r<=c)
{
System.out.print("* ");
}
else {
System.out.print(" ");
}
}
System.out.println();
}
}
}

*****
****
***
**
*
public class p13 {
public static void main(String[] args)
{
int n=5;
for(int r=1;r<=n;r++ )
{
for(int c=1;c<=n;c++)
{
if(r+c<=n+1)
{
System.out.print("* ");
}
else {
System.out.print(" ");
}
}
System.out.println();
}}}
1
12
123
1234
12345
public class p14
{
public static void main(String[] args)
{
int n=5;
for(int r=1;r<=n;r++ )
{
for(int c=1;c<=n;c++)
{
if(r>=c)
{
System.out.print(c+" ");
}
else {
System.out.print(" ");
}
}
System.out.println();
}
}
}

1
22
333
4444
55555

public class p15 {


public static void main(String[] args)
{
int n=5;
for(int r=1;r<=n;r++ )
{
for(int c=1;c<=n;c++)
{
if(r>=c)
{
System.out.print(r+" ");
}
else {
System.out.print(" ");
}
}
System.out.println();
}
}
}

1
23
456
7 8 9 10
11 12 13 14 15
public class p16
{
public static void main(String[] args)
{
int n=5;
int k=1;
for(int r=1;r<=n;r++ )
{
for(int c=1;c<=n;c++)
{
if(r>=c)
{
System.out.print(k++ +" ");
}
else {
System.out.print(" ");
}
}
System.out.println();}}}
GGGGGGG
G
G
G GGGG
G G
G G
GGGGGGG
public class p17
{
public static void main(String[] args) {
int n=7;
for(int r=1;r<=n;r++)
{
for(int c=1;c<=n;c++)
{
if(r==1||c==1||r==n||(c==n&&r>=n/2+1)||r==n/2+1&&c>=n/2+1) {
System.out.print("G ");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
}
}

*
***
*****
public class P18 {
public static void main(String[] args)
{
int n=5;
for(int r=0;r<=5;r++)
{
for(int c=0;c<=5;c++)
{
if(r>=c && r+c>=n+1)
{
System.out.print("* ");
}
else {
System.out.print(" ");
}
}
System.out.println();
}
}
}

*
***
*****
*******
*********
public class p19
{
public static void main(String[] args)
{
int n=5;
int z=1;
for(int i=1;i<=n;i++)//outer loop
{
for(int j=n-1;j>=i;j--)//space loop
{
System.out.print(" ");
}
for(int k=1;k<=z;k++)//print loop
{
System.out.print("* ");
}
z=z+2;
System.out.println();
}
}
}
1
222
33333
4444444
555555555

public class p20 {


public static void main(String[] args)
{
int n=5;
int z=1;
for(int i=1;i<=n;i++)//outer loop
{
for(int j=n-1;j>=i;j--)//space loop
{
System.out.print(" ");
}
for(int k=1;k<=z;k++)//print loop
{
System.out.print(i+" ");
}
z=z+2;
System.out.println();
}
}
}

1
123
12345
1234567
123456789

public class p21 {


public static void main(String[] args)
{
int n=5;
int z=1;
for(int i=1;i<=n;i++)//outer loop
{
for(int j=n-1;j>=i;j--)//space loop
{
System.out.print(" ");
}
for(int k=1;k<=z;k++)//print loop
{
System.out.print(k+" ");
}
z=z+2;
System.out.println();
}
}
}

1
333
55555
7777777
999999999

public class p22 {


public static void main(String[] args)
{
int n=5;
int z=1;
for(int i=1;i<=n;i++)//outer loop
{
for(int j=n-1;j>=i;j--)//space loop
{
System.out.print(" ");
}
for(int k=1;k<=z;k++)//print loop
{
System.out.print(z+" ");
}
z=z+2;
System.out.println();
}
}
}
1
321
54321
7654321
987654321

public class p23 {


public static void main(String[] args)
{
int n=5;
int z=1;
for(int i=1;i<=n;i++)//outer loop
{
for(int j=n-1;j>=i;j--)//space loop
{
System.out.print(" ");
}
for(int k=z;k>=1;k--)//print loop
{
System.out.print(k+" ");
}
z=z+2;
System.out.println();
}
}

1
212
32123
4321234
543212345

public class p24 {


public static void main(String[] args)
{
int n=5;
int z=1;
for(int i=1;i<=n;i++)//outer loop
{
for(int j=n-1;j>=i;j--)//space loop
{
System.out.print(" ");
}
for(int k=1;k<=z;k++)//print loop
{
System.out.print((Math.abs(k-i)+1)+" ");
}
z=z+2;
System.out.println();
}
}

1
121
12321
1234321
123454321
public class p25 {
public static void main(String[] args)
{
int n=5;
int z=1;
for(int i=1;i<=n;i++)//outer loop
{
for(int j=n-1;j>=i;j--)//space loop
{
System.out.print(" ");
}
for(int k=i-1;k>=(-(i-1));k--)//print loop
{
System.out.print((i-Math.abs(k))+" ");
}
z=z+2;
System.out.println();
}}}

ANAGRAM
public class anagram {
public static void main(String[] args) {
String s1="cat";
String s2="act";
if(s1.length()==s2.length()) {
char[] c1=s1.toCharArray();
char[] c2=s2.toCharArray();
sort(c1);
sort(c2);
boolean b=true;
for (int i = 0; i < c2.length; i++) {
if(c1[i]!=c2[i]) {
b=false;
break;
}
}
if(b==true) {
System.out.println("string is anagram");
}
else {
System.out.println("string is not anagram");
}
}else {
System.out.println("string is not anagram");
}
}
public static void sort(char[] c) {
for (int i = 0; i < c.length; i++) {
for (int j =i+1; j < c.length; j++) {
if(c[i]>c[j]) {
char temp=c[i];
c[i]=c[j];
c[j]=temp;
}
}
}
}
}
PASSWORD IS WEAK OR STRONG
public class password {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
boolean upper=false;
boolean lower=false;
boolean numeric=false;
boolean special=false;
for (int i = 0; i <s.length(); i++) {
if(s.charAt(i)>='A' && s.charAt(i)<='Z') {
upper=true;
}else if(s.charAt(i)>='a' && s.charAt(i)<='z') {
lower=true;

}else if(s.charAt(i)>='0' && s.charAt(i)<='9') {


numeric=true;
}else if
(s.charAt(i)=='@'||s.charAt(i)<='#'||s.charAt(i)=='$'||s.charAt(i)<='%'||s.charAt(i)=='&'
||s.charAt(i)=='*'||s.charAt(i)=='+'||s.charAt(i)=='-') {
special=true;
}
}
if(s.length()>=8 && upper && lower && numeric && special==true) {
System.out.println("strong password");
}else {
System.out.println("weak password");
}
}
}

You might also like