Blog

PROGRAM TO CHECK WHETHER THE NUMBER IS KAPREKAR OR NOT

04/01/2016 08:09
import java.util.*;
public class Kaprekar
{
public static void main(String args[])
{
 Scanner sc=new Scanner(System.in);
 System.out.println("Enter a number");
 int num=sc.nextInt(); 
 int sq=num*num; String sq1=Integer.toString(sq);
 int mid=(Integer.toString(sq).length())/2;
 int rem=sq%((int)Math.pow(10,sq1.length()-mid));
 int quo=sq/((int)Math.pow(10,sq1.length()-mid));
 int sum=rem+quo;
 if(sum==num)
 {System.out.println("Kaprekar");}else{System.out.println("Not Kaprecar");}
}
}

Write a program to design a calculator

06/02/2014 13:13
import java.io.*;
public class calculator
{
public static void main(String args[])throws IOException
{
InputStreamReader ir=new InputStreamReader(System.in);
BufferedReader br =new BufferedReader(ir);
System.out.println("enter choice 1,2,3 or 4");
System.out.println("enter choice 1 to add");
System.out.println("enter choice 2 to subtract");
System.out.println("enter choice 3 to multiply");
System.out.println("enter choice 4 to divide");
int choice=Integer.parseInt(br.readLine());
System.out.println("enter two numbers");
double a=Double.parseDouble(br.readLine());
double b=Double.parseDouble(br.readLine());
switch(choice)
{
case 1: System.out.println(a+b);
break;
case 2: System.out.println(a-b);
break;
case 3: System.out.println(a*b);
break;
case 4: System.out.println(a/b);
break;
default: System.out.println("enter correct choice again");
}
}
}
 

Write a program to print numbers 1 to 100

06/11/2013 16:43
public class prime
{
 static boolean prime1(int n)  /* defining a function to check whether the number is prime or not  */
 {
 int sum=0;          /*define a variable to check the number of factors for n*/    
 for(int j=1;j<=n;j++)
 {
 if(n%j==0)
 {
 sum++;
}
}
if(sum==2)
return(true);
else
return(false);
}
public static void main(String args[])
{
for(int i=2;i<=100;i++)
{
if(prime1(i))/* By doing this value of i will be stored in n variable defined in above function*/
{
System.out.println(i);
}
}
}
}

WAP to accept array values from user and sort them using bubble sort

18/10/2013 17:49
import java.io.*;
public class accepting
{
public static void main(String args[])throws IOException
{
System.out.println("Enter 5 numeric values");
int[]a=new int[5];
int b=a.length;
int t;
for(int l=0;l<=b-1;l++)
{
InputStreamReader ir=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ir);
System.out.print("Enter the data of "+(l+1)+" value");
a[l]=Integer.parseInt(br.readLine());
}
for(int i=0;i<=b-1;i++)
{
for(int j=0;j<b-1;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
System.out.println("the sorted elements are is");
for(int k=0;k<b;k++)
{
System.out.println(a[k]+" ");
}
}
}

WAP to sort string arrays using bubble sort

18/10/2013 16:41
public class bubblestring
{
public static void main(String args[])
{
String[]a={"delhi","mumbai","agra","chennai","orrisa","indore"};
String t;
int b=a.length;
for(int i=0;i<=b-1;i++)
{
for(int j=0;j<b-1;j++)
{
if(a[j+1].compareTo(a[j])<1)
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
for(int k=0;k<b;k++)
{
System.out.println(a[k]+" ");
}
}
}
 

WAP to search a value in a array using binary search.Value should be accepted from the user

17/10/2013 21:30
import java.io.*;
public class binarysearch
{
public static void main(String args[])throws IOException
{
System.out.println("enter the element");
InputStreamReader ir=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ir);
int[]a={12,43,67,77,95,100,105,900,1000,1500,3000};
int b=a.length;
String c=br.readLine();
int value=Integer.parseInt(c);
int mid;
int l=0;
int u=a.length-1;
int pos=-1;
while(pos==-1&&u>=l)
{
mid=u+l/2;
if(a[mid]==value)
{
pos=mid;
}
if(a[mid]<value)
{
l=mid+1;
}
if(a[mid]>value)
{
u=mid-1;
}
}
if(pos==-1)
System.out.println("the element is not found");
else
System.out.println("the element is found and it is at "+pos);
}
}
 

WAP to search a value in a array using linear search.Value should be accepted from the user

17/10/2013 21:28
import java.io.*;
public class linearsearch
{
public static void main(String args[])throws IOException
{
System.out.println("please enter a value");
InputStreamReader ir=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ir);
int []a={81,90,100,26};
int b=a.length;
String c=br.readLine();
int value=Integer.parseInt(c);
int pos=0;
for(int i=0;i
{
if(a[i]==value)
{
pos=i+1;
}
}
if(pos>0)
System.out.println("the array is found and it is at "+pos);
else
System.out.println("the array is not found");
}
}

 

Sort an array using bubble sort

17/10/2013 17:44
public class bubblesort
{
public static void main(String args[])
{
int[]a={25,12,32,90,1};
int b=a.length;
int t;
for(int i=0;i<=b-1;i++)
{
for(int j=0;j<b-1;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
for(int k=0;k<=b-1;k++)
{
System.out.println(a[k]+" ");
}
}
}

Absolutely correct without a single error

 

Sort an array using selection sort

17/10/2013 20:18
public static void main(String args[])
{
int[]a={12,33,1,3,2,5};
int b=a.length;
int max;
int p;
for(int i=0;i<=b-1;i++)
{
max=a[i];
p=i;
for(int j=i+1;j<b;j++)
{
if(a[j]>max)
{
max=a[j];
p=j;
}
}
a[p]=a[i];
a[i]=max;
}
for(int k=0;k<b;k++)
{
System.out.println(a[k]+" ");
}
}
}
 

Accept two string values from user and find out their length,convert them into upper case and lower case and check whether they are equal

04/07/2013 11:46
import java.io.*;
public class acceptingvalues
{
public static void main(String args[])throws IOException
{
InputStreamReader ir=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ir);
System.out.println("enter two strings");
String a=br.readLine();
String b=br.readLine();
System.out.println(a.length());
System.out.println(b.length());
System.out.println(a.toUpperCase());
System.out.println(b.toLowerCase());
System.out.println(a.equals(b));
System.out.println(b.equalsIgnoreCase(a));
}
}
1 | 2 | 3 | 4 >>