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");
  }
  }
}  

Reverse Cases(upper c to lower c, lower c to upper c)

import java.io.*;
public class reversecase
{ public static void main(String args[]) throws IOException
  { InputStreamReader read = new InputStreamReader(System.in);
  BufferedReader br = new BufferedReader(read);
   
  System.out.println("enter");
  String s=br.readLine();
  char ch;
  for(int i=0;i  { ch = s.charAt(i);
  int a=ch;
  if(a>=65 && a<=90)
  { a+=32;
  System.out.print((char)a);
  }
  else if(a>=97 && a<=122)
  { a-=32;
  System.out.print((char)a);
  }
  else 
  System.out.print(ch);
  }
  }
}

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]);
  }
  }
}
   
   
 

Enter String-Find No of Vowels,Chars,Reverse String

import java.io.*;
public class reverse
{ public static void main(String args[]) throws IOException
  {  
  InputStreamReader read = new InputStreamReader(System.in);
  BufferedReader br = new BufferedReader(read);
   
  System.out.println("Enter string(uppercase)");
  String s=br.readLine();
   
  s = s.toUpperCase();
  StringBuffer revs = new StringBuffer(s);
  int ch = revs.length();
  int vow = 0;
  for(int i=0;i  { if(revs.charAt(i)=='A' || revs.charAt(i)=='E' || revs.charAt(i)=='I' || revs.charAt(i)=='O' || revs.charAt(i)=='U')
  {vow++;}
  }

  revs.reverse();
  System.out.println("Total number of characters: " +ch);
  System.out.println("Number of vowels: "+vow);
  System.out.println("Reverse String: "+revs);
  }
}
   

   

Very Funny Program-Easy To Understand

import java.io.*;

public class question10
{ 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");

int a = Integer.parseInt(br.readLine());

System.out.println("Ha Dumbass I just wasted your time faggot");
  }
  }
   

Student Report

import java.io.*;

public class question6
{
  public static void main(String args[]) throws IOException
  { question6 abc = new question6();
  abc.show();
  }
   
  void show() throws IOException
  { InputStreamReader read = new InputStreamReader(System.in);
  BufferedReader br = new BufferedReader(read);
  String name[] = new String[2];
  int math[] = new int[2];
  int comp[] = new int[2];
  int english[] = new int[2];
  int total[] = new int[2];
  int percent[] = new int[2];
  for(int i=0;i<2;i++)
  { System.out.println("Enter name of student");
  name[i] = br.readLine();
  System.out.println("Enter his marks in maths");
  math[i]=Integer.parseInt(br.readLine());
  System.out.println("Enter his marks in english");
  english[i]=Integer.parseInt(br.readLine());
  System.out.println("Enter his marks in computers");
  comp[i]=Integer.parseInt(br.readLine());
  }
  for(int i=0;i<2;i++)
  { total[i] = math[i]+english[i]+comp[i];
  percent[i] = total[i]/3;
  }
  System.out.println(" *****Student Report*******\n");
  System.out.println(" name english math computers total percentage");
  for(int i=0;i<2;i++)
  { System.out.println(name[i]+ " " +english[i]+ " " +math[i]+" " +comp[i]+" " +total[i]+ " " +percent[i]+"%");
  } 
  }
}
   

Fibonacci

import java.io.*;
public class fibonacci
{  
  public static void main(String args[]) throws IOException
  {  
  InputStreamReader read = new InputStreamReader(System.in);
  BufferedReader br = new BufferedReader(read);
   
  int a=0, b=1;
  int c;
  for(int i=0;i<11;i++)
  { c=a+b;
  System.out.println(c);
  a=b;
  b=c;
  }
  }
}

Enter State-Enter Capital Then Find Capital

import java.io.*;
public class question2
{ public static void main(String args[]) throws IOException
  { question2 abc = new question2();
  abc.show();
  }
   
  void show() throws IOException
  { InputStreamReader read = new InputStreamReader(System.in);
  BufferedReader br = new BufferedReader(read);
   
  String st[] = new String[4];
  String ca[] = new String[4];
   
  for(int i=0;i<4;i++)
  { System.out.println("Enter a state");
  st[i] = br.readLine();
  System.out.println("Enter its capital");
  ca[i] = br.readLine();
  }
  System.out.println("OK!");
  System.out.println();
  System.out.println("Enter state to display its capital");
  String s = br.readLine();
  int count=0;
  for(int i=0;i<4;i++)
  { if(s.equals(st[i]))
  {System.out.println(ca[i]);
  count++;
  }}
  if(count==0)
  System.out.println("Error");
  }
}