Вы находитесь на странице: 1из 63

Number. Contents. Page No.

1. Smallest 1
2. Series 6
3. Twisted Prime. 10
4. Binary 13
5. Sort. 16
6. Longest word. 20
7. Fibonacci 22
8. Unique 26
9. Upper. 30
10. Special. 33
11. Selection Sort. 36
12. Bubble Sort 39
13. Vowels 43
14. Tokens 46
15. Consecutive Number 50
16. Even 53
17. Linear Search. 56
18. Pattern. 58
19. SDA. 62
20. Perimeter. 65
//1st to input a number more than 3 digit and find the largest and the smallest
digit of that number.

import java.io.*;

class Smallest

public static void main(String args[])throws IOException

BufferedReader br=new BufferedReader(new


InputStreamReader(System.in));

int a,i;

String n;

char q,b,c,d,e,f,s;

System.out.println("Enter any number!");

n= br.readLine();

a=n.length();

if(a>3)

s=n.charAt(a-1);

b=n.charAt(a-2);

d=n.charAt(a-3);

f=n.charAt(a-4);

if(s>b&&s>d&&s>f)
{

System.out.println(s+" is the largest digit. ");

if(b>s&&b>d&&b>f)

System.out.println(b+" is the largest digit.");

if(d>s&&d>b&&d>f)

System.out.println(d+" is the largest digit.");

if(f>s&&f>b&&f>d)

System.out.println(f+" is the largest digit.");

if(s<b&&s<d&&s<f)

System.out.println(s+" is the smallest digit. ");

if(b<s&&b<d&&b<f)

System.out.println(b+" is the smallest digit.");


}

if(d<s&&d<b&&d<f)

System.out.println(d+" is the smallest digit.");

if(f<s&&f<b&&f<d)

System.out.println(f+" is the smallest digit.");

else

System.out.println("You have to enter a number more than 3 digit!");

}
//3 wap to input a number and check whether the number is twisted prime or
not

import java.io.*;

class TwistedPrime

public static void main(String args[])throws IOException

InputStreamReader re=new InputStreamReader(System.in);

BufferedReader br=new BufferedReader(re);

int i,j,m,n,x,s,p,t; s=0;

System.out.println("Enter the no.");

n=Integer.parseInt(br.readLine());

m=n;

for(i=1;i<=n;i++)

if(n%i==0)

s=s+1;

if(s==2)

{
p=0;

while(n>0)

x=n%10;

n=n/10;

p=(p*10)+x;

t=0;

for(j=1;j<=p;j++)

if(p%j==0)

t=t+1;

if(t==2)

System.out.println(m+" is a twisted prime no.");

else

System.out.println(m+" is a twisted not a prime no.");


}

else

System.out.println(m+" is a not a twisted prime no.");

}
//10 wap to input a number and check whether the number is special or not

import java.io.*;

class special

public static void main(String args[])throws IOException

InputStreamReader re=new InputStreamReader(System.in);

BufferedReader br=new BufferedReader(re);

int i,m,n,x,f,s; s=0;

System.out.println("Enter the no.");

n=Integer.parseInt(br.readLine());

m=n;

while(n>0)

x=n%10;

f=1;

for(i=1;i<=x;i++)

f=f*i;

n=n/10;
s=s+f;

if(s==m)

System.out.println("no. is a special no.");

else

System.out.println("no. is not a special no.");

}
//5 to input a String and print it in alphabetical order

import java.io.*;

class Sort

public static void main(String args[])throws IOException

BufferedReader br=new BufferedReader(new


InputStreamReader(System.in));

int i,j,n,s;

String t;

System.out.println("Enter the value of s");

s=Integer.parseInt(br.readLine());

String a[]=new String[s];

System.out.println("Enter any name");

for(i=0;i<s;i++)

a[i]=br.readLine();

n=a.length;

for(i=0;i<n-1;i++)

for(j=0;j<n-i-1;j++)
{

if(a[j].compareTo(a[j+1])>0)

t=a[j];

a[j]=a[j+1];

a[j+1]=t;

System.out.println("Sorted list as follows:");

for(i=0;i<n;i++)

System.out.println(a[i]);

}
//6 to input a String and print and find the longest word with length..

import java.io.*;

class longest

public static void main(String args[])throws IOException

InputStreamReader re=new InputStreamReader(System.in);

BufferedReader br=new BufferedReader(re);

int i,n,m,l;

l=0;

char p;

String a,b=" ",c=" ";

System.out.println("Enter any string");

a=br.readLine();

a=a+" ";

n=a.length();

for(i=0;i<n;i++)

p=a.charAt(i);

if(p!=' ')

b=b+p;
}

else

m=b.length();

if(m>l)

l=m-1;

c=b;

b=" ";

System.out.println("Longest word = "+c);

System.out.println("Length of longest word = "+l);

}
//7 wap to input first 20 fibonacci series

class fibonacci

public static void main(String args[])

int a,b,c,i;

a=0; b=1;

System.out.println("The fibonacci series is : ");

System.out.print(a+" ");

System.out.print(b+" ");

for(i=3;i<=20;i++)

c=a+b;

System.out.print(c+" ");

a=b;

b=c;

}
//2 a menu driven program to print two types of Series.

import java.io.*;

class Series

public static void main(String args[])throws IOException

BufferedReader br=new BufferedReader(new


InputStreamReader(System.in));

int n,x;

int p=-1;

double i, s=0.0;

System.out.println("Press 1 for choice A");

System.out.println("Press 2 for choice B");

x=Integer.parseInt(br.readLine());

System.out.println("Enter the value of n");

n=Integer.parseInt(br.readLine());

switch(x)

case 1:

for(i=2;i<=n;i=i+2)

s= (i/(i+1));
System.out.println("The series is as follows:"+s);

break;

case 2:

for(i=2;i<=n;i=i+2)

p=p*(-1);

s=s+(p*(i/(i+1)));

System.out.println("The sum of series is as follows:"+s);

break;

default: System.out.println("You have entered the wrong choice");

break;

}
//8 to enter a string and check whether it is unique or not.

import java.io.*;

class Unique

public static void main(String args[])throws IOException

BufferedReader br=new BufferedReader(new


InputStreamReader(System.in));

int i,a,x,c,f=0;

String n,m;

System.out.println("Enter any string");

n=br.readLine();

a=n.length();

m=n;

for(i=1;i<=a;i++)

c=0;

while(a>0)

x=a%10;

a=a/10;
if(x==i)

c++;

if(c>1)

f=1;

break;

n=m;

if(f==1)

System.out.println(n+" is unique");

else

System.out.println(n+" is not unique");

}
//9 to change each of the first letter into upper case.

import java.io.*;

class change

public static void main(String args[])throws IOException

InputStreamReader re=new InputStreamReader(System.in);

BufferedReader br=new BufferedReader(re);

String a,b=" "; int n,i; char p,q;

System.out.println("Enter the string");

a=br.readLine();

a=" "+a;

n=a.length();

for(i=0;i<n;i++)

p=a.charAt(i);

if(p==' ')

q=a.charAt(i+1);

b=b+" "+Character.toUpperCase(q);

i=i+1;

}
else

b=b+p;

System.out.println("New String-:");

System.out.print(b);

}
//11 to input 10 integers in an array and sort them in descending order using
selection sort.

import java.io.*;

class selection

public static void main(String args[])throws IOException

InputStreamReader re=new InputStreamReader(System.in);

BufferedReader br=new BufferedReader(re);

int i,j,t;

int m[]=new int[10];

System.out.println("Enter the numbers");

for(i=0;i<10;i++)

m[i]=Integer.parseInt(br.readLine());

for(i=0;i<9;i++)

for(j=i+1;j<10;j++)

if(m[i]<m[j])

{
t=m[i];

m[i]=m[j];

m[j]=t;

System.out.println("the no. in descending order are ");

for(i=0;i<10;i++)

System.out.println(m[i]);

}
//12 to store 10 names with their weights and sort them in ascending
order.
import java.io.*;

class bubble

public static void main(String args[])throws IOException

BufferedReader br=new BufferedReader(new


InputStreamReader(System.in));

int i,j,n; String t=" ";

String a[]=new String[10];

for(i=0;i<10;i++)

System.out.println("Enter the name"+" "+"Enter the weight");

a[i]=br.readLine();

n=a.length;

for(i=0;i<n-1;i++)

for(j=0;j<n-i-1;j++)

if(a[j].compareTo(a[j+1])>0)
{

t=a[j];

a[j]=a[j+1];

a[j+1]=t;

System.out.println("Sorted list is as follows");

for(i=0;i<n;i++)

System.out.println(a[i]);

}
// 4 to input an array of size n and search the inputted number in the list with
its position using Binary Search.

import java.io.*;

class Binary

public static void main(String args[])throws IOException

BufferedReader br=new BufferedReader(new


InputStreamReader(System.in));

int i,n,s,l,u,m=0,f=0;

System.out.println("Enter the size of array");

n=Integer.parseInt(br.readLine());

int a[]=new int[n];

System.out.println("Enter any number");

for(i=0;i<n;i++)

a[i]=Integer.parseInt(br.readLine());

System.out.println("Enter the number to be searched");

s=Integer.parseInt(br.readLine());

l=0;

u=n-1;
while(l<=u)

m=(l+u)/2;

if(a[m]==s)

f=1;

break;

else if(s>a[m])

l=m+1;

else

u=m-1;

if(f==1)

System.out.println("Number found at position="+(m+1));

else
{

System.out.println("Number not found");

}
//18 to print the pattern.

class pattern

public static void main(String arg[])

int i,j;

for(i=5;i>=1;i--)

for(j=i+1;j<=5;j++)

System.out.print(j);

for(j=1;j<=i;j++)

System.out.print(j);

System.out.println();

}
//13 to accept a String and count the number of Vowels with their frequencies.

import java.io.*;

class Vowels

public static void main(String args[])throws IOException

BufferedReader br=new BufferedReader(new


InputStreamReader(System.in));

int j,n,a=0,e=0,i=0,q=0,u=0,s=0,t=0;

String d="";

char p;

System.out.println("Enter any string");

d=br.readLine();

n=d.length();

for(j=0;j<n;j++)

p=d.charAt(j);

if(p=='A'||p=='a')

a=a+1;

if(p=='E'||p=='e')
{

e=e+1;

if(p=='I'||p=='i')

i=i+1;

if(p=='O'||p=='o')

q=q+1;

if(p=='U'||p=='u')

u=u+1;

if(p==' ')

s=s+1;

t=a+e+i+q+u;

System.out.println("No.of vowel A="+a);


System.out.println("No.of vowel E="+e);

System.out.println("No.of vowel I="+i);

System.out.println("No.of vowel O="+q);

System.out.println("No.of vowel U="+u);

System.out.println("Total no.of vowels="+t);

}
//16 to input an array to store 20 different numbers and display the sum of
even and odd numbers seperately.

import java.io.*;

class Even

public static void main(String args[])throws IOException

InputStreamReader re=new InputStreamReader(System.in);

BufferedReader br=new BufferedReader(re);

int i,b=0,d=0,n,j;

int a[]=new int[20];

System.out.println("Enter the numbers");

for(i=0;i<20;i++)

a[i]=Integer.parseInt(br.readLine());

n=a.length;

for(i=0;i<n;i++)

if(a[i]%2==0)

b=b+a[i];
}

else

d=d+a[i];

System.out.println("Sum of even numbers="+b);

System.out.println("Sum of odd numbers="+d);

}
//17 to input 10 names in array and check whether the name is present or not
using Linear Search.

import java.io.*;

class Linear

public static void main(String args[])throws IOException

BufferedReader br=new BufferedReader(new


InputStreamReader(System.in));

int i,f=0;

String s;

String a[]=new String[10];

System.out.println("Enter any name");

for(i=0;i<10;i++)

a[i]=br.readLine();

System.out.println("Enter the name to be searched.");

s=br.readLine();

for(i=0;i<10;i++)

if(a[i]==s)
{

f=1;

break;

if(f==1)

System.out.println("Name found at position= "+(i+1));

else

System.out.println("Name not found!");

}
//14 to store a line and print the numeric token present in it.

import java.io.*;

class Tokens

public static void main(String args[])throws IOException

BufferedReader br=new BufferedReader(new


InputStreamReader(System.in));

int i,n;

char s=' ';

System.out.println("Enter any String!");

String a=br.readLine();

n=a.length();

for(i=0;i<n;i++)

s=a.charAt(i);

if(s.equals(0)||s.equals(1)||s.equals(2)||s.equals(3)||s.equals(4)||s.equals(5)||s
.equals(6)||s.equals(7)||s.equals(8)||s.equals(9))

System.out.println("Numeric Token="+s);

}
else

System.out.println("No Numeric Token present!");

}
//15 to input n numbers in an array and display all those numbers which are
consecutive.

import java.io.*;

class consecutive

public static void main(String args[])throws IOException

InputStreamReader re=new InputStreamReader(System.in);

BufferedReader br=new BufferedReader(re);

int n,i,p,j; p=1;

System.out.println("Enter the value of n");

n=Integer.parseInt(br.readLine());

int a[]=new int[n];

System.out.println("Enter any no.");

for(i=0;i<n;i++)

a[i]=Integer.parseInt(br.readLine());

n=a.length;

for(j=0;j<n-1;j++)

if(a[j]+p==a[j+1])
{

System.out.print(a[j]);

System.out.print(a[j+1]);

}
//19 to print 10 country names with their capitals.

import java.io.*;

class Country

public static void main(String args[])throws IOException

BufferedReader br=new BufferedReader(new


InputStreamReader(System.in));

String Country[]=new String[10];

String Capital[]=new String[10];

int i;

for(i=0;i<10;i++)

System.out.println("Enter the name of country at index:"+(i+1));

Country[i]=br.readLine();

System.out.println("Enter the name of capital at index:"+(i+1));

Capital[i]=br.readLine();

System.out.println("Country \t"+"\t"+"\tCapital");

for(i=0;i<10;i++)

System.out.println(Country[i]+" "+"\t"+"\t"+"\t"+Capital[i]); }}}


//20th to find the perimeter of Square, Rectangle and Circle using function
overloading.

import java.io.*;

class Perimeter

void simple(double s)

double P=0.0;

P=4*s;

System.out.println("Perimeter of Square is="+P);

void simple(double l,double b)

double p=0.0;

p=2*(l+b);

System.out.println("Perimeter of Rectangle="+p);

void simple(float r)

float a=0.0f;

a=(float)(2*3.14*r);

System.out.println("Perimeter of Circle is="+a);


}

public static void main(String args[])throws IOException

BufferedReader br=new BufferedReader(new


InputStreamReader(System.in));

double s,l,b,r;

System.out.println("Enter the side of Square");

s=Double.parseDouble(br.readLine());

System.out.println("Enter the length of Rectangle");

l=Double.parseDouble(br.readLine());

System.out.println("Enter the width of Rectangle");

b=Double.parseDouble(br.readLine());

System.out.println("Enter the radius of the Circle");

r=Float.parseFloat(br.readLine());

Perimeter i=new Perimeter();

i.simple(s);

i.simple(l,b);

i.simple(r)

Вам также может понравиться