-->

Hello,This is me!

ITEngineer8995

Professional Coder Coding is my passion

About me

Hello

I'mITEngineer8995

Spatial Data Specialist

Do you want all of your codes in one place?? Then you are landed on the right blog... Here you will find varieties of codes of various languages starting from noob to advance with their samples. Do learn, execute and share your knowledge with others. Do visit my YouTube Channel for execution of these codes .

Experience

Video Clone Detector using Hadoop

2017-2018

A detector which differentiates copied videos from the original ones using Hadoop.

Portfolio

Java Program to Display Prime Numbers from 1 to n

 

















Java Program to Display Prime Numbers from 1 to n

In this Java Program, prime numbers from 1 to n are displayed to the user.

A prime number is a natural number greater than 1 that is not a product of two small natural numbers.

E.g. 2, 3, 5 etc are prime numbers.

Step 1 : Create main method and create the object of  Scanner.

Step 2 : Initialize i to zero and num to zero.

Step 3 : Create an empty String named primeNumbers

Step 4 : Ask the user to enter the value of n using System.out.print method

Step 5 : Store the object of Scanner class in  variable n

Step 6 : Close the Scanner 

Step 7 : Apply For loop where i is equal to 1,  i is less than equal to n and increment i by 1

Step 8 : Initialize counter to 0

Step 9 : Apply another for loop where num is equal to i, num is greater than equal to 1 and decrement num by 1 

Step 10 : Apply if statement where i % num == 0

Step 11 : Increment counter by 1 and store it in variable counter

Step 12 : Close both the for loops

Step 13 : Apply if statement where counter == 2

Step 14 : Append the Prime number to the String

Step 15 : Print the output

Program :

import java.util.Scanner;
public class PrimeNumbersTwo 
    {
        public static void main(String[]args)
        {
            Scanner sc = new Scanner(System.in);
            int i = 0;
            int num = 0;
            //Empty String
            String primeNumbers = "";
            System.out.println("Enter the value of n:");
            int n = sc.nextInt();
            sc.close();
        for(i = 1; i <= n; i++)
        {
            int counter = 0;
            for(num = i; num>=1; num--)
            {
                if(i%num == 0)
                {
                    counter = counter + 1;
                   
                }
            }
            if (counter == 2)
                
            {
                //Appended the Prime number to the String
                primeNumbers = primeNumbers + i + " ";
            }
        }
            System.out.println("Prime numbers from 1 to n are:");
            System.out.println(primeNumbers);
    
}
}

Output:

Enter the value of n:
150
Prime numbers from 1 to n are:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 

For execution of the same do visit my YouTube Channel :


Java Program to Display Prime Numbers from 1 to 100

  

















Java Program to Display Prime Numbers from 1 to 100

In this Java Program, prime numbers from 1 to 100 are displayed to the user.

A prime number is a natural number greater than 1 that is not a product of two small natural numbers.

E.g. 2, 3, 5 etc are prime numbers.

Step 1 : Initialize i to zero

Step 2 : Initialize num to zero

Step 3 : Create an empty string named as primeNnumbers

Step 4 : Apply for loop where num is equal to i, num is greater than equal to 1 and decrement num

Step 5 : Apply if statement inside for loop where i mod num is equal equal to zero

Step 6 : Increment counter by one and store it in counter variable

Step 7 : Close the for loop

Step 8 : Apply if statement where counter is equal equal to 2

Step 9 : Append the prime number to the string

Step 10 : Print the output by using System.out.println method

Program :

public class PrimeNumbers {
    
    public static void main (String[] args)
    {
        int i =0;
        int num =0;
        //Empty String
        String primeNumbers = "";
        
        for(i = 1; i<=100; i++)
        {
            int counter = 0;
            for(num = i; num>= 1; num--)
            {
                if(i%num==0)
                {
                    counter= counter + 1;
                }
            }
            if(counter == 2)
            {
                //Appended the Prime number to the String
                        primeNumbers = primeNumbers + i + "";
               
            }
        }
        System.out.println("Prime numbers from 1 to 100 are:");
        System.out.println(primeNumbers);
    }
    
}

Output:

Prime numbers from 1 to 100 are:
2357111317192329313741434753596167717379838997

For execution of the same do visit my YouTube Channel :





ITEngineer8995
India

SEND ME A MESSAGE

Search This Blog

Powered by Blogger.

Pages