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