Showing posts with label for loop. Show all posts
Showing posts with label for loop. Show all posts

Tuesday, May 5, 2015

JAVA program to print all prime numbers between two user given numbers

JAVA program to print all prime numbers between two user given numbers.

import java.io.*;
public class MyMain {
public static void main(String[] args) {
// TODO Auto-generated method stub
InputStreamReader istream = new InputStreamReader(System.in) ;
        BufferedReader read = new BufferedReader(istream) ;
        System.out.print("Enter the first number: ");
        int num1=0, num2=0;
        try{
            num1=Integer.parseInt(read.readLine() );
            System.out.print("Enter the second number: ");
            num2=Integer.parseInt(read.readLine() );
            if(num1>=num2){ //Checking for valid range
            System.out.println("The first number should must be less than the second number.");
            }
            else{
            for(int i=num1;i<=num2;i++){
            int count = 0; //Setting a counter
            for(int j=1;j<=i;j++){
            if(i%j==0) count++;
            }
            if(count == 2)
            System.out.println(i+ " is a prime number.");
            else
            System.out.println(i+ " is a non-prime number.");
            }
            }
           
        } catch(Exception Number){
            System.out.println("Invalid Number!");
        }
           
}
}

Output: 

Enter the first number: 12
Enter the second number: 23
12 is a non-prime number.
13 is a prime number.
14 is a non-prime number.
15 is a non-prime number.
16 is a non-prime number.
17 is a prime number.
18 is a non-prime number.
19 is a prime number.
20 is a non-prime number.
21 is a non-prime number.
22 is a non-prime number.
23 is a prime number.

JAVA program to check for a prime number

Write a JAVA program to check whether a number is prime or not.

import java.io.*;
public class PrimeCheck {
public static void main(String[] args) {
// TODO Auto-generated method stub
InputStreamReader istream = new InputStreamReader(System.in) ;
        BufferedReader read = new BufferedReader(istream) ;
        System.out.print("Enter the number: ");
        int num=0;
        try{
            num=Integer.parseInt(read.readLine() );
            int count = 0;
            for(int i=1;i<=num;i++){
            if(num%i == 0) count++;
            }
            if(count == 2) {
            System.out.println("The number "+num+" is prime.");
            }
            else{
            System.out.println("The number "+num+" is not prime.");
            }
        } catch(Exception Number){
            System.out.println("Invalid Number!");
        }
           
}
}

JAVA print a line

Write a JAVA program to print a line of *. The length of line will be given as user input.

import java.io.*;
public class MyMain {
public static void main(String[] args) {
// TODO Auto-generated method stub
InputStreamReader istream = new InputStreamReader(System.in) ;
        BufferedReader read = new BufferedReader(istream) ;
        System.out.print("Enter Line Size : ");
        int num=0;
        try{
            num=Integer.parseInt(read.readLine() );
        } catch(Exception Number){
            System.out.println("Invalid Number!");
        }
            for(int i=1;i<=num;i++){
                    System.out.print("*");
                }
}
}

Wednesday, April 29, 2015

Java create a upside down triangle pattern



import java.io.*;
public class triangle {
    public static void main ( String arg[] ){
        InputStreamReader istream = new InputStreamReader(System.in) ;
        BufferedReader read = new BufferedReader(istream) ;
        System.out.print("Enter Triangle Size : ");
        int num=0;
        try{
            num=Integer.parseInt( read.readLine() );
        } catch(Exception Number){
            System.out.println("Invalid Number!");       
            for(int i=1;i<=num;i++){
                for(int j=i;j>1;j--){
                    System.out.print(" ");
                }
                for(int k=1;k<=num-(i-1);k++){
                    System.out.print("*");
                    for(int k1=1;k1<k;k1+=k){
                        System.out.print("*");
                    }
                }
                System.out.println();         
    }
}

Monday, April 27, 2015

Java FOR Loop



Flow of control in a FOR loop:

  1. The initialization step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears.
  2. Next, the Boolean expression is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement past the for loop.
  3. After the body of the for loop executes, the flow of control jumps back up to the update statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the Boolean expression.
  4. The Boolean expression is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then update step, then Boolean expression). After the Boolean expression is false, the for loop terminates.

Example

public class HelloWorld{
     public static void main(String []args){
        int i = 5;
        for(int k=0;k<=i;k++){
            System.out.println(k+"\n");
        }
     }
}