You are on page 1of 17

import java.util.

*;
class WordPig
{
public static void main (String args[])
{
Scanner sc = new Scanner ( System.in );
System.out.println("Enter a word ");
String str = sc.nextLine();
//generating piglatin
int i;
int l = str.length();
String w = "";
for(i=0;i<l;i++)
{
char c = str.charAt(i);
if(c=='a'||c=='A'||c=='e'||c=='E'||c=='i'||c=='I'||c=='o'||c=='O'||c=='u'||
c=='U')
break;
else
w=w+c;
}
System.out.println("Piglatin of :"+str+" = "+str.substring(i,l)+w+"ay");
}
}
Output:
Enter a word
trouble
Piglatin of: trouble = oubletray

import java.util.*;
class PrimePalNum
{
public static void main(String args[])
{
Scanner sc = new Scanner (System.in);
System.out.println("Enter lower limit for range ::");
int l = sc.nextInt();
System.out.println("Enter upper limit for range ::");
int u = sc.nextInt();
//generating prime palindromic numbers
for (int i=l;i<=u;i++)
{
if(isprime(i)==true&& ispal(i)==true)
System.out.print(i+" ; ");
}
}
static boolean isprime(int i)
{
boolean flag = true ;
for(int j=2;j<=i/2;j++)
{
if(i%j==0)
{
flag = false;
break;
}
}
return flag;
}
static boolean ispal(int i)
{
int rev =0,d,copy =i;
while(copy>0)
{
d=copy%10;
rev=(rev*10)+d;
copy/=10;

}
if(rev==i)
return true;
else
return false;
}
}
Output:
Enter lower limit for range ::
10
Enter upper limit for range ::
200
11 ; 101 ; 131 ; 151 ; 181 ; 191 ;

import java.util.*;
class RevSent
{

public static void main(String args[])


{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a sentence ");
String s = sc.nextLine();
String str = s.toLowerCase();
str=str+" ";
int len = str.length();
String nsent="",w="";
for(int i=0;i<len;i++)
{
char c = str.charAt(i);
if(c!=' '&&c!='.'&&c!=','&&c!=?&&c!=!)
w=w+c;
else
{
if(c==',')
nsent = c+w+" "+nsent;
else if(c=='.'||c==?||c==!)
nsent = w+" "+nsent+c;
else
nsent =w+" "+nsent;
w="";
}
}
System.out.println("Original sentence :: "+s);
System.out.println("Final sentence :: "+nsent);
}
}
Output :
Enter a sentence
I love apples.
Original sentence :: I love apples.
Final sentence :: apples love i .

import java.util.*;
class Insert
{

public static void main(String args[])


{
Scanner sc = new Scanner(System.in);
System.out.println("Enter length of array");
int n = sc.nextInt();
int a[] = new int[n+1];
System.out.println("Enter "+n+" numbers into the array");
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
System.out.println("Original array ::");
for(int i=0;i<n;i++)
System.out.print(a[i]+",");
System.out.println();
System.out.println("Enter number to be inserted ");
int num = sc.nextInt();
System.out.println("Enter position where "+num+" is to be inserted");
int pos = sc.nextInt();
for(int i=n;i>pos;i--)
a[i]=a[i-1];
a[pos]=num;
System.out.println("Final array ::");
for (int i=0;i<n+1;i++)
System.out.print(a[i]+",");
}
}

Output:
Enter length of array
4
Enter 4 numbers into the array
3
7
9
6

Original array ::
3,7,9,6,
Enter number to be inserted
8
Enter position where 8 is to be inserted
2
Final array ::
3,7,8,9,6,

import java.util.*;
class Delete
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of array");
int len = sc.nextInt();
int a[] =new int[len];

System.out.println("Enter "+len+" numbers into the array");


for (int i=0;i<len;i++)
a[i]=sc.nextInt();
System.out.println("Original array ::");
for (int i=0;i<len;i++)
System.out.print(a[i]+",");
System.out.println();
System.out.println("Enter number to be deleted ");
int num = sc.nextInt();
int pos=0;
for(int i=0;i<len;i++)
{
if(a[i]==num)
{
pos=i;
break;
}
}
for (int i=pos;i<(len-1);i++)
a[i]=a[i+1];
System.out.println("Final array ::");
for(int i=0;i<(len-1);i++)
System.out.print(a[i]+",");
}
}

Output:
Enter size of array
4
Enter 4 numbers into the array
9
3
5
2
Original array ::
9,3,5,2,
Enter number to be deleted

3
Final array ::
9,5,2,

import java.util.*;
class Weakarm
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter number");
int n= sc.nextInt();
int copy=n,d,c=1,rev=0;
double s =0.0;

while(copy>0)
{
d=copy%10;
rev=rev*10+d;
copy/=10;
}
while(rev>0)
{
d=rev%10;
s=s+Math.pow(d,c++);
rev/=10;
}
if((int)s==n)
System.out.println(n+"is Weakarm");
else
System.out.println(n+"is not Weakarm");
}
}
Output:
Enter number
135
135 is Weakarm
Enter number
987
987 is not Weakarm

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

Scanner sc = new Scanner(System.in);


int first=0,second=1,sum=0;
System.out.println("Enter lower limit and upper limit");
int l= sc.nextInt();
int u= sc.nextInt();
System.out.println();
System.out.println("Prime fibonacci numbers are ::");
System.out.println();
while(sum<u)
{
sum=first+second;
if((sum>l) && (isprime(sum)==true)&&(sum<u))
System.out.println(sum);
first=second;
second=sum;
}
}
static boolean isprime(int b)
{
boolean flag =false;
for(int j=2;j<b/2;j++)
{
if(b%j==0)
{
flag = true;
break;
}
}
return flag;
}
}

Output:
Enter lower limit and upper limit
10
200
Prime fibonacci numbers are ::
21
34

55
144

import java.io.*;
class Insertion_Sort
{
void main(String args[])
throws IOException
{
//Insertion Sort

int A[]=new int[100];


BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int i,N, temp,count,k;
do
{
System.out.println(" How many elements ? ");
N=Integer.parseInt(br.readLine());
} while((N<2) || (N>100));
System.out.println(" Enter "+N+" elements into array ");
for(i=0; i<N; i++)
A[i]=Integer.parseInt(br.readLine());
System.out.println(" Original Array ");
for(i=0; i< N; ++i)
System.out.print(A[i]+" ") ;
temp = 0;
count =0;
for(k=1; k<N; k++)
{
temp = A[k];
count = k-1;
while((temp <=A[count]) && (count>=0))
{
A[count +1] = A[count];
count = count -1;
if(count<0)
break;
}
A[count+1] = temp;
}

//Printing the array


System.out.println();
System.out.println();
System.out.print(" Sorted array ");
for(i=0; i< N; i++)
System.out.print(A[i]+" ") ;
}
}

Output:
How many elements ?
4
Enter 4 elements into array
7
8
6
3
Original Array
7863
Sorted array
3678

import java.util.*;
class AreaOverload
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("1.Area of triangle");
System.out.println("2.Area of square");
System.out.println("3.Area of rectangle");
System.out.println("4.Area of circle");

System.out.println("5.Exit");
System.out.println("Enter choice");
int ch = sc.nextInt();
switch(ch)
{
case 1 :System.out.println("Enter base and height");
double b = sc.nextDouble();
double h = sc.nextDouble();
Area(b,h);
break;
case 2 :System.out.println("Enter length of side");
int s = sc.nextInt();
Area(s);
break;
case 3:System.out.println("Enter length and breadth");
int l = sc.nextInt();
int br = sc.nextInt();
Area(l,br);
break;
case 4:System.out.println("Enter radius");
double r = sc.nextDouble();
Area(r);
break;
case 5:break;
default : System.out.println("Wrong Choice");
}
}

static void Area(double a,double b)


{
System.out.println("Area = "+(0.5*a*b));
}
static void Area(int a)
{
System.out.println("Area = "+(a*a));
}

static void Area(int a,int b)


{
System.out.println("Area = "+(a*b));
}
static void Area(double a)
{
System.out.println("Area = "+(3.14*a*a));
}
}
Output:
1.Area of triangle
2.Area of square
3.Area of rectangle
4.Area of circle
5.Exit
Enter choice
4
Enter radius
9
Area = 254.34

class Sample_Angle
{
public void main()
{
Angle A = new Angle (34,45);
Angle B = new Angle (32,25);
Angle C = new Angle(0,0);
System.out.println("Angle A =");
A.display();
System.out.println("Angle B =");
B.display();

C=C.add(A,B);
System.out.println("Angle C =");
C.display();
}
}
class Angle
{
private int x,y;
Angle()
{
x=0;
y=0;
}
Angle(int a, int b)
{
x=a;
y=b;
}
public void display()
{
System.out.println(x+" degrees "+y+" minutes ");
}
public Angle add(Angle A,Angle B)
{
Angle C = new Angle(0,0);
C.y=A.y+B.y;
if(C.y>60)
{
C.y-=60;
C.x=A.x+B.x+1;
}
else
C.x=A.x+B.x;
return C;
}
}
Output :
Angle A =
34 degrees 45 minutes

Angle B =
32 degrees 25 minutes
Angle C =
67 degrees 10 minutes