Posts

Showing posts from May, 2019

Displaying Numbers in Ascending and Descending Order using array

Image
Printing numbers in Ascending and Descending Order import  java . util . Scanner ;  //Calling Scanner Class public class  Swap {      public static void main(String []args)     {          //Declaring Variables          int  length ;          int    a =1;          int  i=0;          System.out.println ("Enter length of array ");  //Instruction for user          length =  new   Scanner ( System.in ).nextInt();  //Getting value from user          int  num []=  new   int  [ length ];  //Initializing array             for( i=0; i< length ;i++)  //loop for getting array from user             {           ...

Alternative method to make the game

Image
import   java . util . Random ;  //Calling Random Class import  java . util .Scanner;  //Calling Scanner Class public class   Play {      public static void main(String []args)     {          System.out.println ("Wel-Come to our guessing game");  //Instruction to users          Random   num = new  Random ();          int   num1 =  num . nextInt (100);                  for ( int  i=1; i<10;i++)         {              System.out.println ();   //Printing blank line to make result better              System.out.println (+i+". Please Enter your Guess");  //Instruction to user to input value              int  a=...

Guess the number and win the prize(game)

Image
A simple game to guess number form 1 to 100   import   java . util . Random ;  //Calling Random Class import   java . util . Scanner ;  //Calling Scanner Class public class   HighLow {      // instance variables        private  double x;      public void  game()     {          int   n =1;          int  a =1;          Random  b= new  Random ();         int c= b.nextInt(100);          System.out.println ("Guess the Number(1 to 100)");  //Instruction to user          while ( n <=10)         {              System.out.println ("Guess"+ n +": ");              x =  new   Scanner (System...

Find out the divisors or the factors of any number using java program

Image
Program to display the Factors of any number "Factors" are the numbers you multiply to get another number. For instance, factors of 20  are  4  and  5 , because 4 ×5 = 20 . Some numbers have more than one factorization (more than one way of being factored). For instance,  10  can be factored as  1×10, or 2×5. For Example:- The factors of 100 Answer : 1,2,4,5,10,20,25,50,100. import   java . util . Scanner ;  //Calling Scanner Class public class   Divisors {      // instance variable        private   int  x ;      public void   calculation()  //Non return type, Non Parameterized method     {          System.out.println ("Enter any number");  //Instruction to user          x =  new  Scanner ( System.in ). nextInt() ;  //Getting input     ...

Alternative method to display the String in reverse form

Image
Accept and display the String in reverse form import  java . util . Scanner ;  //Calling Scanner Class public class  Reverse {      public static void main(String []args)     {          System.out.println ("Enter String to Reverse it ");  //Instruction to user          String  str=  new  Scanner ( System.in ). nextLine() ;  //Getting String from User          int  i =  str.length() ;  //Calculating Length of Given String          String  rev ="";  //Declaring and Initializing variable             for(int  j = i -1;  j >=0;  j --)  //Looping statement             {                  rev =  rev + str.charAt( j ) ;  //Processing   ...

Check and display whether it is a prime number or not OR an automorphic number or not.

Image
Identify the number is EITHER prime number or not OR an automorphic number or not.  If you need some data from the user then you need to import the  Scanner  class. If you want to know more about  Scanner class then, please visit this link ( https://programmerbrothers.blogspot.com/2019/04/scanner-class-in-java-scanner-is-class.html ) import   java . util . Scanner ; public class  NumberCheck {      // instance variables        private  int  x ;      int   b ;      public void   Prime()     {          System.out.println ("Enter a number");          x =  new   Scanner ( System.in ). nextInt() ; The statements mentioned below check that the input number is either prime or not. Prime numbers are perfectly divisible by only 1 and the same number. So, following such trick, the numb...

Getting string using buffered Reader class and Input Stream class

Getting string using buffered Reader class and Input Stream class BufferedReader reads a couple of characters from the specified stream and stores it in a buffer. This makes input faster. InputStreamReader reads only one character from the specified stream and remaining characters still remain in the stream. For example:- import  java . io . InputStreamReader ; import  java . io . BufferedReader ; public class  InputBR {      public static void main(String []args)throws Exception    {           InputStreamReader  isr=  new  InputStreamReader ( System.in );           BufferedReader  br=  new   BufferedReader (isr);           String  str= br. readLine() ;           System.out.println ("Entered String is : "+str);     } }

Program to display the input string from backward

Image
Display the input string in reverse order The program displays the input String in reverse order and for this action, you can use the substring method. The syntax for the built_in method substring is:- Syntax : public String substring(int begIndex, int endIndex) Parameters : beginIndex : the begin index, inclusive. endIndex : the end index, exclusive. Return Value : The specified substring. If you need some data from the user then you need to import the  Scanner  class. Here ' Scanner ' class, the built-in class inside the ' util ' package of ' java ' project is imported to get value from the keyboard.  If you want to know more about  Scanner  class then, please visit this link ( https://programmerbrothers.blogspot.com/2019/04/scanner-class-in-java-scanner-is-class.html ) import  java . util . Scanner ;  //Importing Scanner class to get input from the user import   java . lang . String ;  //Importing Scanner class fro...

Alternative method to Calculate the final price of the Laptop or Desktop after certain discount of total cost according to criteria

import  java . util . Scanner ;  //Importing Scanner class from util package public class  Purchase {      //Declaring instance variables       private  String  name ;      private  String  address ;      private  double   purchase ;      private   double   finalPrice ;   public static void main(String []args)  //Main method     {          System.out.println ("Press L for laptop and D for desktop"); //Instruction for User         char   ch = new  Scanner ( System.in ). next().charAt(0) ; //Getting Character from user                                    if  ( ch =='L'||  ch =='l') //Checking condition                 ...