Tuesday, 13 February 2018

Write a Java Program to Find Random Number

package com.tutorial4u;

import java.util.Random;
import java.util.Scanner;

public class RandomNumber {
     public static void main(String[] args)
    {
        int maxRange;
        Scanner sc = new Scanner(System.in);
        Random rand = new Random();
       
        System.out.print("Please enter maximum range: ");
        maxRange = sc.nextInt();
       
        for(int i=1; i<=10; i++)
        {
            System.out.println(rand.nextInt(maxRange));
        }
    }


}


Output:

Please enter maximum range: 500
223
263
397
375
371
468
71
124
377
374

Note : Sometimes situation arises where random numbers are required to be generated between the ranges.

package com.tutorial4u;

import java.util.Random;
import java.util.Scanner;

public class RandomNumber {
     public static void main(String[] args)
    {
           Scanner sc = new Scanner(System.in);
        System.out.print("Please enter Minimum Range : ");
        int minRange = sc.nextInt();
        System.out.print("Please enter Maximum Range : ");
        int maxRange = sc.nextInt();
        Random rand = new Random();
        int value = rand.nextInt(maxRange - minRange) + minRange;
        System.out.println(value);
    }
}


Output :

Please enter Minimum Range : 1000
Please enter Maximum Range : 5000
4304


Thursday, 1 February 2018

Print Semi Column without using semi column(;)

package com.tutorial4u;

public class SemiColonsPrint {
     public static void main(String[] args) {
              if(System.out.printf("%C",59) != null){
                  
              }         
     }

}

Output :  ;

Saturday, 27 January 2018

Write a Java Program to draw number pattern.

Example : 1

package com.tutorial4u;

import java.util.Scanner;

public class NumberPattern {
     public static void main(String[] args) {
           System.out.print("Enter number of rows : ");
           Scanner sc = new Scanner(System.in);
           int num = sc.nextInt();
           for(int i=1;i<=num;i++){
                for(int j =1; j<=i;j++){
                      System.out.print(j+"");
                }
                System.out.println();
           }
     }

}



Output : 

Enter number of rows : 6

1
12
123
1234
12345
123456


Example : 2


package com.tutorial4u;

import java.util.Scanner;

public class NumberPattern {
     public static void main(String[] args) {
           System.out.print("Enter number of rows : ");
           Scanner sc = new Scanner(System.in);
           int num = sc.nextInt();
           for(int i=1;i<=num;i++){
                for(int j =i; j>=1;j--){
                      System.out.print(j+"");
                }
                System.out.println();
           }
     }

}



Output :

Enter number of rows : 6

1
21
321
4321
54321
654321

Example :3

package com.tutorial4u;

import java.util.Scanner;

public class NumberPattern {
     public static void main(String[] args) {
           System.out.print("Enter number of rows : ");
           Scanner sc = new Scanner(System.in);
           int num = sc.nextInt();
           for(int i=1;i<=num;i++){
                for(int j =1; j<=i;j++){
                      System.out.print(i+"");
                }
                System.out.println();
           }
     }

}


Output :

Enter number of rows : 6

1
22
333
4444
55555
666666

Write a Java Program to print Star Pattern

Example : 1

package com.tutorial4u;

import java.util.Scanner;

public class Triangle {
     public static void main(String[] args) {
           int num;
           System.out.print("Enter the number of rows : ");
           Scanner sc = new Scanner(System.in);
           num = sc .nextInt();
           for(int i =1;i<=num;i++){
                for(int j =1;j<=i;j++){
                      System.out.print("*");
                }
                System.out.println();
           }
     }

}



Output : 

Enter the number of rows : 5

*
**
***
****
*****

Example : 2


package com.tutorial4u;

import java.util.Scanner;

public class Triangle {
     public static void main(String[] args) {
           int num;
           System.out.print("Enter the number of rows : ");
           Scanner sc = new Scanner(System.in);
           num = sc .nextInt();
           for(int i =num;i>=1;i--){
                for(int j =1;j<=i;j++){
                      System.out.print("*");
                }
                System.out.println();
           }
     }

}



Output : 

Enter the number of rows : 7

*******
******
*****
****
***
**
*


Example : 3


package com.tutorial4u;

import java.util.Scanner;

public class Triangle {
     public static void main(String[] args) {
           int num;
           System.out.print("Enter the number of rows : ");
           Scanner sc = new Scanner(System.in);
           num = sc .nextInt();
           for(int i = num;i>=1;i--){
                for(int j =num;j>i;j--){
                      System.out.print(" ");
                }
                for(int k=1;k<=i;k++)
                {
                System.out.print("*");
                }
                System.out.println();
           }
     }

}


Output :

Enter the number of rows : 7

*******
 ******
  *****
   ****
    ***
     **
      *


Example : 4


package com.tutorial4u;

import java.util.Scanner;

public class Triangle {
     public static void main(String[] args) {
           int num;
           System.out.print("Enter the number of rows : ");
           Scanner sc = new Scanner(System.in);
           num = sc .nextInt();
           for(int i = num;i>=1;i--){
                for(int j =1;j<i;j++){
                      System.out.print(" ");
                }
                for(int k=num;k>=i;k--)
                {
                System.out.print("*");
                }
                System.out.println();
           }
     }

}


Output : 

Enter the number of rows : 8

       *
      **
     ***
    ****
   *****
  ******
 *******

Example : 5


package com.tutorial4u;

import java.util.Scanner;

public class Triangle {
     public static void main(String[] args) {
           int num;
           System.out.print("Enter the number of rows : ");
           Scanner sc = new Scanner(System.in);
           num = sc .nextInt();
           {
                for(int i=1; i<=numi++)
                {
                for(int j=num-1; j>=ij--)
                {
                System.out.print(" ");
                }
                for(int k=1; k<=(2*i-1); k++)
                {
                System.out.print("*");
                }
                System.out.println("");
                }
           }
     }
}



Output : 

Enter the number of rows : 5

    *
   ***
  *****
 *******
*********


Example:6


package com.tutorial4u;

import java.util.Scanner;

public class Triangle {
     public static void main(String[] args) {
           int num;
           System.out.print("Enter the number of rows : ");
           Scanner sc = new Scanner(System.in);
           num = sc .nextInt();
           {
            for(int i=num;i>=1;i--)
            {
                 for(int j = num ; j>i;j--)
                 {
                       System.out.print(" ");
                 }
                for(int k=1;k<(i*2);k++)
                {
                System.out.print("*");
                }
                System.out.println();
                }
           }
     }
}



Output : 

Enter the number of rows : 7

*************
 ***********
  *********
   *******
    *****
     ***
      *

Java Development Kit (JDK) and Java Runtime Environment (JRE)

                  Java Development Kit (JDK) and Java Runtime Environment (JRE)  To download and install the Java Development Kit (  JDK ) ...