ICSE 2010 Programs

For any queries related to the programs you can either leave a comment below 
or SMS me at 9934183218 of mail me at nilabja.sarkar@gmail.com

Question 4   

( ARRAY TYPE )

import java.io.*;
class BinarySearch
{
public static void main() throws IOException
{
InputStreamReader x= new InputStreamReader(System.in);
BufferedReader y= new BufferedReader(x);

int ARR[]=new int[10];
int i,pos=-1;
int low=0,up=9,mid;

System.out.println(“Enter 10 numbers in sorted order”);
for(i=0;i<10;i++)
{
ARR[i]=Integer.parseInt(y.readLine());
}

System.out.println(“Enter the search element”);
int src=Integer.parseInt(y.readLine());

while(low<=up)
{
mid=(low+up)/2;
if(src==ARR[mid])
{
pos=mid;
break;
}

else if(src>ARR[mid])
low=mid+1;

else
up=mid-1;
}

if(pos>-1)
System.out.println(“Found at “+(pos+1));
else
System.out.println(“Not found”);

}
}

Question 5   

( FUNCTION TYPE )

import java.io.*;
class Student
{
String name;
int age, m1, m2, m3, maximum, average;

Student()
{
name=””;
age=0; m1=0; m2=0; m3=0; maximum=0; average=0;
}

void accept() throws IOException
{
InputStreamReader x = new InputStreamReader(System.in);
BufferedReader y = new BufferedReader(x);

System.out.println(“Enter name”);
name=y.readLine();
System.out.println(“Enter age”);
age=Integer.parseInt(y.readLine());
System.out.println(“Enter marks in 3 subjects”);
m1=Integer.parseInt(y.readLine());
m2=Integer.parseInt(y.readLine());
m3=Integer.parseInt(y.readLine());
}

void compute()
{
average=(m1+m2+m3)/3;
maximum=Math.max(Math.max(m1,m2),m3);
}

void display()
{
System.out.println(“Name : “+name);
System.out.println(“Age : “+age);
System.out.println(“Marks 1 : “+m1);
System.out.println(“Marks 2 : “+m2);
System.out.println(“Marks 3 : “+m3);
System.out.println(“Average : “+average);
System.out.println(“Maximum Marks : “+maximum);
}

public static void main() throws IOException
{
Student obj = new Student();
obj.accept();
obj.compute();
obj.display();
}
}

Question 6   

( IF…ELSE TYPE )

import java.io.*;
class ShashaTravels
{
public static void main() throws IOException
{
InputStreamReader x= new InputStreamReader(System.in);
BufferedReader y= new BufferedReader(x);
double t,d,n;
int i;
String name;
for(i=1;i<=15;i++)
{
System.out.println(“Enter name”);
name=y.readLine();
System.out.println(“Enter amount”);
t=Double.parseDouble(y.readLine());
if(t>70000)
d=0.18*t;
else if(t>55000)
d=0.16*t;
else if(t>35000)
d=0.12*t;
else if(t>25000)
d=0.1*t;
else
d=0.02*t;

n=t-d;
System.out.printf(“%s %20s %15s %12s %12s \n”,”Sl.No.”,”Name of Customer”,”Ticket Charges”,”Discount”,”Net Amount”);
System.out.printf(“%-10d %-20s %-15f %-12f %-12f \n”,i,name,t,d,n);
}
}
}

Question 7    

( SWITCH…CASE & FOR LOOP TYPE )

import java.io.*;
class Prime_Automorphic
{
public static void main() throws IOException
{
InputStreamReader x= new InputStreamReader(System.in);
BufferedReader y= new BufferedReader(x);
int i,ch,n,num,c=0,sq,len;
System.out.println(“1. Prime”);
System.out.println(“2. Automorphic”);
System.out.println(“Enter choice”);
ch=Integer.parseInt(y.readLine());

switch(ch)
{
case 1:
System.out.println(“Enter a number”);
n=Integer.parseInt(y.readLine());
for(i=1;i<=n;i++)
if(n%i==0) c++;

if(c==2)System.out.println(“Prime”);
else System.out.println(“Composite”);
break;

case 2:
System.out.println(“Enter a number”);
n=Integer.parseInt(y.readLine());
sq=n*n;
len=Integer.toString(n).length();
num=sq%(int)Math.pow(10,len);
if(n==num)
System.out.println(“Automorphic”);
else
System.out.println(“Not an automorphic”);
break;

default:
System.out.println(“Wrong choice…”);
}
}
}

Question 8    

( ARRAY TYPE )

import java.io.*;
class ArrayMerge
{
public static void main() throws IOException
{
InputStreamReader x= new InputStreamReader(System.in);
BufferedReader y= new BufferedReader(x);

int P[]=new int[6];
int Q[]=new int[4];
int R[]=new int[10];
int i;
System.out.println(“Enter 6 numbers”);
for(i=0;i<6;i++)
P[i]=Integer.parseInt(y.readLine());

System.out.println(“Enter 4 numbers”);
for(i=0;i<4;i++)
Q[i]=Integer.parseInt(y.readLine());

for(i=0;i<6;i++)
{
if(i<4)
{
R[i]=P[i];
R[i+6]=Q[i];
}
else
R[i]=P[i];
}

for(i=0;i<10;i++)
System.out.println(R[i]);
}
}

Question 9   

( STRING TYPE )

import java.io.*;
class AlphabetFrequency
{
public static void main() throws IOException
{
InputStreamReader x= new InputStreamReader(System.in);
BufferedReader y= new BufferedReader(x);
String s;

System.out.println(“Enter a string”);
s=y.readLine();
s=s.toUpperCase();
int i,j,c,l=s.length();

System.out.println(“CHARACTER \t FREQUENCY”);
for(i=65;i<=90;i++)
{
c=0;
for(j=0;j<l;j++)
{
if(s.charAt(j)==i)
c++;
}
if(c>0)
System.out.println(”   “+(char)i+”\t\t\t”+c);
}
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s