Thursday, 8 October 2015

cs java progrmes

                                       
1) Write a test program that prompts the user to enter an integer
and reports whether the integer is a palindrome
Program:
/*
* To change this license header, choose License Headers in
Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package clshm2.hm3;
/**
*
* @author kishore
*/
import java.util.*;
public class PalindromeNumber {
public static boolean isPalindrome(int number) {
int palindrome = number;
int reverse = 0;
while (palindrome != 0) {
int remainder = palindrome % 10;
reverse = reverse * 10 + remainder;
palindrome = palindrome / 10;
}

if (number == reverse) {
System.out.println(number+" Is a Palindrome");
return true;
}
System.out.println(number+" Is Not a Palindrome");
return false;
}
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int number=sc.nextInt();
PalindromeNumber.isPalindrome(number);
}
}
Output:
run:
Enter a number
575
575 Is a Palindrome
BUILD SUCCESSFUL (total time: 12 seconds)
run:
Enter a number

345
345 Is Not a Palindrome
BUILD SUCCESSFUL (total time: 3 seconds)
2) write a java program to calculate BMI
Program:
/*
* To change this license header, choose License Headers in
Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package clshm2.hm3;
/**
*
* @author kishore
*/
import java.util.Scanner;
public class BMI {
Scanner input = new Scanner(System.in);
public static void calculateBMI(double weight, double
height){
double pounds=weight;
double inches=height;
double inches2=inches*inches;
double bmi = (pounds / inches2)*703;

System.out.println("Your Body Mass Index is " + bmi);
if (bmi < 18.5)
System.out.println("You are underweight");
else if (bmi < 24.99)
System.out.println("You are normal weight");
else if (bmi < 29.99)
System.out.println("You are overweight");
else if (bmi < 34.99)
System.out.println("You are seriously overweight");
else
System.out.println("You are gravely overweight");
}
public static void main(String args[]){
Scanner sc= new Scanner(System.in);
System.out.println("if u want to convert kgs to pounds enter
1 else 0");
int ch = sc.nextInt();
if(ch==1)
{
System.out.println( "enter the kgs");
double kgs=sc.nextDouble();
double pounds=kgs*2.20462;
System.out.println("you are weight in pounds=
"+pounds);

}
System.out.println("Enter weghit in pounds");
double pounds=sc.nextDouble();
System.out.println("if u want to convert feets to inches
enter 1 else 0");
int ch1 = sc.nextInt();
if(ch1==1)
{
System.out.println( "enter the feets");
double feets=sc.nextDouble();
double inches=feets*12;
System.out.println("you are height in inches= "+inches);
}
System.out.println("enter heghit in inches");
double inches=sc.nextDouble();
calculateBMI(pounds,inches);
}
}
Output:
1.
run:
if u want to convert kgs to pounds enter 1 else 0
1
enter the kgs
56
you are weight in pounds= 123.45871999999999
Enter weghit in pounds
123.45

if u want to convert feets to inches enter 1 else 0
1
enter the feets
5.7
you are height in inches= 68.4
enter heghit in inches
68.4
Your Body Mass Index is 18.549585769980503
You are normal weight
BUILD SUCCESSFUL (total time: 22 seconds)
2.
run:
if u want to convert kgs to pounds enter 1 else 0
0
Enter weghit in pounds
123.459
if u want to convert feets to inches enter 1 else 0
0
enter heghit in inches
68.4
Your Body Mass Index is 18.55093810916179
You are normal weight
BUILD SUCCESSFUL (total time: 28 seconds)

3a) Linear Search
Program:
package clshm3;
public class LinearSearch {
public static void main(String args[]){
LinearSearch ls=new LinearSearch();
ls.lineraSearch(4);
ls.lineraSearch(-4);
ls.lineraSearch(-3);
}
public void lineraSearch (int num){
int list[]={1,4,-4,2,5,-3,6,2};
for(int i=0;i<list.length;i++){
if(num==list[i])
{
System.out.println(num+" number found at index position "+i);
}
}
}
}
Output:
4 number found at index position 1
-4 number found at index position 2
-3 number found at index position 5
3b) Binary Search:
Program:
package clshm3;
public class BinarySearch {
public static void main(String[] args) {
int[] list = {2, 4, 7, 10, 11, 45, 50, 59, 60, 66, 69, 70, 79};
int i = BinarySearch.binarySearch(list, 2);
System.out.println(i);
int j = BinarySearch.binarySearch(list, 11);
System.out.println(j);
int k = BinarySearch.binarySearch(list, 12);
System.out.println(k);
int l = BinarySearch.binarySearch(list, 1);
System.out.println(l);
int m = BinarySearch.binarySearch(list, 3);
System.out.println(m);
}
public static int binarySearch(int[] list, int key) {
int low = 0;
int high = list.length - 1;
while (high >= low) {
int mid = (low + high) / 2;
if (key < list[mid])
high = mid - 1;
else if (key ==list[mid])
return mid;
else
low = mid + 1;
}
return - 1;
}
}
Output: 0
4
-1
-1
-1
3c) Selection Sort
Program:
package clshm3;
import java.util.Scanner;
public class SelectionSort
{
public void Selectionsort(double[] arr){
for(int i=0; i<arr.length; i++)
{
for(int j=i+1; j<arr.length; j++)
{
if(arr[i] > arr[j] )
{
double temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
}
for(int i=0; i<arr.length; i++)
{
System.out.print(arr[i] + " ");
}
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
int n = sc.nextInt();
double [] x = new double[n];
System.out.print("Enter "+ n +" numbers: ");
for(int i=0; i<n; i++)
{
x[i] = sc.nextDouble();
}
SelectionSort access = new SelectionSort();
System.out.print("The Sorted numbers: ");
access.Selectionsort(x);

}
}
Output:
Enter the size of the array: 5
Enter 5 numbers: 1.9
4.5
6.6
5.7
-4.5
The Sorted numbers: -4.5 1.9 4.5 5.7 6.6


4) Write a program that prompts the user to enter two sorted lists
and displays the merged list.
Program:
package clshm3;
import java.util.*;
public class Merge {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter list1 size and elements : ");
int n = sc.nextInt();
int[] array1 = new int[n];
for(int i = 0 ; i < n ; i++){
array1[i]=sc.nextInt();
}
System.out.println("Enter list2 size and elements : ");
int m = sc.nextInt();
int[] array2 = new int[m];
for(int i =0 ; i < m ; i++){
array2[i]=sc.nextInt();
}
display(merge(array1,array2));
sc.close();
}
public static int[] merge(int[] list1 , int[] list2){
int count = 0;
boolean WTF = false;
int k = 0;
int[] m = new int[list1.length + list2.length];
for(int i = 0 ; i < list1.length; i++){
for(int j = k ; j < list2.length ; j++){

if(list1[i] < list2[j]){
m[count] = list1[i];
count++;
break;
}
if(list1[i]> list2[j]){
m[count] = list2[j];
count++;
k= j + 1 ;
}
if(list1[i] == list2[j]){
m[count] =list1[i] ;
count++;
break;
}
}
if (k > list2.length -1){
m[count] = list1[i];
count++;
}
}
int left = list1[list1.length -1];
for(int i = 0 ; i < list2.length ; i++ ){
if(list2[i] > left){
left = i;
WTF = true;
break;}
}
if(WTF){
for(int i = left ; i < list2.length ; i++){
m[count] = list2[i];
count++;
}
}
return m;
}
public static void display(int[] array){
System.out.print("the merged list is "
+ " ");
for(int i = 0 ; i < array.length ; i++){
System.out.print(array[i]+" ");
}
}
}
Output:
Enter list1 size and elements :
5
1 5 16 61 111
Enter list2 size and elements :
4
2 4 5 6
the merged list is 1 2 4 5 5 6 16 61 111

No comments:

Post a Comment