Showing posts with label bubble sort java. Show all posts
Showing posts with label bubble sort java. Show all posts

Wednesday, March 24, 2010

Sum Of Double Dimensional Array Diagonals

import java.io.*;

public class sumofdiagonals
{ public static void main(String args[]) throws IOException
{ InputStreamReader read = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(read);

int arr[][] = {{6,9,0,8},{3,2,1,5},{2,9,0,1},{4,1,9,6}};
int sum=0;
for(int i=0;i<4;i++)
{ for(int j=0;j<4;j++)
{ if(i==j)
sum+=arr[i][j];
}
}
System.out.println(sum);
}
}

Tuesday, March 23, 2010

Arrange Names From Array Alphabetically

import java.io.*;

public class namearranger
{ public static void main(String args[]) throws IOException
{ InputStreamReader read = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(read);
String arr[] = new String[5];
System.out.println("Enter 5 names.");
for(int i=0;i<5;i++)
{ arr[i]=br.readLine();
}
String temp=null;
System.out.println("They will now be arranged alphabetically");
for(int i=0;i<5;i++)
{ for(int j=0;j<4;j++)
{ if((int)arr[j].charAt(0) > (int)arr[j+1].charAt(0))
{ temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
for(int i=0;i<5;i++)
{ System.out.println(arr[i]);
}
}
}

2 - 4 + 6 - 8......20

import java.io.*;
public class twominusfourplussixminuseight
{
public static void main(String args[])
{ System.out.println("2-4+6-8...-20");
int a=1;
int s=0;
for(int i=2;i<=20;i+=2)
{
if(a%2!=0)
s+=i;
else
s-=i;
a++;
System.out.println(a+ " " +i+ " " +s);

}
System.out.println(s);

}
}

Count Number Of Times Word Occurs

import java.io.*;

public class position
{ public static void main(String args[]) throws IOException
{ InputStreamReader read = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(read);

System.out.println("Enter a sentence");
String a=br.readLine();
System.out.println("Enter word to be searched for");
String word = br.readLine();

int count=0;
int pos=-1;
pos = a.indexOf(word);

while(pos>=0)
{ count++;
pos = pos + word.length();
pos = a.indexOf(word,pos);
}
System.out.println("Number of times word occurs " +count);
System.out.println(pos);
}
}

Salary Program(Enter Name, Salary, Calculate Tax etc)

import java.io.*;

public class salary
{ String name, add, subsp;
double phone, monsal, intax;

void accept() throws IOException
{ InputStreamReader read = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(read);

System.out.println("Enter the teacher name");
name = br.readLine();
System.out.println("Enter the teacher's address");
add = br.readLine();
System.out.println("Enter the teacher's phone number");
phone = Integer.parseInt(br.readLine());
System.out.println("Enter the teacher's subject specialization");
subsp = br.readLine();
System.out.println("Enter the teacher's monthly salary");
monsal = Double.parseDouble(br.readLine());
}

void compute()
{ if(monsal<175000)
intax=0;
else
intax = (monsal-175000)*0.05;
}

void show()
{ System.out.println("TEACHER DETAILS--------\n\n");
System.out.println("NAME: " +name);
System.out.println("ADDRESS: " +add);
System.out.println("PHONE NUMBER: " +phone);
System.out.println("SUBJECT SPECIALIZATION: " +subsp);
System.out.println("MONTHLY SALARY: " +monsal);
System.out.println("INCOME TAX: " +intax);
}

public static void main(String args[]) throws IOException
{ salary abc = new salary();
abc.accept();
abc.compute();
abc.show();
}
}

Dont just blindly copy paste this one-there's a small fuckup inside
(salary must be multiplied by 12 to calculate income tax)

x/2 + x/5 + x/8

import java.io.*;

public class xupon258
{
public static void main(String args[]) throws IOException
{ InputStreamReader read = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(read);

System.out.println("Enter a number");
double x=Double.parseDouble(br.readLine());
System.out.println("Now x/2+x/5+x/8");
double sum=0;
for(int i=2;i<=8;i+=3)
{ sum+=x/i;
System.out.println("After adding "+x+"/"+i+", sum till now is "+sum);
}
System.out.println("Final Sum is " +sum);

}
}

Count Positive & Negative Numbers In List

import java.io.*;

public class countposneg
{ public static void main(String args[]) throws IOException
{ InputStreamReader read = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(read);
int arr[] = new int[575];
int j=2;
System.out.println("Enter a list of numbers(terminates on entering 0)");

for(int i=0;j<3;i++)
{ arr[i] = Integer.parseInt(br.readLine());
if(arr[i]==0)
{j++; }
}
int neg=0, poseve=0, posodd=0;
for(int i=0;i { if(arr[i]<0)
{neg++;}
if(arr[i]>0 && arr[i]%2==0)
{poseve++;}
if(arr[i]>0 && arr[i]%2==1)
{posodd++;}
}
System.out.println("\n\nNumber Of Negative Numbers: "+neg);
System.out.println("Number Of Positive Even Numbers: "+poseve);
System.out.println("Number of Positive Odd Numbers: "+posodd);
}
}

Check For Palindrome & Perfect Number

import java.io.*;

public class palindrome
{
  public static void main(String args[]) throws IOException
  { InputStreamReader read = new InputStreamReader(System.in);
  BufferedReader br = new BufferedReader(read);
  palindrome ab = new palindrome();
  ab.show();
  }
   
  void show() throws IOException
  { InputStreamReader read = new InputStreamReader(System.in);
  BufferedReader br = new BufferedReader(read);
  System.out.println("Enter 1 for checking for a palindrome, 2 to check for perfect number");
  int a = Integer.parseInt(br.readLine());
  System.out.println("Enter number to check");
  int aa = Integer.parseInt(br.readLine());
  switch(a)
  { case 1: palin(aa); break;
  case 2: pno(aa); break;
  default: System.out.println("Wrong numeber");
  }
  }
  void palin(int x)
  { int remain, reverse = 0; int original=x;
  while(x>0)
  { remain = x%10;
  reverse = reverse*10 + remain;
  x=x/10;
  }
  if(reverse==original)
  System.out.println("It is palindrome");
  else System.out.println("Sorry");
  }
  void pno(int x)
  { int sum=0; 
  for(int i=1;i  { if(x%i==0)
  sum=sum+i;
  }
  if(sum==x) 
  System.out.println("It is perfect number");
  else 
  System.out.println("It is not a perfect number");
  }
}


   

Star Pyramid(Nested Loops)

import java.io.*;

public class vfg
{ public static void main(String args[])  
  {  
  int b=4;
  for(int i=1;i<=9;i=i+2)
  { for(int j=1;j<=b;j++)
  { System.out.print(" ");
  }
  for(int k=1;k<=i;k++)
  { System.out.print("*");
  }
  System.out.println();
  b--;
  }
  }
}

Print Upright/Inverted Triangle Till n Terms

import java.io.*;

public class triangles
{ public static void main(String args[]) throws IOException
  { InputStreamReader read = new InputStreamReader(System.in);
  BufferedReader br = new BufferedReader(read);
   
  System.out.println("Enter 1 for upright triangle, 2 for inverted triangle");
  int choice=Integer.parseInt(br.readLine());
   
  switch(choice)
  { case 1: System.out.println("Enter number");
  int x=Integer.parseInt(br.readLine());
   
  for(int i=1;i<=x;i++)
  { for(int j=1;j<=i;j++)
  { System.out.print(i+" ");
  }
  System.out.println();
  }
  break;
   
  case 2: System.out.println("Enter number");
  int y=Integer.parseInt(br.readLine());
   
  for(int i=y;i>=1;i--)
  { for(int j=i;j>=1;j--)
  { System.out.print(i+" ");
  }
  System.out.println();
  }
  break;
   
  default: System.out.println("Error, wrong choice monsieur");
  }
  }
}  

Bubble Sort

import java.io.*;

public class sortbubble
{ public static void main(String args[]) throws IOException
  { InputStreamReader read = new InputStreamReader(System.in);
  BufferedReader br = new BufferedReader(read);
  int arr[] = new int[10];
  System.out.println("Enter 10 numbers");
  for(int i=0;i<10;i++)
  { arr[i]=Integer.parseInt(br.readLine());
  }
  System.out.println("They will now be sorted IN ASCENDING ORDER using bubb;e sort");
  int temp=0;
  for(int i=5;i<15;i++)
  { for(int j=0;j<9;j++)
  { if(arr[j]>arr[j+1])
  { temp=arr[j];
  arr[j]=arr[j+1];
  arr[j+1]=temp;
  }
  }
  }
  for(int i=0;i<10;i++)
  { System.out.println(arr[i]);
  }
   
  System.out.println("Now they will be sorted in DESCENDING ORDER");
  for(int i=0;i<10;i++)
  { for(int j=0;j<9;j++)
  { if(arr[j]  { temp=arr[j];
  arr[j]=arr[j+1];
  arr[j+1]=temp;
  }
  }
  }
  for(int i=0;i<10;i++)
  { System.out.println(arr[i]);
  }
  }
}