Programmingbygaurav
Programmingbygaurav
Programmingbygaurav
//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........
}
}
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);
}
}
}
}
}
}
}
//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;
}
}
}
//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);
}
}
}
sum=sum+fact;
n=n/10;
}
if(sum==temp)
{
System.out.println("strong no.");
}
else
{
System.out.println();
}
}
}
}
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.");
}
}
}
//armstrong 1 to 1000
public class armstrong1to1000
{
//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 );
}
}
//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);
}
}
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]);
}
}
}
//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);
}
}
//secondlargerray
// secondlarg1
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
{
//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;
// 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;
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
//question
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}};
}
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]+" ");
}
}
}
//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]+" ");
}
}
}
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);
}
}
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]);
}
}
}
}
}
}
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]+" ");
}
}
}
//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 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
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
1
123
12345
1234567
123456789
1
333
55555
7777777
999999999
1
212
32123
4321234
543212345
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;