Monday, November 23, 2009

Class X - Q21 to Q27 2009

/*
Q21. Write program in java to display the given pattern :
1AAAAA
22BBBB
333CCC
4444DD
55555E
*/

import java.io.*;
class pattern
{
public static void main(String args[])
{
int i,j,c;
c=65;

for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
System.out.print(i);
}
for(j=5;j>=i;j--)
{
System.out.print((char)c);
}
c++;
System.out.println();
}
}
}
/*OUTPUT

1AAAAA
22BBBB
333CCC
4444DD
55555E
*/

-------------------------------------------------------------------
/*
Q22. To accept a number and print whether the number is a special number or not.
A number is said to be a special number if the sum of the factorial of the digits
as same as the original number.
Eg. 145 is a perfect number as 5!+4!+1!=145.
*/

import java.io.*;
class special_number
{
public static int factorial(int a)
{
int i,f=1;
for(i=1;i<=a;i++)
{
f=f*i;
}
return f;
}
public static void main(String args[])throws IOException
{
BufferedReader br= new BufferedReader( new InputStreamReader(System.in));

int n,t,ld,sum=0;

System.out.print("Enter a number = ");
n=Integer.parseInt(br.readLine());

t=n;

while(n!=0)
{
ld=n%10;
sum=sum+factorial(ld);
n=n/10;
}
n=t;
if(sum==n)
{
System.out.print("The number is a special number ");
}
else
{
System.out.print("The number is not a special number ");
}
}
}

/*
output
Enter a number = 145
The number is a special number Enter a number = 643
The number is not a special number
*/


------------------------------------------------------------


/*
Q23. An automorphic number is the number, which is contained, in the last digit (s) of its square.
Write a program in java to accept a number and check whether it is a automorphic number or not,
using function name digit(int n) which returns number of digits present in the number.
Eg. 25 is an Automorphic as its squre is 625 and 25 is present as the last two digits.
*/

import java.io.*;
class Q4_automorphic_number
{
public static int digit(int n)
{
int c=0;
while(n!=0)
{
n=n/10;
c++;
}
return c;
}
public static void main(String args[])throws IOException
{
BufferedReader br= new BufferedReader( new InputStreamReader(System.in));

int n,sq,ld;

System.out.print("Enter a number =");
n=Integer.parseInt(br.readLine());

sq=n*n;

ld=sq%(int)Math.pow(10,digit(n));

if(n==ld)
{
System.out.println("The number is a Automorphic number ");
}
else
{
System.out.println("The number is Not a Automorphic number ");
}
}
}

/*
output
Enter a number =625
The number is a Automorphic number
Enter a number =23
The number is Not a Automorphic number
*/


-------------------------------------------------------------

/*
Q24. Raj Kishore is an assistant in the office. He has the habit of writing the Names of the emplooyees by eliminating the middle name "KUMAR". Write a program in java to accept the names and insert the middle name "KUMAR" and display the full name.Consider the name of an employee contains three words.
Eg. Input: RAJIB NATH and Output: RAJIV KUMAR NATH.
*/

import java.io.*;
class Q5_middle_name
{
public static void main(String args[])throws IOException
{
BufferedReader br= new BufferedReader( new InputStreamReader(System.in));

String name,fullname;
int len,ps;

System.out.print("Enter a Name : ");
name=br.readLine();

len=name.length();
ps=name.indexOf(' ');

fullname=name.substring(0,ps+1)+"KUMAR"+name.substring(ps,len);

System.out.println("Full Name : "+fullname);
}
}
/*
output
Enter a Name : Rajiv Nath
Full Name : Rajiv KUMAR Nath
*/


----------------------------------------------------------


/*
Q25. Write a program in java to accept 10 numbers in single dimensional array.
pass the array to a function search(int m[], int ns) to search the given number ns
in the list of array elements. It returns true if the number is found,false otherwise.
*/


import java.io.*;

public class Q6_search
{
public static boolean search(int m[], int ns)
{
int i,flag=0;

for(i=0;i<10;i++)
{
if(m[i]==ns)
{
flag=1;
}
}

if(flag==1)
{
return true;
}
else
{
return false;
}
}

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

int array[]= new int[10];
int i,ns;

for(i=0; i<10;i++)
{
System.out.print("Enter a number= ");
array[i]=Integer.parseInt(br.readLine());

}

System.out.print("Enter a number for search = ");
ns=Integer.parseInt(br.readLine());

System.out.print("Result = "+ search(array,ns));


}
}

/*OUTPUT
Enter a number= 98
Enter a number= 56
Enter a number= 45
Enter a number= 65
Enter a number= 45
Enter a number= 66
Enter a number= 78
Enter a number= 21
Enter a number= 12
Enter a number= 45
Enter a number for search = 45
Result = true
*/

--------------------------------------------------------



/*
Q26.Define a class security having the following description :
Data members : String n(to store name)
Instant variables : int h (to store number of hours for which wages to paid )
: double r (rate of the wages)
: double w (to calculate the wages)
Member function :
Get() : to store the name, rate, and number of hours.
CalWage() : Calculate the wages of an employee.
Display() : Output details of an employee
Write a program to compute the wages according to the given conditions and display the output as per given format:
Number of hours Rate
Up to 40 hours Rs. r per hours
For the next 20 hours Rs. 1.5 r per hour
For the next 20 hours Rs. 2 r per hour
above 80 hours Rs. 3 r per hour
Output: NAME HOURS WAGES
XXXXXXX XXX XXXX.XX
*/

import java.io.*;
public class Q7_function
{
String n;
int h;
double r;
double w;

public void get()throws IOException
{
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter the name : ");
n=br.readLine();
System.out.print("Enter the number of hours : ");
h=Integer.parseInt(br.readLine());
System.out.print("Enter the rate of wages : ");
r=Integer.parseInt(br.readLine());
}

public void CalWage()
{
if(h<=40)
{
w=r*h;
}
else if(h>40 && h<=60)
{
w=1.5*r*h;
}
else if(h>60 && h<=80)
{
w=2*r*h;
}
else if(h>80)
{
w=3*r*h;
}
}

public void display()
{
System.out.println("NAME \t HOURS \t WAGES");
System.out.println(n+"\t"+h+"\t"+w);
}
}

/*
Enter the name : KASHIFA AFRIN
Enter the number of hours : 18
Enter the rate of wages : 120
NAME HOURS WAGES
KASHIFA AFRIN 18 2160.0
*/


------------------------------------------------------


/*
Q27. Write a program to create 4x4 matrix.Find the sum of the numbers of left diagonal and the sum of right diagonal of the matrix by using assignment statement.Display the original matrix also.
*/


import java.io.*;
public class Q10_matrix
{
public static void main (String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

int i,j,ld=0,rd=0;
int m[] []={{3,2,1,4},
{5,6,7,8},
{12,9,10,11},
{13,14,15,16}};



for(i=0;i<4;i++)
{
ld=ld+m[i][i];
}
System.out.println("The sum of left diagonal is="+ld);




for(i=0;i<4;i++)
{
rd=rd+m[i][3-i];

}
System.out.println("The sum of right diagonal is="+rd);

System.out.println("The matrix is");

for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
System.out.print(m[i][j]+"\t");
}
System.out.println();
}
}
}

/*
The sum of left diagonal is=35
The sum of right diagonal is=33
The matrix is
3 2 1 4
5 6 7 8
12 9 10 11
13 14 15 16
*/



------------------------------------------------------------------------

Saturday, November 21, 2009

Class X Q1 to Q20 2009

/*
Q1. Write a program to find if a number is armstrong or not.
*/
import java.io.*;
public class Q1_armstrongnumber
{
public static void main (String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader br= new BufferedReader(read);

int n,ld,s=0,q,t;

System.out.print("Enter a number = ");
n=Integer.parseInt(br.readLine());
t=n;

while(n!=0)
{
ld=n%10;
q=ld*ld*ld;
s=s+q;
n=n/10;
}
n=t;

if(s==n)
{
System.out.println("The number is an armstrong ");
}
else
{
System.out.println("The number is not armstrong ");
}
}
}

/*OUTPUT :
Enter a number = 153
The number is an armstrong number
Enter a number = 165
The number is not an armstrong number
*/
-----------------------------------------------------------
/*
Q2.Write a program to find a number is perfect or not.
*/
import java.io.*;
public class Q2_perfectnumber
{
public static void main (String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader br= new BufferedReader(read);

int n,i,s=0;

System.out.print("Enter a number = ");
n=Integer.parseInt(br.readLine());

for(i=1;i break;
}
case 2:
{
System.out.print("Monday");
break;
}
case 3:
{
System.out.print("Tuesday");
break;
}
case 4:
{
System.out.print("Wednesday");
break;
}
case 5:
{
System.out.print("Thursday");
break;
}
case 6:
{
System.out.print("Friday");
break;
}
case 7:
{
System.out.print("Saturday");
break;
}
default :
{
System.out.print("Wrong number");
}
}
}
}

/*
OUTPUT :
Enter a number = 1
Sunday
Enter a number = 5
Thursday
Enter a number = 7
Saturday
*/
------------------------------------------------------
/*
Q3.Write a program to find a number is even or odd.
*/

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

int n;

System.out.print("Enter a number = ");
n=Integer.parseInt(br.readLine());

if(n%2==0)
{
System.out.println("The number is even ");
}
else
{
System.out.println("The number is odd ");
}
}
}


/*
OUTPUT :

Enter a number = 6
The number is even
Enter a number = 17
The number is odd

*/

-------------------------------------------------------

/*
Q4. Write a program to find the factorial of a number.
*/

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

int n,i,f=1;

System.out.print("Enter a number = ");
n=Integer.parseInt(br.readLine());

for(i=1;i<=n;i++)
{
f=f*i;
}

System.out.println("Factorial = "+f);

}
}

/*
OUTPUT :
Enter a number = 4
Factorial = 24
Enter a number = 9
Factorial = 362880
*/

------------------------------------------

/*
Q5. Write a program to display the names of the days of the week.[1 for mondday.....]
*/

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

int n;

System.out.print("Enter a number = ");
n=Integer.parseInt(br.readLine());

switch(n)
{
case 1:
{
System.out.print("Sunday");
break;
}
case 2:
{
System.out.print("Monday");
break;
}
case 3:
{
System.out.print("Tuesday");
break;
}
case 4:
{
System.out.print("Wednesday");
break;
}
case 5:
{
System.out.print("Thursday");
break;
}
case 6:
{
System.out.print("Friday");
break;
}
case 7:
{
System.out.print("Saturday");
break;
}
default :
{
System.out.print("Wrong number");
}
}
}
}

/*
OUTPUT :
Enter a number = 1
Sunday
Enter a number = 5
Thursday
Enter a number = 7
Saturday
*/


-------------------------------------------
/*
Q6. Write a program to check if a string is palindrome or not.
*/

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

String w,rw="";
int len,i;
char c;

System.out.print("Enter a word = ");
w=br.readLine();

len=w.length();

for(i=len-1;i>=0;i--)
{
c=w.charAt(i);
rw=rw+c;
}

if(w.equals(rw)==true)
{
System.out.print("The word is palindrome");
}
else
{
System.out.print("The word is not palindrome");
}

}
}

/*
OUTPUT :
Enter a word = MADAM
The word is palindrome
Enter a word = COMPUTER
The word is not palindrome
*/
----------------------------------------------------------
/*
Q7. Write a program to swap two values.
*/

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

int a=0,b=0;

System.out.print("Enter a 1st number = ");
a=Integer.parseInt(br.readLine());

System.out.print("Enter a 2nd number = ");
b=Integer.parseInt(br.readLine());

a=a+b;
b=a-b;
a=a-b;

System.out.println("1st number= "+a);
System.out.println("2nd number= "+b);
}
}

/*
OUTPUT :
Enter a 1st number = 5
Enter a 2nd number = 8
1st number= 8
2nd number= 5
*/
-------------------------------------------
/*
Q8.Write a program of bubble sorting to set the values in ascending order:9,2,1,22,5,3,8,16,98,6.
*/
import java.io.*;
class Q8_bubblesorting
{
public static void main(String args[])
{
int n[]={9,2,1,22,5,3,8,16,98,6};
int i,j,t;

for(i=0;i<9;i++) j="0;j<9;j++)">n[j+1])
{
t=n[j];
n[j]=n[j+1];
n[j+1]=t;
}
}
}
/*
OUTPUT :
1
2
3
5
6
8
9
16
22
*/

-----------------------------------------------


/*
Q9. Write a program to search an element by linear search.
*/

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

int n[]=new int[10];
int i,a,flag=0;

for(i=0;i<10;i++)
{
System.out.print("Enter a number = ");
n[i]=Integer.parseInt(br.readLine());
}

System.out.println("Enter a number for search = ");
a=Integer.parseInt(br.readLine());

for(i=0;i<10;i++)
{
if(n[i]==a)
{
flag=1;
}
}

if(flag==1)
{
System.out.println("Search successful");
}
else
{
System.out.println("Search unsucessful");
}
}
}

/*
OUTPUT :
Enter a number = 4
Enter a number = 5
Enter a number = 6
Enter a number = 8
Enter a number = 9
Enter a number = 5
Enter a number = 4
Enter a number = 2
Enter a number = 1
Enter a number = 7
Enter a number for search = 8
Search successful

Enter a number = 5
Enter a number = 6
Enter a number = 4
Enter a number = 2
Enter a number = 9
Enter a number = 7
Enter a number = 4
Enter a number = 5
Enter a number = 8
Enter a number = 6
Enter a number for search = 3
Search unsucessful


*/

------------------------------------------------
/*
Q10.Write a program to replace 'u' by 'o' in EDUCATION.*/

import java.io.*;

public class Q10_replace
{
public static void main(String args[])
{
String s,ns="";
int len,i;
char c;

s="EDUCATION";
len=s.length();

for(i=0;i {
c=s.charAt(i);

if(c=='U')
{
ns=ns+'O';
}
else
{
ns=ns+c;
}
}
System.out.println(ns);
}
}


/*
OUTPUT :
EDOCATION
*/


--------------------------------------------


/*
Q11.Design a calculator with add() and sub()method.
*/

import java.io.*;

public class Q11_calculator
{
public static void add(int a, int b)
{
int s=a+b;
System.out.println("Sum of two numbers="+s);
}

public static void sub(int a, int b)
{
int s=a-b;
System.out.println("Subtracting two number="+s);
}

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

int x,y;

System.out.print("Enter the 1st number = ");
x=Integer.parseInt(br.readLine());

System.out.print("Enter the 2nd number = ");
y=Integer.parseInt(br.readLine());

add(x,y);
sub(x,y);
}
}

/*
OUTPUT :
Enter the 1st number = 9
Enter the 2nd number = 5
Sum of two numbers=14
Subtracting two number=4
*/

-------------------------------------------
/*
Q12. Write a program to calculate the area of a square. */

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

int s,area=0;

System.out.print("Enter side of a square = ");
s=Integer.parseInt(br.readLine());

area=s*s;

System.out.print("The area of a square = "+area);
}
}


/*
OUTPUT :
Enter side of a square = 12
The area of a square = 144

*/

--------------------------------------------
/*
Q13. Write a program to calculate the area of a square. */

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

int s,area=0;

System.out.print("Enter side of a square = ");
s=Integer.parseInt(br.readLine());

area=s*s;

System.out.print("The area of a square = "+area);
}
}


/*
OUTPUT :
Enter side of a square = 12
The area of a square = 144

*/

------------------------------------------------------

/*
Q14.Write a program to find whether a number is prime or not. */

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

int n,i,flag=0;

System.out.print("Enter a number = ");
n=Integer.parseInt(br.readLine());

for(i=2;i {
if(n%i==0)
{
flag=1;
}
}
if(flag==0)
{
System.out.print("The number is prime");
}
else
{
System.out.print("The number is not prime");
}
}
}


/*
OUTPUT :
Enter a number = 8
The number is not prime
Enter a number = 3
The number is prime
*/
------------------------------------------------------

/*
Q15.Write a program to find the cube of a number.
*/

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

int n;
double q;

System.out.print("Enter a number = ");
n=Integer.parseInt(br.readLine());

q=Math.pow(n,3);

System.out.print("Cube of the number is = "+q);
}
}


/*
OUTPUT :
Enter a number = 5
Cube of the number is = 125.0
Enter a number = 2
Cube of the number is = 8.0
*/

-------------------------------------------------

/*
Q16.Write a program to add two matrices and disply.
*/

import java.io.*;
public class Q16_matrices
{
public static void main(String args[])
{
int i,j;
int sum[][]=new int [4][4];

int m1[][]={{9,8,7,4},{1,2,3,4},{4,5,6,1},{1,4,7,2}};
int m2[][]={{1,2,1,4},{5,2,6,5},{3,1,5,9},{3,5,8,3}};

for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
sum[i][j]=m1[i][j]+m2[i][j];
}
}

System.out.println("1st matrices :");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
System.out.print(m1[i][j]+"\t");
}
System.out.println();
}

System.out.println("2nd matrices :");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
System.out.print(m2[i][j]+"\t");
}
System.out.println();
}

System.out.println("Sum of two matrices :");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
System.out.print(sum[i][j]+"\t");
}
System.out.println();
}
}
}
/*
OUTPUT :
1st matrices :
9 8 7 4
1 2 3 4
4 5 6 1
1 4 7 2
2nd matrices :
1 2 1 4
5 2 6 5
3 1 5 9
3 5 8 3
Sum of two matrices :
10 10 8 8
6 4 9 9
7 6 11 10
4 9 15 5
*/





-------------------------------------------------------


/*
Q17.Write a program to convert temperature value from fahrenheit.
*/

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

double f,c=0.0;

System.out.print("Enter the temperature in farenheit = ");
f=Double.parseDouble(br.readLine());

c=(f-32)/9;

System.out.println("Celsius = "+c);
}
}

/*
OUTPUT :
Enter the temperature in farenheit = 8
Celsius = -2.6666666666666665
Enter the temperature in farenheit = 6
Celsius = -2.888888888888889
*/


-----------------------------------------------------------

/*
Q18.Write a program tocheck if a character is in uppercase or lowercase
*/

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

char c;
int as;

System.out.print("Enter a character : ");
c=(char)br.read();

as=c;

if(as>=65 && as<=90)
{
System.out.println("It is a upper case character");
}
else if(as>=97 && as<=122)
{
System.out.println("It is lower case character");
}
else
{
System.out.println("It is not a character");
}
}
}

/*
OUTPUT :
Enter a character : s
It is lower case character
Enter a character : A
It is a upper case character
Enter a character : 2
It is not a character
*/
----------------------------------------
/*
Q19.Write a program to print 'INDIA' as 'AIDNI'.
*/

import java.io.*;
public class Q19_revers
{
public static void main (String args[])
{

String s,rs="";
int len,i;
char ch;

s="INDIA";
len=s.length();

for(i=len-1;i>=0;i--)
{
ch=s.charAt(i);
rs=rs+ch;
}

System.out.print("The reverse word is ="+rs);
}
}

/*
OUTPUT :
The reverse word is =AIDNI

*/

----------------------------------------------------

/*
Q20.Write a program to calculate the sum of this series:1/1!+2/2!+3/3!+/4!....10/10!.
*/

import java.io.*;
public class Q20_series
{
public static double factorial(int n)
{
int i;
double f=1;

for(i=1;i<=n;i++)
{
f=f*i;
}
return f;
}

public static void main(String args[])
{
int i;
double sum=0.0;

for(i=1;i<=10;i++)
{
sum=sum+1/factorial(i);
}

System.out.println("Sum of the series = "+sum);
}
}

/*
OUTPUT :
Sum of the series = 1.7182818011463847
*/

----------------------------------------------------------

Wednesday, November 18, 2009

Welcome To my School

Welcome To My Computer School.