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

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

1 comment: