Vous êtes sur la page 1sur 25

1. WAJP that checks whether a given string is a palindrome or not.

Ex: MADAM
is a
palindrome.
import java.io.*;
class Palind {
public static void main(String args[ ])throws IOException {
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter the string to check for palindrome:");
String s1=br.readLine();
StringBuffer sb=new StringBuffer();
sb.append(s1);
sb.reverse();
String s2=sb.toString();
if(s1.equals(s2))
System.out.println("palindrome");
else
System.out.println("not palindrome");
}
}
2. WAJP to multiply two given matrices.
import java.util.*;
class Test {
int r1,c1,r2,c2;
Test(int r1,int c1,int r2,int c2) {
this.r1=r1;
this.c1=c1;
this.r2=r2;
this.c2=c2;
}
int[ ][ ] getArray(int r,int c) {
int arr[][]=new int[r][c];
System.out.println("Enter the elements for "+r+"X"+c+" Matrix:");
Scanner input=new Scanner(System.in);
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
arr[i][j]=input.nextInt();
return arr;
}
int[ ][ ] findMul(int a[ ][ ],int b[ ][ ]) {
int c[][]=new int[r1][c2];
for (int i=0;i<r1;i++)
for (int j=0;j<c2;j++) {
c[i][j]=0;
for (int k=0;k<r2;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
return c;
}
void putArray(int res[ ][ ]) {
System.out.println ("The resultant "+r1+"X"+c2+" Matrix is:");
for (int i=0;i<r1;i++) {
for (int j=0;j<c2;j++)
System.out.print(res[i][j]+" ");
System.out.println();
}
}
} //end of Test class
class MatrixMul {
public static void main(String args[ ])throws IOException {
Test obj1=new Test(2,3,3,2);
Test obj2=new Test(2,3,3,2);
int x[ ][ ],y[ ][ ],z[ ][ ];
System.out.println("MATRIX-1:");
x=obj1.getArray(2,3); //to get the matrix from user
System.out.println("MATRIX-2:");
y=obj2.getArray(3,2);
z=obj1.findMul(x,y); //to perform the multiplication
obj1.putArray(z); // to display the resultant matrix
}
}


WAJP that reads a line of integers and then displays each integer and the sum of all
integers. (use StringTokenizer class)
class Arguments {
public static void main(String args[ ]) {
int sum=0;
int n=args.length;
System.out.println("length is "+n);
int arr[]=new int[n];
for(int i=0;i<n;i++)
arr[i]=Integer.parseInt(args[i]);
System.out.println("The enterd values are:");
for(int i=0;i<n;i++)
System.out.println(arr[i]);
System.out.println("sum of enterd integers is:");
for(int i=0;i<n;i++)
sum=sum+arr[i];
System.out.println(sum);
}
}

Alternate solution using StringTokenizer class
import java.lang.*;
import java.util.*;
class tokendemo {
public static void main(String args[ ]) {
String s="10,20,30,40,50";
int sum=0;
StringTokenizer a=new StringTokenizer(s,",",false);
System.out.println("integers are ");
while(a.hasMoreTokens()) {
int b=Integer.parseInt(a.nextToken());
sum=sum+b;
System.out.println(" "+b);
}
System.out.println("sum of integers is "+sum);
}
}

Write a program to find whether given no. is Armstrong or not.
Example :
Input - 153
Output - 1^3 + 5^3 + 3^3 = 153, so it is Armstrong no. */
class Armstrong{
public static void main(String args[]){
int num = Integer.parseInt(args[0]);
int n = num; //use to check at last time
int check=0,remainder;
while(num > 0){
remainder = num % 10;
check = check + (int)Math.pow(remainder,3);
num = num / 10;
}
if(check == n)
System.out.println(n+" is an Armstrong Number");
else
System.out.println(n+" is not a Armstrong Number");
}
WAP to find Factorial of a number.

import java.util.*;

public class Facto
{
public static void main(String args[])
{
int n, f=1;
System.out.println("Enter any number:
");
Scanner input=new Scanner(System.in);
n=input.nextInt();
for(int i=1; i<=n;i++)
{
f=f*i;
}
System.out.println("Factorial of "+n+"
is "+f);
}
}

Output:

Enter any number:
6
Factorial of 6 is 720








WAP using while loop to reverse the given number.

import java.util.*;
public class ReverseNo
{
public static void main(String args[])
{
int Revnum=0, num, temp;
System.out.println("Enter any Number:
");
Scanner input= new Scanner(System.in);
num=input.nextInt();
temp=num;
while(temp>0)
{
Revnum=(Revnum*10)+(temp%10);
temp=temp/10;
}
System.out.println("Reverse of "+num+
" is "+Revnum);
}

}

Output:

Enter any Number:
536219
Reverse of 536219 is 912635








A cloth showroom has announced following discounts on
purchase of items.
Purchase Amt Mill Cloth Handloom
Item

0-100 - 5%
101-200 5% 7.5%
201-300 7.5% 10%
>300 10% 15%

WAP using Switch & If statement to compute net amount to the
customer.


import java.util.*;

public class SwitchIf
{
public static void main(String args[])
{
char ch;
System.out.println("Enter your
choice:\nM: Mill Cloth\nH: Handloom
Item");
try
{
switch(ch=(char)System.in.read())
{
case 'M':
System.out.println("Enter
amount: ");
Scanner input1=new
Scanner(System.in);
float
amt=input1.nextFloat();



if(amt<=100)
System.out.println("No
Discount");

else if(amt<=200)
System.out.println("Pay
Amount: "+(amt-(amt*5/100)));

else if(amt<=300)
System.out.println("Pay
Amount: "+(amt-
(amt*7.5/100)));

else
System.out.println("Pay
Amount: "+(amt-
(amt*10/100)));
break;

case 'H':
System.out.println("Enter
amount: ");
Scanner input2=new
Scanner(System.in);
float
amt1=input2.nextFloat();

if(amt1<=100)
System.out.println("Pay
Amount: "+(amt1-
(amt1*5/100)));

else if(amt1<=200)
System.out.println("Pay
Amount: "+(amt1-
(amt1*7.5/100)));



else if(amt1<=300)
System.out.println("Pay
Amount: "+(amt1-
(amt1*10/100)));

else
System.out.println("Pay
Amount: "+(amt1-
(amt1*15/100)));
break;

default:

System.out.println("Invalid
choice");
}
}
catch (Exception e)
{
System.out.println("I/O Error");
}

}
}

Output:

Enter your choice:
M: Mill Cloth
H: Handloom Item
M
Enter amount:
280
Pay Amount: 259.0
Admission to professional course is subject to following
conditions:
Marks in Maths should be >=60
Marks in Physics should be >=50
Marks in Chemistry should be >=40
Total should be >=200
Or Total of Physics & Maths should be >=200
WAP to accept the marks from student and display the message
whether he is eligible for IIT admission or not.

import java.util.*;
public class Marks
{
public static void main(String args[])
{
int P, C, M, total;
Scanner inpt=new Scanner(System.in);
System.out.println("Enter Marks in
Physics: ");
P=inpt.nextInt();
System.out.println("Enter Marks in
Chemistry: ");
C=inpt.nextInt();
System.out.println("Enter Marks in
Maths: ");
M=inpt.nextInt();

total=P+C+M;











if( (M>=60 && P>=50 && C>=40 &&
total>=200)||((P+M)>=150) )
{
System.out.println("Student is
eligible for IIT admission");
}
else
{
System.out.println("Student is not
eligible for IIT admission");
}
}
}

Output:

Enter Marks in Physics:
65
Enter Marks in Chemistry:
80
Enter Marks in Maths:
60
Student is eligible for IIT admission

In inventory management, Economic order quantity and Optimal
Quantity between orders is given.
WAP to compute EOQ and TBO. Accept demand rate, Setup Cost
and Holding Cost from user.

public class ExampleET
{
public static void main(String args[])
{
double EOQ, DR, SC, HC, TBO;
DR=12.5;
SC=40.20;
HC=15.00;

EOQ=Math.sqrt(2*DR*SC/HC);
TBO=Math.sqrt((2*SC)/(DR*HC));

System.out.println("Economic Order
Quantity: "+EOQ);
System.out.println("Time Between
Orders: "+TBO);
}
}

Output:

Economic Order Quantity: 8.18535277187245
Time Between Orders: 0.654828221749796



public class Design3
{
public static void main(String args[])
{
int p=0;
for(int i=5;i>=1;i--)
{
++p;
for(int j=1;j<i;j++)
{
System.out.print(" ");
}
for(int k=5;k>=i;k--)
{
System.out.print(p+" ");

}
System.out.println();
}
}

}

Output:

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Design a class to represent bank account. Include following
members:
Data Members-
1. Name of Depositor
2. Account No.
3. Type of account (Saving/Recurring)
4. Balance amount in the account
Methods-
1. To assign initial values
2. To deposit an amount
3. To withdraw amount after checking balance
Create a class Account with above data. Create a class
AccountTest where you can create object of an Account class.

import java.util.*;

class Account
{
int Acc_no;
String Acc_type, Cust_name;
float Amt;

Account(int A_no, String type,String name,
float amt )
{
Acc_no=A_no;
Acc_type=type;
Cust_name=name;
Amt=amt;
}

void deposit()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the amount
to be deposited");
int no = in.nextInt();
Amt=Amt+no;
System.out.println("Updated Balance
Amount: "+Amt);
}

void withdraw()
{
System.out.println("Enter the amount
to be withdrawn: ");
Scanner input = new Scanner(System.in);
int no = input.nextInt();

if(Acc_type.equalsIgnoreCase("Saving") &&
(Amt-no)>500)
{
Amt = Amt-no;
System.out.println("Amount is
withdrawn");
System.out.println("Updated
Balance Amount: "+Amt);
}

else if(Acc_type.equalsIgnoreCase
("Recurring") && (Amt-no)>1000)
{
Amt = Amt-no;
System.out.println("Amount is
withdrawn");
System.out.println("Updated
Balance Amount: "+Amt);
}
else
{
System.out.println("Amount cannot
be withdrawn");
}
}
}

public class AccountTest
{
public static void main(String args[])
{
Scanner input1 = new
Scanner(System.in);

System.out.print("Enter Customer
Name: ");
String name = input1.nextLine();

System.out.print("\nEnter Account Type
[Saving|Recurring]: ");
String Acc_type = input1.nextLine();

System.out.print ("\nEnter the Account
Number: ");
int Acc_no = input1.nextInt();

System.out.print ("\nEnter the initial
balance amount: ");
float amt_no = input1.nextFloat();

Account act=new Account(Acc_no,
Acc_type, name, amt_no);
int choice;
do
{
Scanner input = new
Scanner(System.in);

System.out.println("Enter Your
Choice:\n1. Deposit Amount\n2.
Withdraw Amount\n3. Exit");

choice=input.nextInt();




switch(choice)
{
case 1:
act.deposit();
break;

case 2:
act.withdraw();
break;

default:
System.out.println("Invalid
choice.");
}
}while(choice!=3);

}
}

Output:

Enter Customer Name: Sachin
Enter Account Type [Saving|Recurring]: Recurring
Enter the Account Number: 65481
Enter the initial balance amount: 2500

Enter Your Choice:
1. Deposit Amount
2. Withdraw Amount
3. Exit
1
Enter the amount to be deposited
200
Updated Balance Amount: 2700.0

Enter Your Choice:
1. Deposit Amount
2. Withdraw Amount
3. Exit
2
Enter the amount to be withdrawn::
1600
Amount is withdrawn
Updated Balance Amount: 1100.0

Enter Your Choice:
1. Deposit Amount
2. Withdraw Amount
3. Exit
2
Enter the amount to be withdrawn:
100
Amount cannot be withdrawn

Enter Your Choice:
1. Deposit Amount
2. Withdraw Amount
3. Exit

Vous aimerez peut-être aussi