Computer Projectblue J

  • Uploaded by: Kellie Thomas
  • 0
  • 0
  • July 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Computer Projectblue J as PDF for free.

More details

  • Words: 13,221
  • Pages: 71
Computer ProjectBlue J BY P.HARSHA

1.To Print The Roots of a Quadratic Equation/ import java.io.* ; public class quadratic { public static void main()throws IOException {

//Throwing of io exceptions

BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //For input from keyboard System.out.println("Enter the value of a"); //For value of 'a' from user System.out.println("Enter the value of b"); //For value of 'b' from user System.out.println("Enter the value of c"); //For value of 'c' from user int a=Integer.parseInt(s.readLine()); //Storing 'a' vlaue int b=Integer.parseInt(s.readLine()); //Storing 'b' value int c=Integer.parseInt(s.readLine()); //Storing 'c' value double d; //To store value of b^2-4ac double r; //To store value of root1 double v; //To store value of root2 d=bb-4ac; //Calculating b^2-4ac if(d>0) { r=(-b+Math.sqrt(d))/(2a); //Calculating root1 v=(-b-Math.sqrt(d))/(2a); //Calculating root2 System.out.println("ROOT 1:"+r); //Displaying the value of root1 System.out.println("ROOT 2:"+v); //Displaying the value of root2 } else if(d==0) { r=-b/2a; System.out.println("Both roots are equal and the value is:"+r); } else { System.out.println("Roots don't exist and are imaginary:"); } } }

Variable DataType Description a Integer To store the value of 'a' which is given by the user b Integer To store the value of 'b' which is given by the user c Integer To store the value of 'c' which is given by the user d Double To calculate & store the value of b^2-4ac r Double To calculate & store the value of root1 v Double To calculate & store the value of root2 / Sample Input: a=2 b=8 c=6 Output: Root 1:-1.0 Root 2:-3.0 /

2.To accept a string & count tne number of characters & spaces/ import java.io.* ; //Importing io package public class count { public static void main()throws IOException //Throwing io exceptions { BufferedReader v=new BufferedReader(new InputStreamReader(System.in)); //For input from keyboard System.out.println("Enter a sentence"); //For the string from user String r=new String(); //To store the string given by user int c=0; //To store the number of characters int s=0; //To store the number of spaces r=v.readLine(); for(int i=0;i=0&&c<=1) //A condition to extract only the day { d+=r.charAt(i); //Extraction of day is done only when the condition is true } else if(c>2&&c<=4) //A condition to extract only the month { m+=r.charAt(i); //Extraction of month is done only when the condition is true } else if(c>5) //A condition to extract only the year { y+=r.charAt(i); //Extraction of year is done only when the condition is true } c++; //Increment of 'c' is done for the above extractions } //The following conditions represent the conversion of month which is given in integer form to word/string form //After the respective conversions take place the converted string is displayed if(m.equals("01")) { System.out.println(d+" "+"January"+" "+y); } else if(m.equals("02")) { System.out.println(d+" "+"February"+" "+y); } else if(m.equals("03")) {

System.out.println(d+" "+"March"+" "+y); } else if(m.equals("04")) { System.out.println(d+" "+"April"+" "+y); } else if(m.equals("05")) { System.out.println(d+" "+"May"+" "+y); } else if(m.equals("06")) { System.out.println(d+" "+"June"+" "+y); } else if(m.equals("07")) { System.out.println(d+" "+"July"+" "+y); } else if(m.equals("08")) { System.out.println(d+" "+"August"+" "+y); } else if(m.equals("09")) { System.out.println(d+" "+"September"+" "+y); } else if(m.equals("10")) { System.out.println(d+" "+"October"+" "+y); } else if(m.equals("11")) { System.out.println(d+" "+"November"+" "+y); } else if(m.equals("12")) { System.out.println(d+" "+"December"+" "+y); } else { System.out.println("Invalid Date"); //In this case if the give date is wrong this statement is displayed } } }

Variable

DataType

Description

r d m y c i /

String String String String Integer Integer

To store the date given by the user To extract & store the day To extract & store the month To extract & store the year To store only the number of characters required Counter Variable for teminating the loop

Sample Input: 15/08/1947 Output: 15 August 1947 / 4.To print the first n even numbers & n odd numbers/ import java.io.*; //Importing io package public class sumevenodd { public static void main()throws IOException //Throwing io exceptions { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input System.out.println("Enter a number"); //For getting the number from the user int n; //To store the given number n=Integer.parseInt(s.readLine()); //Storing the number for(int i=1;i<=n;i++) //A loop for getting all the numbers below the given number { if(i%2==0) //Checking whether the number is even { System.out.println("Even Number:"+i); //The number will be displayed if it is even } else //If the condition is false then it is odd { System.out.println("Odd Numbers:"+i); //The number will be displayed if it is not even but odd } } } } Variable DataType Description n Integer To store the number given by the user i Integer Counter Variable for terminating the loop as well as used for checking whether it is even or odd / Sample Input: 10 Output: Odd Number:1

Even Number:2 Odd Number:3 Even Number:4 Odd Number:5 Even Number:6 Odd Number:7 Even Number:8 Odd Number:9 Even Number:10 / 5.To print all multiples of 4 between 1 & 100/ public class multiples { public static void main(String args[]) //A function to perform the process { int m=1; //A variable to store the multiples int n=100; //A variable to terminate the loop System.out.println("Multiples of 4 between 1 & 100:"); for(int i=2;i
Variable DataType Description m Integer To store the multiples of 4 n Integer For terminating the loop i Integer Counter Variable also used for terminating the loop / Multiples of 4 between 1 & 100: 336 340 344 348 352 356 360 364 368 372 376 380 384 388

392 396 / 6.To print the n terms in the fibonacci series/ import java.io.; //Importing io package public class fibonacci { public static void main()throws IOException //Throwing io exceptions { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input System.out.println("Enter a Number"); //For input from user int n; //Declaring a variable for fibonacci series int a=0; //Declaring a variable for swapping to get the fibonacci series int b=1; //Declaring a variable for swapping to get the fibonacci series int c=0; //Declaring a variable for swapping to get the fibonacci series n=Integer.parseInt(s.readLine()); //Converting string to integer System.out.print("Fibonacci series of the given number:"+a+" "+b+" "); //Displaying the values of a&b as per the fibonacci series for(int i=2;i
Variable DataType n Integer a Integer during swapping b Integer during swapping c Integer during swapping i Integer /

Description To store the value given by the user To display the first value of the fibonacci series as well as To display the second value of the fibonacci series as well as To display the later values of the fibonacci series as well as To terminate the loop

Sample Input: 10 Output: Fibonacci series of the given number:0 1 1 2 3 5 8 13 21 34 55 89 /

7.To print the factors of a given number/ import java.io.; //Importing io package public class factor { public static void main()throws IOException //Throwing io exceptions { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input System.out.println("Enter a Number"); //For input from user int n; //Declaring a variable to store the given number n=Integer.parseInt(s.readLine()); //Converting string to integer System.out.println("Factors of the given number:"); for(int i=1;i<=n;i++) //A loop for getting the factors { if(n%i==0) //A condition to get only the factors of the given number { System.out.println(i); //Displaying the factors of the given number } } } }

Variable DataType Description n Integer To store the given number i Integer Counter Variable as well as to terminate the loop / Sample Input: 10 Output: Factors of the given number: 1 2 5 10 /

8.To print the factorial of a given number/ import java.io.; //Importing io package public class factorial { public static void main()throws IOException //Throwing io exceptions { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input

System.out.println("Enter a number"); int n; //To store the given number int f=1; //To store the factorial of a given number n=Integer.parseInt(s.readLine()); //Converting string to integer for(int i=1;i<=n;i++) //A loop for getting the factorial { if(n%i==0) //A condition for getting the factors of the given number { f=i; //Getting the factorial of the given number } } System.out.println("Factorial of the given number="+f); //Displaying the factorial of the given number } }

Variable DataType Description n Integer To store the given number f Integer To store the factorial of the given number i Integer Counter Variable as well as to terminate the loop / Sample Input: 6 Output: Factorial of the given number=36 / 9.To find the sum of the digits of a given number/ import java.io.; //Importing io package public class sum { public static void main()throws IOException //Throwing io exceptions { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input System.out.println("Enter a Number"); //For input from user int n; //Declaring a variable to store the given number int rev=0; //Declaring a variable to get a single digit every time int rem=0; //Declaring a variable to store the sum of the digits n=Integer.parseInt(s.readLine()); //Converting string to integer for(int i=1;i<=n;i++) //A loop for getting the digits { rem=n%10; //Storing the digit rev+=rem; //Adding to get the sum of the digits n=n/10; //removal of the digit after adding } System.out.println("Sum of the digits in the given number="+rev); //Displaying the sum of the digits

} }

Variable DataType Description n Integer To store the given number rev Integer To store a single digit every time rem Integer To store the sum of the digits i Integer Counter Variable as well as to terminate the loop / Sample Input: 123 Output: Sum of the digits in the given number=6 / 10.To check whether the given number is palindrome/ import java.io.; //Importing io package public class palindrome { public static void main()throws IOException //Throwing io exceptions { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input System.out.println("Eneter a number"); //For input from user int n; //Declaring a variable to store the given number int rem; //Declaring a variable used during reversal of a number is taken place int rev=0; //Declaring a variable used during reversal of a number is taken place int p; //Declaring a variable to store the number given so that it can be used for comparing n=Integer.parseInt(s.readLine()); //Converting string to integer p=n; //Assigning n value to p for(;n>0;) //A loop for reversal of the number { //The process shown below is for reversal of a number rem=n%10; rev=rev10+rem; n=n/10; } if(p==rev) { //If the given number is equal to the reversed number then the following statement is displayed System.out.println("The given number is a Palindrome Number"); } else

{ //If the given number is not equal to the reversed number then the following statement is displayed System.out.println("The given number is not a Palindrome Number"); } } } Variable n rem rev p

DataType Integer Integer Integer Integer

Description To store the given number To store the remainder of the number Used during reversal of a number is taken place To store the number given so that it can be used for comparing

Sample Input: 121 Output: The given number is a Palindrome Number / 11.To check whether a given number is prime or not/ import java.io.; //Importing io package public class prime0 { public static void main()throws IOException //Throwing io exceptions { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input System.out.println("Enter a number"); //For input from user int n; //Declaring a variable to store the given number int p=0; //Declarint a variable to store the sum of the factors n=Integer.parseInt(s.readLine()); //Converting string to integer for(int i=2;i
}

Variable DataType Description n Integer To store the given number p Integer To store the sum of the factors i Integer Counter Variable as well as to terminate the loop

Sample Input: 11 Output: The given number is a Prime Number / 12.To check whether a given number is perfect number or not/ import java.io.; //Importing io package public class perfect { public static void main()throws IOException //Throwing io exceptions { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input System.out.println("Enter a number"); //For input from user int n; //Declaring a variable to store the given number int f=0; //Declaring a variable used for storing the sum of factors n=Integer.parseInt(s.readLine()); //Converting string to integer for(int i=1;i
Variable DataType Description n Integer To store the given number f Integer Used for storing the sum of factors i Integer Counter Variable as well as to terminate the loop / Sample Input: 28 Output: The given number is a Perfect Number /

13.To check whether a given number is special number or not/ import java.io.; //Importing io package public class special { public static void main()throws IOException //Throwing io exceptions { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input System.out.println("Enter a number"); //For input from user int n; //Declaring a variable to store the given number int p; //For assigning values int rem=0; //Declaring a variable to extract a single number every time int rev=0; //Declaring a variable to store the sum of the factorial n=Integer.parseInt(s.readLine()); //Convertint string to integer p=n; //Assigning n value to p while(n>0) //A loop for getting single number every time { rem=n%10; //Getting single number rev+=fact(rem); //Getting the sum of factorial n=n/10; //Removing the number } if(p==rev) //If the sum of the factorials is equal to the given number then it is a special number { System.out.println("The given number is a Special Number"); } else //If the sum of the factorials is not equal to the given number then it is not a special number { System.out.println("The given number is not a Special Number"); } } public static int fact(int v)throws IOException //Throwing io exceptions { int f=1; //Declaring a variable to store the factorial of the given number for(int i=1;i<=v;i++) //A loop to get the factorial {

f=i; } return f;

//Returning the factorial of the number

} }

Variable DataType n Integer p Integer rem Integer rev Integer v Integer i Integer /

Description To store the given number To check the given number To extract a single number every time To store the sum of the factorial To store the single number passed while calling the function Counter Variable as well as to terminate the loop

Sample Input: 145 Output: The given number is a Special Number /

14.To chack whether a given number is armstrong or not/ import java.io.; //Importing io package public class armstrong { public static void main()throws IOException //Throwing io exceptions { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input System.out.println("Eneter a number"); //For input from user int n; //Declaring a variable to store the given number int rem; //Declaring a variable used during calculating the armstrong of a number is taken place int rev=0; //Declaring a variable used during calculating the armstrong of a number is taken place int p; //Declaring a variable to store the number given so that it can be used for comparing n=Integer.parseInt(s.readLine()); //Converting string to integer p=n; //Assigning n value to p while(n>0) //A loop for calculating the armstrong of the number { //The process shown below is for calculating the armstrong of a number rem=n%10; rev+=remremrem;

n=n/10; } if(p==rev) { //If the given number is equal to the armstrong number then the following statement is displayed System.out.println("The given number is an Armstrong Number"); } else { //If the given number is not equal to the armstrong number then the following statement is displayed System.out.println("The given number is not an Armstrong Number"); } } }

Variable n rem rev p /

DataType Description Integer To store the given number Integer Used during calculating the armstrong of a number Integer Used during calculating the armstrong of a number Integer To check the given number

Sample Input: 153 Output: The given number is an Armstrong Number / 16.To print the prime numbers between any user specified range/ import java.io.; //Importing io package public class prime { public static void main(String args[])throws IOException //Throwing io exceptions { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input System.out.println("Enter two numbers"); //For input from user int a; //Declaring a variable to store the first value int b; //Declaring a variable to store the second value a=Integer.parseInt(s.readLine()); //Converting string to integer b=Integer.parseInt(s.readLine()); //Converting string to integer for(int i=a+1;i
} } } public static boolean check(int n)throws IOException { int p=0; //Declaring a variable to store the sum of the factors for(int i=2;i
DataType Description Integer To store the first value Integer To store the second value Integer To store the sum of the factors Integer Counter Variable as well as to terminate the loop

Sample Input: 2 10 Output: Prime Number:3 Prime Number:5 Prime Number:7 /

17.c:-To print a series/ public class series_3 { public static void main() { for(int i=1;i<=4;i++) { for(int j=1;j<=4-i;j++)

//A loop to get the number of rows //A loop to get the number of spaces

{ System.out.print(" "); } for(int j=1;j<=i;j++) //A loop to get the number { System.out.print(j); } for(int j=i-1;j>=1;j--) //A loop to get the number again { System.out.print(j); } System.out.println(); } } }

Variable DataType Description i Integer To get the number of columns j Integer To get the number of spaces & the numbers / Output:

1 121 12321 1234321

/ 17.d:-To print a series/ public class series_4 { public static void main() { //The below loop is for the first series for(int i=1;i<=4;i++) //A loop to get the number of rows { for(int j=1;j<=i;j++) //A loop to get the number of columns { System.out.print(j+" "); //Displaying 'j' to get the output } System.out.println(); } //The below loop is to get the second series for(int i=3;i>=1;i--) //A loop to get the number of rows { for(int j=1;j<=i;j++) //A loop to get the number of columns { System.out.print(j+" ");

} System.out.println(); } } }

Variable DataType Description i Integer To get the number of rows j Integer To get the number of columns / Output:

1 12 123 1234 123 12 1

/ 18.Write a program to calculate and print the sum of odd numbers & sum of even numbers for the first 'n' natural numbers/ import java.io.; //Importing io package public class oddeven { public static void main()throws IOException //throwing io exceptions { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input System.out.println("Enter a number"); //For user to input int n; //Declaring a variable to store the given number int r=0; //Declaring a variable to store the sum of odd numbers int v=0; //Declaring a variable to store the sum of even numbers n=Integer.parseInt(s.readLine()); //Converting string to integer for(int i=1;i<=n;i++) //A loop to get all the numbers below n { if(i%2==0) //A condition to check whether the given number is even { v+=i; //Adding the even numbers } else //If the condition is false then it is odd { r+=i; //Adding the odd numbers } }

System.out.println("Sum of Odd Numbers="+r); numbers System.out.println("Sum of Even Numbers="+v); numbers } }

Variable DataType n Integer r Integer v Integer i Integer /

//Displaying the sum of odd //Displaying the sum of even

Description To store the given number To store the sum of odd numbers To store the sum of even numbers Counter Variable as well as to terminate the loop

Sample Input: 10 Output: Sum of Odd Numbers=25 Sum of Even Numbers=30 /

20.Write a class to input a number and print it's odd factors/ import java.io.; //Importing io package public class oddfactor { public static void main()throws IOException //throwing io exceptions { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input System.out.println("Enter a number"); //For user to input int n; //Declaring a variable to store the given number n=Integer.parseInt(s.readLine()); //Converting string to integer System.out.print("The Odd Factors of the given number are:"+" "); for(int i=1;i<=n;i++) //A loop to get all the numbers below n { if(i%2!=0) //A condition to check whether the given number is odd { if(n%i==0) //A condition to check whether the given number is a factor of n { System.out.print(i+" "); //Displaying the odd factors } } } } }

Variable DataType Description n Integer To store the given number i Integer Counter Variable as well as to terminate the loop / Sample Input: 10 The Odd Factors of the given number are: 1 5 / 21.a:-To print a series/ import java.io.; //Importing io package public class series1 { public static void main()throws IOException {

//Throwing io exceptions

BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input System.out.println("Enter a number"); //For user to input int n; //Declaring a variable to store the given number int v=0; //Declaring a variable to store the sum n=Integer.parseInt(s.readLine()); //Converting string to integer for(int i=1;i<=n;i++) //A loop to get all the numbers below n { v+=i; //Adding the numbers below n } System.out.println("Sum of the numbers="+v); //Displaying the sum of the numbers } }

Variable DataType Description n Integer To store the given number v Integer To store the sum of the numbers i Integer Counter Variable as well as to terminate the loop / Sample Input: 20 Output: Sum of the numbers=210 / 21.b:-To print a series/ import java.io.; //Importing io package public class series2 { public static void main()throws IOException //Throwing io exceptions { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input System.out.println("Enter a number"); //For user to input int n; //Declaring a variable to store the given number int v=0; //Declaring a variable to store the sum n=Integer.parseInt(s.readLine()); //Converting string to integer for(int i=1;i<=n;i++) //A loop to get all the numbers below n { v+=Math.pow(i,2); //Adding the squares of the given numbers } System.out.println("Sum of their squares="+v); //Displaying the sum of their squares } }

Variable DataType Description n Integer To store the given number v Integer To store the sum of square of numbers i Integer Counter Variable as well as to terminate the loop / Sample Input: 100 Output: Sum of their squares=338350 / 21.c:-To print a series/ import java.io.; //Importing io package public class series3 { public static void main()throws IOException //Throwing io exceptions { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input System.out.println("Enter a number"); //For user to input int n; //Declaring a variable to store the given number int v=0; //Declaring a variable to store the sum n=Integer.parseInt(s.readLine()); //Converting string to integer for(int i=1;i<=n;i++) //A loop to get all the numbers below n { v+=Math.pow(i,i+1); } System.out.println("Sum of their powers="+v); } }

Variable DataType Description n Integer To store the given number v Integer To store the sum of their powers i Integer Counter Variable as well as to terminate the loop / Sample Input: 1000 Output: Sum of their powers=2147483647 / 21.d:-To print a series/ import java.io.; //Importing io package public class series4 {

public static void main()throws IOException //Throwing io exceptions { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input System.out.println("Enter a number"); //For user to input int n; //Declaring a variable to store the given number int v=0; //Declaring a variable to store the sum int sum=0; //Declaring a variable to store the sum n=Integer.parseInt(s.readLine()); //Converting string to integer for(int i=1;i<=n;i++) //A loop to get all the numbers below n { v+=i; //To add the numbers including their previous numbers sum+=v; //To add the numbers } System.out.println("Sum="+sum); //Displaying the sum of the numbers } }

Variable DataType Description n Integer To store the given number v Integer To store the sum of numbers including it's previous numbers sum Integer To store the sum of the numbers i Integer Counter Variable as well as to terminate the loop / Sample Input: 10 Output: Sum=220 / 21.e:-To print a series/ import java.io.; //Importing io package public class series5 { public static void main()throws IOException //Throwing io exceptions { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input System.out.println("Enter a number"); //For user to input int n; //Declaring a variable to store the given number int v=0; //Declaring a variable to store the sum n=Integer.parseInt(s.readLine()); //Converting string to integer for(int i=1;i<=n;i++) //A loop to get all the numbers below n { v+=fact(i); //Adding their factorials }

System.out.println("Sum of their factorials="+v); //Displaying the sum of factorials } public static int fact(int r) { int f=1; //Declaring a variable to store the factorial of the number for(int i=1;i<=r;i++) //A loop to get the factorial { f=i; } return f; //Returning the factorial of the given number } }

Variable DataType n Integer v Integer r Integer f Integer i Integer /

Description To store the given number To store the sum of factorials To store the number passed while calling the function To store the factorial of the number passed Counter Variable as well as to terminate the loop

Sample Input: 10 Output: Sum of their factorials=4037913 /

23.to accept a decimal number and converts into binary system/ import java.io.; //Importing io package public class binary { public static void main()throws IOException //Throwing io exceptions { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input System.out.println("Enter a Number"); //For the user to input int n; //Declaring a variable to store the given number int d=0; //Declaring a variable to store each digit String r=""; //Declaring a variable to store the binary number n=Integer.parseInt(s.readLine()); //Converting string to integer while(n>0) //A loop for getting the binary number { d=n%2; //Storing each digit r=String.valueOf(d)+r; //Storing the binary number n=n/2; //Removing the digit }

System.out.println("The number in binary form="+r); number } }

//Displaying the binary

Variable DataType Description n Integer To store the given number d Integer To store each digit r String To store the binary number / Sample Input: 12 Output: The number in binary form=1100 / 24.To find the LCM & HCF of the given 2 numbers/ import java.io.; //Importing io package public class lcmhcf { public static void main()throws IOException //Throwing io exceptions { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input System.out.println("Enter two numbers"); //For the user to input int a; //Declaring a variable to store the given number int b; //Decalring a variable to store the given number int c=0; //Declaring a variable to store the value of the smallest one int hcf=0; //Declaring a variable to store the value of the hcf after calculating int lcm=0; //Declaring a variable to store the value of the lcm after calculating a=Integer.parseInt(s.readLine()); //Converting string to integer b=Integer.parseInt(s.readLine()); //Converting string to integer if(a
System.out.println("HCF of the given numbers="+hcf); of hcf lcm=(ab)/hcf; //Calculating the value of lcm System.out.println("LCM of the given numbers="+lcm); of lcm } }

Variable a b c hcf lcm /

//Displaying the value //Displaying the value

DataType Description Integer To store the first number Integer To store the second number Integer To store the smallest number Integer To store the value of hcf Integer To store the value of lcm

Sample Input: 96 64 Output: HCF of the given numbers=32 LCM of the given numbers=192 / 25.To print the combinations of any given three digit numbers/ import java.io.; //Importing io package public class compute { public static void main()throws IOException //Throwing io exceptions { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input System.out.println("Enter a number"); //For the user to input int n; //Declaring a variable to store the given number int a=0; //Declaring a variable to store the first digit int b=0; //Declaring a variable to store the second digit int c=0; //Declaring a variable to store the third digit n=Integer.parseInt(s.readLine()); //Converting string to integer a=n%10; //Storing the first digit n=n/10; //Removing the digit b=n%10; //Storing the second digit n=n/10; //Removing the digit c=n%10; //Storing the third digit n=n/10; //Removing the digit System.out.print("The combinations are:"); //Diaplaying the statement System.out.print(c+""+b+""+a+","+" "); //Displaying the first combination System.out.print(c+""+a+""+b+","+" "); //Displaying the second combination System.out.print(b+""+c+""+a+","+" "); //Displaying the third combination

System.out.print(b+""+a+""+c+","+" "); System.out.print(a+""+c+""+b+","+" "); System.out.print(a+""+b+""+c);

//Displaying the fourth combination //Displaying the fifth combination //Displaying the sixth combination

} }

Variable n a b c /

DataType Integer Integer Integer Integer

Description To store the given number To store the first digit To store the second digit To store the third digit

Sample Input: 123 Output: The combinations are:123, 132, 213, 231, 312, 321 / 26.Write a men driven program to display the menu and do the needful: The men should be displayed as long as the user chooses to/ import java.io.; public class menu_driven { public static void main()throws IOException { char c; //For user's choice do //For continuation { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input //Displaying the options as per the question System.out.println("1.Check Prime Number"); System.out.println("2.Check Special Number"); System.out.println("3.Check Perfect Number"); System.out.println("4.Check Armstrong Number"); System.out.println("5.Check Automorphic Number"); System.out.println("6.Exit"); System.out.println("Enter your choice"); int i; //Declaring a variable to store the choice of the user int n=0; //Declaring a variable to store the given number i=Integer.parseInt(s.readLine()); //Converting string to integer switch(i) //A condition for getting the required information of the user's choice { case 1: System.out.println("Enter a number"); //For the user to input n=Integer.parseInt(s.readLine()); //Converting string to integer int p=0; //Declaring a variable to store the sum

int j=2; while(j0) //A loop to get the special number { rem=n%10; //To get a single number for(j=1;j<=rem;j++) { v=j; } rev+=v; //storing the sum of factorial n=n/10; //Removing the number v=1; } if(p==rev) //If the sum of the factorials is equal to the given number then it is a special number { System.out.println("The given number is a Special Number"); } else //If the sum of the factorials is not equal to the given number then it is not a special number { System.out.println("The given number is not a Special Number"); } break; case 3: System.out.println("Enter a number"); //For the user to input

n=Integer.parseInt(s.readLine()); //Converting string to integer int f=0; //Declaring a variable to store the sum of factors of n j=1; while(j0) //A loop for calculating the armstrong of the number { //The process shown below is for calculating the armstrong of a number rem=n%10; rev+=remremrem; n=n/10; } if(p==rev) { //If the given number is equal to the armstrong number then the following statement is displayed System.out.println("The given number is an Armstrong Number"); } else { //If the given number is not equal to the armstrong number then the following statement is displayed System.out.println("The given number is not an Armstrong Number"); } break; case 5: int mul=0; //Declaring a variable to store the square of the given number int a=0; //Declaring a variable to store each digit

int b=0; //Declaring a variable to store the reverse of the number rev=0; //Clearing the veriable rem=0; //Clearing the variable c=0; //To store the number of digits System.out.println("Enter a number"); //For the user to input n=Integer.parseInt(s.readLine()); //Converting string to integer mul=nn; //Storing the square of the given number p=n; //Assigning the value of n while(n>0) //A loop to get the number of digits { c+=1; //Storing the number of digits n=n/10; //Removing the last digit } for(i=1;i<=c;i++) //A loop to get the digits required { rem=mul%10; //Storing each digit rev=rev10+rem; //Storing the reverse of the number mul=mul/10; //Removing the number } while(rev>0) { a=rev%10; //Storing each digit b=b10+a; //Storing the reverse of the number rev=rev/10; //Removing the number } if(b==p) //A condition to check whether the given number is automorphic or not { System.out.println("The given number is an Automorphic Number"); //Displaying the statement } else { System.out.println("The given number is not an Automorphic Number"); //Displaying the statement } break; case 6: System.out.println("THANK YOU"); break; default: System.out.println("INVALID NUMBER"); } if(i!=6) //If the user wishes to conitinue without exiting then his choice is to be entered to continue or not { System.out.println("Do You Want To Continue"); c=(char)s.read(); }

else //If the user wishes to conitinue exit by entering 5 as his choice then c is initialised to blank thereby terminating the whole program { c=' '; } }while(c=='y'||c=='Y'); if(c=='n'||c=='N') //If the user enters n or N then the following statement is displayed { System.out.println("THANK YOU"); } } }

Variable i n c p some cases rem rev f j mul a b /

DataType Description Integer To store the user's choice Integer To store the number given by user Character To store the choice of the user Integer To store the sum in sum cases & to store the value of n in Integer Integer Integer Integer Integer Integer Integer

To store a single number of the given number To store the sum To store the sum of the factors of the given number To terminate the loop To store the square of the given number To store each digit To store the reverse of the number

Check Prime Number Check Special Number Check Perfect Number Check Armstrong Number Exit Enter your choice Sample Input: 1 Enter a number 2 Output: The given number is a Prime Number Do You Want To Continue Sample Input: n Output: THANK YOU /

27.a:-To print n prime numbers/ package mylist; import java.io.; //Importing io package public class prime { public boolean call(int v)throws IOException //Throwing io exceptions { int p=0; //Declaring a variable to store the sum for(int i=2;i0) //If p>0 then the number has factors so it is not a prime number { return false; } else //If p=0 then the number does not have any factors so it is a prime number { return true; } } public void cal()throws IOException //Throwing io exceptions { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input System.out.println("Enter a number"); //For the user to input int n; //Declaring a variable to store the given number n=Integer.parseInt(s.readLine()); //Converting string to integer System.out.println("Prime Numbers:"); for(int i=2;i<=n;i++) //A loop to get the numbers below n { if(call(i)) //Calling the function call() with the argument passed as i { //If the condition is true then the following statement is executed System.out.print(i+" "); } } System.out.println(); } }

Variable DataType Description n Integer To store the given number p Integer To store the sum i Integer Counter Variable as well as to terminate the loop / Sample Input: 5 Output: Prime Numbers:2 3 5 /

27.b:-To print the multiplication table of a given number/ package mylist; import java.io.; //Importing io package public class table { public void multiply()throws IOException //Throwing io exceptions { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input System.out.println("Enter a number"); //For the user to input int n; //Declaring a variable to store the given number int mul=0; //Declaring a variable to store the multipied value n=Integer.parseInt(s.readLine()); //Converting string to integer for(int i=1;i<=20;i++) //A loop to get the multiplication table { mul=ni; //Storing the multipied value System.out.println(n+"x"+i+"="+mul); //Displaying the multiplication table } } }

Variable DataType Description n Integer To store the given number mul Integer To store the multiplied value i Integer Counter Variable as well as to terminate the loop / Sample Input: Output: 4x1=4 4x2=8 4x3=12 4x4=16

4

4x5=20 4x6=24 4x7=28 4x8=32 4x9=36 4x10=40 4x11=44 4x12=48 4x13=52 4x14=56 4x15=60 4x16=64 4x17=68 4x18=72 4x19=76 4x20=80 /

27.c:-To print the factors of a given number/ package mylist; import java.io.; //Importing io package public class factor { public void get()throws IOException //Throwing io exceptions { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input System.out.println("Enter a Number"); //For input from user int n; //Declaring a variable to store the given number n=Integer.parseInt(s.readLine()); //Converting string to integer System.out.println("Factors of the given number:");

for(int i=1;i<=n;i++) //A loop for getting the factors { if(n%i==0) //A condition to get only the factors of the given number { System.out.println(i); //Displaying the factors of the given number } } } }

Variable DataType Description n Integer To store the given number i Integer Counter Variable as well as to terminate the loop / Sample Input: 10 Output: Factors of the given number: 1 2 5 10 /

27.d:-To print the reverse of the given number/ package mylist; import java.io.; //Importing io package public class reverse { public void rev()throws IOException //throwing io exceptions { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input System.out.println("Enter a number"); //For the user to input int n; //Declaring a variable to store the given number int rev=0; //Declaring a variable to store the reverse of the given number int rem=0; //Declaring a variable to store a single number every time n=Integer.parseInt(s.readLine()); //Converting string to integer while(n>0) //A loop for getting the reverse of the given number { rem=n%10; //Storing a single number every time rev=rev10+rem; //Reversing the number n=n/10; //Removing the number }

System.out.println("The Reversed Number:"+rev); string } }

Variable n rem rev /

//Displaying the reversed

DataType Description Integer To store the given number Integer To store a single number Integer To store the reversed number

Sample Input: 345 Output: The Reversed Number:543 /

27.e:-To print the reverse of a given string/ package mylist; import java.io.; //Inporting io package public class revstring { public void reverse()throws IOException //Throwing io exceptions { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input System.out.println("Enter a Word or Sentence"); String r; //Declaring a variable to store the given string String v=""; //Declaring a variable to store the reversed string r=s.readLine(); //Reading the given string for(int i=r.length()-1;i>=0;i--) //A loop to reverse the given string { v+=r.charAt(i); //Reversing the string } System.out.println("The Reversed Word or Sentence:"+v); //Displaying the reversed string } }

Variable DataType Description r String To store the given string v String To store the reversed string i Integer Counter Variable as well as to terminate the loop

/ Sample Input: The Hyderabad Public School Output: The Reversed Word or Sentence:loohcS cilbuP dabaredyH ehT /

28.Write a program to input the total cost and to compute and display the amount to be paid by the customer after availing the discount/ import java.io.; //Inporting io package public class discount { public static void main()throws IOException //Throwing io exceptions { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input System.out.println("Enter the Cost"); //For the user to input double n; //Declaring a variable to store the given cost double amt=0; //Declaring a variable to store the amount double d=0; //Declaring a variable to store the discount n=Double.parseDouble(s.readLine()); //Converting string to double if(n<=2000) { //If the cost is less than equal to Rs.2000 d=(n5)/100; amt=n-d; } else if(n>=2001&&n<=5000) { //If the cost is >=Rs.2001 and <=Rs.5000 d=(n25)/100; amt=n-d; } else if(n>=5001&&n<=10000) { //If the cost is >=Rs.5001 and <=Rs.10000 d=(n35)/100; amt=n-d; } else { //If the cost is greater than Rs.10000 d=(n50)/100; amt=n-d;

} System.out.println("Amount to be Paid="+"Rs."+amt);

//Displaying the amount

} }

Variable n d amt /

DataType Double Double Double

Description To store the given cost To store the discount on calculated on the cost To store the amount to be paid

Sample Input: 10000 Output: Amount to be Paid=Rs.6500.0 /

29.Write a program that outputs the results of the following evaluations based on the number entered by the user/ import java.io.; //Importing io package public class math_logic { public static void main()throws IOException //Throwing io exceptions { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input //For the user to input System.out.println("1.Natural Logarithm"); System.out.println("2.Absolute Value"); System.out.println("3.Square Root"); System.out.println("4.Random Numbers"); System.out.println("5.Exit"); int i; //Declaring a variable to store the choice of the user double n; //Declaring a variable to store the given number i=Integer.parseInt(s.readLine()); //Converting string to double switch(i) { case 1: System.out.println("Enter a number"); //For the user to input n=Double.parseDouble(s.readLine()); System.out.println("Natural Logarithm of the number="+Math.log(n)); //Displaying the log value of the given number break; case 2: System.out.println("Enter a number"); //For the user to input n=Double.parseDouble(s.readLine());

System.out.println("Absolute value of the number="+Math.abs(n)); //Displaying the square root value of the given number break; case 3: System.out.println("Enter a number"); //For the user to input n=Double.parseDouble(s.readLine()); System.out.println("Square root of the number="+Math.sqrt(n)); //Displaying the absolute value of the given number break; case 4: System.out.println("Random Numbers between 1 & 100="+Math.random()); break; case 5: System.out.println("THANK YOU"); break; default: System.out.println("INVALID NUMBER"); } } }

Variable DataType Description i Integer To store the choice of the user n Double To store the given number / 1.Natural Logarithm 2.Absolute Value 3.Square Root 4.Random Numbers 5.Exit Sample Input: Enter a number

1

Sample Input: 4 Output: Natural Logarithm of the number=1.3862943611198906 / 30.Write a program that outputs the results of the following evaluations based on the choice entered by the user/ import java.io.; //Importing io package

public class menudriven { public static void main(String args[])throws IOException //Throwing io exceptions { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input System.out.println("1.Factors of a given number"); System.out.println("2.Factorial of a given number"); System.out.println("3.Check number is prime or not"); System.out.println("4.Check number is perfect or not"); System.out.println("Enter your choice"); int j; //Declaring a variable to store the choice j=Integer.parseInt(s.readLine()); //Converting string to integer switch(j) { case 1: System.out.println("Enter a number"); int n=Integer.parseInt(s.readLine()); System.out.println("Factors of the given number:"); for(int i=1;i<=n;i++) //A loop for getting the factors { if(n%i==0) //A condition to get only the factors of the given number { System.out.println(i); //Displaying the factors of the given number } } break; case 2: System.out.println("Enter a number"); n=Integer.parseInt(s.readLine()); int f=1; //To store the factorial of a given number for(int i=1;i<=n;i++) //A loop for getting the factorial { if(n%i==0) //A condition for getting the factors of the given number { f=i; //Getting the factorial of the given number } } System.out.println("Factorial of the given number="+f); //Displaying the factorial of the given number break; case 3: System.out.println("Enter a number"); n=Integer.parseInt(s.readLine()); int p=0; //Declaring a variable to store the sum for(int i=2;i
} } if(p==0) //If the sum of the factors is equal to zero then it is a Prime number { System.out.println("The given number is a Prime Number"); } else //If the sum of the factors is greater than zero then it is not a Prime number { System.out.println("The given number is not a Prime Number"); } break; case 4: System.out.println("Enter a number"); n=Integer.parseInt(s.readLine()); f=0; //Declaring a variable to store the sum of factors of n for(int i=1;i
Variable DataType j Integer n Integer f Integer p Integer i Integer /

Description To store the given choice To store the given number To store the factorial as well as to store the sum of the factors To store the sum Counter Variable as well as to terminate the loop

1.Factors of a given number 2.Factorial of a given number 3.Check number is prime or not 4.Check number is perfect or not Enter your choice Sample Input: 1 Enter a number Sample Input: 6 Output: Factors of the given number: 1 2 3 6 /

31.Write a program to display the Income Tax/ import java.io.; //Importing io package public class tax { public static void main()throws IOException //Throwing io exceptions { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input System.out.println("Enter your Salary"); //For the user to input double n; //Declaring a variable to store the salary double v=0; //Declaring a variable to store the income tax n=Double.parseDouble(s.readLine()); //Converting string to double if(n>=9000) { //If the salary is 9000 or more v=(n40)/100; } else if(n>=7500&&n<=8999) { //If the salary is from 7500 to 8999 v=(n30)/100; } else if(n<=7499) { //If the salary is less than 7499 v=(n20)/100; } System.out.println("The Income Tax to be Paid="+"Rs."+v); //Displaying the income tax } }

Variable n v /

DataType Double Double

Description To store the given salary To store the income tax that is calculated

Sample Input: 10000 Output: The Income Tax to be Paid=Rs.4000.0 / 32.Using switch case create class which take day number (1-7) and prints the corresponding day in words/ import java.io.; //Importing io package public class weekday { public static void main()throws IOException //Throwing io exceptions { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input System.out.println("Enter a number"); //For the user to input int n; //Declaring a variable to store the given number n=Integer.parseInt(s.readLine()); //Converting string to integer switch(n) { case 1: //If the given number is 1 then it is Monday System.out.println("Monday"); break; case 2: //If the given number is 2 then it is Tuesday System.out.println("Tuesday"); break; case 3: //If the given number is 3 then it is Wednesday System.out.println("Wednesday"); break; case 4: //If the given number is 4 then it is Thursday System.out.println("Thursday"); break; case 5: //If the given number is 5 then it is Friday System.out.println("Friday"); break; case 6: //If the given number is 6 then it is Saturday System.out.println("Saturday"); break; case 7: //If the given number is 7 then it is Sunday System.out.println("Sunday"); break;

default: //If the given number is other then 1-7 then the following statement is displayed System.out.println("INVALID NUMBER"); } } }

Variable n /

DataType Description Integer To store the given number

Sample Input: 7 Output: Sunday /

33.Write a program to compute charges for sending parcels/ import java.io.; public class courier { public static void main ()throws IOException { BufferedReader br=new BufferedReader (new InputStreamReader (System.in)); System.out.println ("Enter the mass in grams"); int n=Integer.parseInt (br.readLine ()); // to store the entered mass double bill=0; int d,e; if (n<=100) bill=30; // conditions to calculate the bill else if (n>100) { d=n/50; e=n%50; if (n==0) d=d; else d=d+1; bill=30+(d-2)10; } System.out.println ("Total charge for "+n+" grams is "+" Rs. "+bill); } // close main } // close class

35.Using a Switch statement, write a program in JAVA to input an amount in Indian Rupees to convert into any other foreign currency/ import java.io.; //Importing io package public class money { public static void main()throws IOException //Throwing io exceptions { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input System.out.println("1.Euro"); System.out.println("2.Swiss France"); System.out.println("3.Thai Baht"); System.out.println("4.Yen"); System.out.println("5.US Dollars"); System.out.println("6.Pound"); System.out.println("Enter your choice"); int i; //Declaring a variable to store the choice double n=0; //Declaring a variable to store the amount double v=0; //Declaring a variable to store the amount after conversion i=Integer.parseInt(s.readLine()); switch(i) { case 1: //Converting Indian Rupees to Euroes System.out.println("Enter some Amount in Indian Rupees"); //For the user to input n=Double.parseDouble(s.readLine()); v=n/53.17; System.out.println("Amount after conversion="+v+" "+"Euroes"); //Displaying the converted amount break; case 2: //Converting Indian Rupees to Swiss System.out.println("Enter some Amount in Indian Rupees"); //For the user to input n=Double.parseDouble(s.readLine()); v=n/34.01; System.out.println("Amount after conversion="+v+" "+"Swiss"); //Displaying the converted amount break; case 3: //Converting Indian Rupees to Thai Bahts System.out.println("Enter some Amount in Indian Rupees"); //For the user to input n=Double.parseDouble(s.readLine()); v=n/2.606; System.out.println("Amount after conversion="+v+" "+"Thai Bahts"); //Displaying the converted amount break; case 4: //Converting Indian Rupees to Yen System.out.println("Enter some Amount in Indian Rupees"); //For the user to input n=Double.parseDouble(s.readLine());

v=n/34.81; System.out.println("Amount after conversion="+v+" "+"Yen"); //Displaying the converted amount break; case 5: //Converting Indian Rupees to Dollars System.out.println("Enter some Amount in Indian Rupees"); input n=Double.parseDouble(s.readLine()); v=n/47.36; System.out.println("Amount after conversion="+v+" "+"$"); //Displaying the converted amount break; case 6: //Converting Indian Rupees to Pounds System.out.println("Enter some Amount in Indian Rupees"); input n=Double.parseDouble(s.readLine()); v=n/79.62; System.out.println("Amount after conversion="+v+" "+"Pounds"); //Displaying the converted amount break; default: System.out.println("INVALID NUMBER"); } } }

//For the user to

//For the user to

Variable DataType Description i Integer To store the choice of the user n Double To store the given amount v Double To store the converted amount / Sample Input: 200 Output: Amount after conversion=2.5119Pounds / 36.Write a menu driven program to input two strings from the user and find the following: a.Both are equal or not b.Total number of uppercase in first string c.Change the lowercase letters to uppercase in second string/ import java.io.; //Importing io package public class stringmenu { public static void main()throws IOException //Throwing io exceptions { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input

System.out.println("1.Both are equal or not"); System.out.println("2.Total number of uppercase letters in first string"); System.out.println("3.Change the lowercase letters to uppercase in second string"); System.out.println("Enter your choice"); System.out.println("Enter two strings"); int i; //Declaring a variable to store the choice of the user int c=0; //Declaring a variable to store the number of lowercase letters String r=new String(); //Declaring a variable to store the first string String v=new String(); //Declaring a variable to store the second string i=Integer.parseInt(s.readLine()); //Converting string to integer r=s.readLine(); //Storing the first string v=s.readLine(); //Storing the second string switch(i) { case 1: if(r.equalsIgnoreCase(v)) //If both are equal the following statement is executed { System.out.println("Both the strings are equal"); } else //If the given strings are not equal the the following statement is displayed { System.out.println("They are not equal"); } break; case 2: for(int j=0;j
Variable

DataType

Description

i c r v j /

Integer Integer String String Integer

To store the choice of the user To store the number of uppercase letters To store the first string To store the secong string Counter Variable as well as to terminate the loop

1.Both are equal or not 2.Total number of uppercase letters in first string 3.Change the lowercase letters to uppercase in second string Enter your choice Enter two strings Sample Input: 3 Rohit Hyderabad Public School Output: Converted String:HYDERABAD PUBLIC SCHOOL /

37.Write a program using a method palin(), to check whether a string is a palindrome or not/ import java.io.; //Importing io package public class palinstring { public static void main()throws IOException //Throwing io excptions { palinstring v=new palinstring(); //Object Declaration v.palin(); } public void palin()throws IOException //Throwing io exceptions { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input System.out.println("Enter a String"); //For the user to input String r=new String(); //Declaring a variable to store the given string String m=new String(""); //Declaring a variable to store the reversed string r=s.readLine(); //Storing the given string for(int i=r.length()-1;i>=0;i--) { m+=r.charAt(i); //Reversing the string } if(r.equalsIgnoreCase(m)) //If the given string is equal to the reversed string then it is palindrome { System.out.println("The given string is Palindrome"); } else //If the given string is not equal to the reversed string then it is not palindrome

{ System.out.println("The given string is not Palindrome"); } } }

Variable DataType Description r String To store the given string m String To store the reversed string i Integer Counter Variable as well as to terminate the loop / Sample Input: Eye Output: The given string is Palindrome / 38.Write a program to input a string and two words and replace first word with second word/ import java.io.; //Importing io package public class replace { public static void main()throws IOException //Throwing io exceptions { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input //For the user to input System.out.println("Enter a String"); System.out.println("Enter a Word which is to be replaced"); System.out.println("Enter a Word with which the other word is to be replaced"); String r=new String(""); //Declaring a variable to store the given string String v=new String(""); //Declaring a variable to store the first word String m=new String(""); //Declaring a variable to store the secong word String a=new String(""); //Declaring a variable to store the replaced string String b=new String(""); //Declaring a variable to store a single word char c=' '; //Declaring a variable to store a single character r=s.readLine(); //Storing the given string v=s.readLine(); //Storing the given word m=s.readLine(); //Storing the given word for(int i=0;i
a+=c;

//Adding space to the string

} else

//If they are not equal then the following statements are

executed { a+=b; a+=c; b="";

//Adding the word which is not replaced //Adding space to the string //Assigning b to null

} } else { b+=c; //Adding the character to b if(b.equalsIgnoreCase(v)) { a+=m; //Replacing the word } } } System.out.println(a);

//Displaying the replaced string

} }

Variable DataType r String v String m String a String b String c Character i Integer /

Description To store the given string To store the given word To store the given word To store the replaced string To store a single character or a group of characters To store the character at the position 'i' Counter Variable as well as to terminate the loop

Sample Input: hot plate with hot egg egg tea Output: hot plate with hot tea / 39.Write a program to print the initials/ public class initial { static String s="Vinod Kumar Khare"; //Storing the name in a variable public static void init1() { int l=s.length(); //Storing the length of the name

int ls=s.lastIndexOf(' '); System.out.print(s.charAt(0)); //Displaying the first character String last=s.substring(ls+1); for(int i=0;i
Variable s l ls last c i /

DataType Description String To store the name Integer To store the length of the name Integer To store the index String To store a word Character To store each character Integer Counter Variable as well as to terminate the loop

Output:V.K.Khare Khare,V.K. V.K.K. /

40.Write a program to enter the name and convert its each lower case letters to upper case and vice versa/ import java.io.; //Importing io package public class changecase { public static void main( )throws IOException //Throwing io exceptions { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input System.out.println("Enter a name"); //For the user to input String r=new String(""); //Declaring a variable to store the given name String v=new String(""); //Declaring a variable to store the changed name char c=' '; //Declaring a variable to store a single character every time char d=' '; //Declaring a variable to store the changed character r=s.readLine(); //Storing the given name for(int i=0;i
d=Character.toUpperCase(c); //If the condition is false then the given character is changed to it's opposite case v+=d; //Storing the changed character } } System.out.println("The name after changing it's case is:"+v); //Displaying the changed name } }

Variable DataType Description r String To store the given name v String To store the changed name c Character To store a single character every time d Character To store the character in it's opposite case i Integer Counter Variable as well as to terminate the loop / Sample Input: Subhash Chandra Bose Output: The name after changing it's case is:sUBHASH cHANDRA bOSE / 41.Write a program in JAVA to input a sentence performing encoding or decoding/ import java.io.; //Importing io package public class code { public static void convert()throws IOException //Throwing io exceptions { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input System.out.println("Enter a sentence"); //For the user to input String v=new String(""); //Declaring a variable to store the given sentence String r=new String(""); //Declaring a variable to store the changed sentence int c=0; //Declaring a variable to store v=s.readLine(); for(int i=0;i90)

{ c=c-26; } r=r+(char)c; } else { r+=' '; } } System.out.println("The converted sentence is:"+r); sentence } }

Variable DataType v String r String n Integer c Integer i Integer /

//Displaying the converted

Description To store the given sentence To store the changed sentence To store the value of shifting places To store the ASCII code of the changed character Counter Variable as well as to terminate the loop

Sample Input: GOOD BOY Output: The converted sentence is:JRRG ERB / 42.To remove all the vowels in the given string/ import java.io.; //Importing io package public class vowel { public static void main()throws IOException //Throwing io exceptions { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input System.out.println("Enter a String"); //For the user to input StringBuffer v=new StringBuffer(s.readLine()); //Declaring a variable to store the given string char c; //Declaring a variable to store the character at a particular position for(int i=0;i
} System.out.println("The sentence after deleting the vowels:"+v); the string after deleting the vowels from the string } }

//Displaying

Variable DataType Description v StringBuffer To store the given string c Character To store a single character i Integer Counter Variable as well as to terminate the loop / Sample Input: Computer Output: The sentence after deleting the vowels:Cmptr / 43.Write a program to accept a string, take out the last character and return new string with last character added at the front & back/ import java.io.; //Importing io package public class add_word { public static void main()throws IOException //Throwing io exceptions { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input System.out.println("Enter a Word"); //For the user to input StringBuffer r=new StringBuffer(s.readLine()); //Storing the given string int n; //Declaring a variable to store the length of the given string n=r.length(); //Storing the length of the string char c; //Declaring a variable to store the last character c=r.charAt(n-1); //Storing the last character r.insert(0,c); //Adding the last character to front n=r.length(); //Storing the length of the string after adding the letter to front r.insert(n-1,c); //Adding the letter to back System.out.println(r); //Displaying the string after conversions } }

Variable DataType Description r StringBuffer To store the given string c Character To store the last character n Integer To store the length of the string /

Sample Input: Output: tfatt /

fat

44.Write a program to accept a string from console &, print a new string where "not" has been added to the front/ import java.io.; //Importing io package public class not { public static void main()throws IOException //Throwing io exceptions { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input System.out.println("Enter a String"); //For the user to input String r=new String(""); //Declaring a variable to store the given string String v=new String(""); //Declaring a variable to add the word not in the front String m=new String(""); //Declaring a variable to store the first three letters & check String j=new String(""); //Declaring a variable to store the chaged string r=s.readLine(); //Storing the given string v="not"; //Storing the word 'not' to add it in the front m=r.substring(0,3); //To extract the first three letters if(v.equalsIgnoreCase(m)) //A condition to check whether the first three letters is equal to 'not' { //If the given word starts with the word 'not' then the sentence is displayed as it is System.out.println("The changed word:"+r); //Displaying the statement } else { //If the given word does not start with the word 'not' then the 'not' is added to it in the front j=v+r; System.out.println("The changed word:"+j); //Displaying the string after the required changes are made } } }

Variable DataType Description r String To store the given integer v String To store the word 'not' m String To store the first three letters of the given string j String To store the changed string /

Sample Input: able Output: The changed word:notable /

44.Write a program to accept a string from console &, print a new string where "not" has been added to the front/ import java.io.; //Importing io package public class not { public static void main()throws IOException //Throwing io exceptions { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input System.out.println("Enter a String"); //For the user to input String r=new String(""); //Declaring a variable to store the given string String v=new String(""); //Declaring a variable to add the word not in the front String m=new String(""); //Declaring a variable to store the first three letters & check String j=new String(""); //Declaring a variable to store the chaged string r=s.readLine(); //Storing the given string v="not"; //Storing the word 'not' to add it in the front m=r.substring(0,3); //To extract the first three letters if(v.equalsIgnoreCase(m)) //A condition to check whether the first three letters is equal to 'not' { //If the given word starts with the word 'not' then the sentence is displayed as it is System.out.println("The changed word:"+r); //Displaying the statement } else { //If the given word does not start with the word 'not' then the 'not' is added to it in the front j=v+r; System.out.println("The changed word:"+j); //Displaying the string after the required changes are made } } }

Variable DataType Description r String To store the given integer v String To store the word 'not'

m j /

String String

To store the first three letters of the given string To store the changed string

Sample Input: able Output: The changed word:notable /

45.Write a program to enter a sentence and count the number of times a particular word occurs in it/ import java.io.; //Importing io package public class word { public static void main()throws IOException //Throwing io exceptions { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input System.out.println("Enter a Sentence"); //For the user to input System.out.println("Enter a word to be searched"); //For the user to input String r=new String(""); //Declaring a variable to store the given sentence String v=new String(""); //Declaring a variable to store the word to be searched String m=new String(""); //Declaring a variable to store the words in the sentence for checking int c=0; //Declaring a variable to store the number r=s.readLine(); //Storing the given sentence v=s.readLine(); //Storing the word for(int i=0;i
Variable DataType r String v String m String c Integer i Integer /

Description To store the given sentence To store the given word To store the extracted word To store the number of times the word occurs Counter Variable as well as to terminate the loop

Sample Input: the quick brown fox jumps overs the lazy dog the Output: Searched Word occurs:2 times / 46.Write a program using a method Palin(), to check whether a given number is a primepalindrome or not/ import java.io.; //Importing io package public class primepalin { public static void Palin()throws IOException //Throwing io exceptions { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input System.out.println("Enter a number"); //For input from user int n; //Declaring a variable to store the given number int p=0; //Declarint a variable to store the sum of the factors int rem; //Declaring a variable used during reversal of a number is taken place int rev=0; //Declaring a variable used during reversal of a number is taken place n=Integer.parseInt(s.readLine()); //Converting string to integer for(int i=2;i
} if(p==rev) { //If the given number is equal to the reversed number then the following statement is displayed System.out.println("The given number is a Prime-Palindrome Number"); } else { //If the given number is not equal to the reversed number then the following statement is displayed System.out.println("The given number is not a Prime-Palindrome Number"); } } else //If the condition is false then the given numberis not a Prime-Palindrome number { System.out.println("The given number is not a Prime-Palindrome Number"); } } }

Variable DataType Description n Integer To store the given number p Integer To store the sum of factors of the given number as well as to store the value of n rem Integer To store a single digit every time rev Integer To store the reversed number i Integer Counter Variable as well as to terminate the loop / Sample Input: 313 Output: The given number is a Prime-Palindrome Number / Write a program to change 26 to 15, January to August, Republic to Independence & print the changed statement/ public class aug { public static void main() //Taking a function to change the statement { StringBuffer r=new StringBuffer("January 26 is celebrated as the Republic Day of India");//Storing the statement which is to be changed StringBuffer v=new StringBuffer(""); //Declaring a variable to store the changed statement

v=r.replace(0,7,"August"); //Replacing 'January' with 'August v=r.replace(7,9,"15"); //Replacing '26' with '15' v=r.replace(31,39,"Independence"); //Replacing 'Republic' with 'Independence' System.out.println(v); //Displaying the changed statement } }

Variable DataType r StringBuffer v StringBuffer / Output: /

Description To store the statement to be changed To store the changed statement

August 15 is celebrated as the Independece Day of India

48.Write a program to input any sentence and find the longest word/ import java.io.; //Importing io package public class length { public static void main()throws IOException //Throwing io exceptions { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input System.out.println("Enter a sentence"); //For the user to input String r=new String(""); //Declaring a vairable to store the given sentence String v=new String(""); //Declaring a variable to store each word String m=new String(""); //Declaring a variable to store the longest word char c; //Declaring a variable to store each character int len=0; //Declaring a variable to store the length of the given word r=s.readLine(); //Storing the given sentence for(int i=0;i
} } System.out.println("The longest word in the given sentence is:"+m); //Displaying the longest word } public static boolean check(int a)throws IOException //Throwing io excpetions { int max=0; //Declaring a variable to store the maximum length if(a>max) //A condition to get the maximum length { max=a; return true; } else { return false; } } }

Variable DataType r String v String m String c Character len Integer a Integer max Integer i Integer /

Description To store the given sentence To store each word To store the longest word To store each character To store the length of each word To store the value passed when the function is called To store the maximum length Counter Variable as well as to terminate the loop

Sample Input: India is my Country Output: The longest word in the given sentence is:Country / 49.Write a program to enter a sentence and count the number of times a particular word occurs in it/ import java.io.; //Importing io package public class freq { public static void main()throws IOException //Throwing io exceptions { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input System.out.println("Enter a Sentence"); //For the user to input System.out.println("Enter a word to be searched"); //For the user to input String r=new String(""); //Declaring a variable to store the given sentence

String v=new String(""); //Declaring a variable to store the word to be searched String m=new String(""); //Declaring a variable to store the words in the sentence for checking int c=0; //Declaring a variable to store the number r=s.readLine(); //Storing the given sentence v=s.readLine(); //Storing the word for(int i=0;i
Variable DataType r String v String m String c Integer i Integer /

Description To store the given sentence To store the given word To store the extracted word To store the number of times the word occurs Counter Variable as well as to terminate the loop

Sample Input: the quick brown fox jumps overs the lazy dog the Output: Searched Word occurs:2 times / 50.Write a method that checks if value stored in a character type variable is a digit or a letter and displays an appropriate message/ import java.io.; //Importing io package public class check { public static void main()throws IOException //Throwing io exceptions

{ BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input System.out.println("Enter a Character"); //For the user to input char c; //Declaring a variable to store the given character c=(char)s.read(); //Storing the given character if(Character.isLetter(c)) //A condition to check whether the given character is a letter { //If the given character is a letter then the following statement is displayed System.out.println("The given Character is a Letter"); } else if(Character.isDigit(c)) //A condition to check whether the given character is a digit { //If the given character is a digit then the following statement is displayed System.out.println("The given Character is a Digit"); } else { //If the given character is neither a letter nor a digit then the following statement is displayed System.out.println("The given Character is neither a Letter nor a Digit"); } } }

Variable c /

DataType Character

Description To store the given character

Sample Input: R Output: The given Character is a Letter /

51.To count all the vowels in the given string/ import java.io.; //Importing io package public class vowelcount { public static void main()throws IOException //Throwing io exceptions { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input System.out.println("Enter a String"); //For the user to input String v=new String(""); //Declaring a variable to store the given string

char c; //Declaring a variable to store the character at a particular position int n=0; //Declaring a variable to store the count of vowels v=s.readLine(); //Storing the given string for(int i=0;i
Variable DataType Description v String To store the given string c Character To store a single character n Integer To store the count of the vowels i Integer Counter Variable as well as to terminate the loop / Sample Input: Computer Output: Vowels occur:3 times / 52.Write a program to accept a string from the user and alters the string in such a way that the alphabet next to a vowel gets repalced by an equivalent opposite case alphabet/ import java.io.; //Importing io package public class vowelcase { public static void main()throws IOException //Throwing io exceptions { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input System.out.println("Enter a string"); //For the user to input String r=new String(""); //Declaring a variable to store the given string char c; //Declaring a variable to store each character r=s.readLine(); //Storing the given string for(int i=0;i
{ if(Character.isLowerCase(c)) { System.out.print(Character.toUpperCase(r.charAt(i+1))); //Displaying the changed character } else { System.out.print(Character.toLowerCase(r.charAt(i+1))); //Displaying the changed character } } else { System.out.print(c); //Displaying the character if it is not a vowel } } } }

Variable DataType Description r String To store the given string c Character To store each character i Integer Counter Variable as well as to terminate the loop / Sample Input: Good decision comes from experience Output: GoOD deCiSiON coMeS froM eXpeRiENce / 53.Write a program in JAVA to encode and decode a string/ import java.io.; //Importing io package public class coded { public static void main()throws IOException { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input System.out.println("Enter a string"); //For the user to input String r=new String(""); //Declaring a variable to store the given string String v=new String(""); //Declaring a variable to store the changed string int n=2; int c=0; r=s.readLine(); //Storing the string for(int i=0;i
c=(int)r.charAt(i)+n; while(c<65) { c+=26; } while(c>90) { c-=26; } v+=(char)c; //Storing the character } System.out.println(v);

//Displaying the changed word

} }

Variable DataType r String v String n Integer c Integer i Integer /

Description To store the given string To store the changed string To store the number of places to shift To store ASCII values Counter Variable as well as to terminate the loop

Sample Input: ABCXYZ Output: CDEZAB /

54.Write a program to count the number of times the letter 'o' occurs in the given string/ import java.io.; //Importing io package public class number { public static void main()throws IOException //Throwing io exceptions { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input System.out.println("Enter a Sentence"); //For the user to input String r=new String(""); //Declaring a variable to store the given sentence int n=0; //Declaring a variable to store the number of o's in the sentence r=s.readLine(); //Storing the given sentence for(int i=0;i
n+=1;

//If the condition is true then the value of n is increased by 1

} } System.out.println("The letter 'o' occurs:"+n+" "+"times"); number of o's in the sentence } }

//Dispalying the

Variable DataType Description r String To store the given sentence n Integer To store the number of o's i Integer Counter Variable as well as to terminate the loop / Sample Input: Computer Project Output: The letter 'o' occurs:2 times / 55.Write a program in JAVA to accept a string and display the number of palindrome words present in the given string/ import java.io.; //Importing io package public class stringpalin { public static void main()throws IOException //Throwing io exceptions { BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); //To input System.out.println("Enter a sentence"); //For the user to input String r=new String(""); //Declaring a variable to store the given sentence String v=new String(""); //Declaring a variable to store a single word String m=new String(""); //Declaring a variable to store the reversed word int n=0; //Declaring a variable to store the number of palindrome words r=s.readLine(); for(int i=0;i=0;j--) { m+=v.charAt(j); } if(m.equalsIgnoreCase(v)) { n+=1; v="";

} else { v=""; } } else { v=v+r.charAt(i); } } System.out.println("Number:"+n); } }

Related Documents

Computer Projectblue J
July 2020 10
Computer
April 2020 30
Computer
November 2019 60
Computer
November 2019 45
Computer
May 2020 36
Computer
October 2019 48

More Documents from ""

Computer Projectblue J
July 2020 10
July 2020 12
Cofetarii
June 2020 11
Pdo Syllabus
May 2020 14