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
*/



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

No comments:

Post a Comment