Tag: java loop program

  • Java Program to Display the Odd Numbers

    The following program takes the value for n that is the max limit of n from the user. The program starts checking for odd numbers between this range 1 to n and it displays all the odd numbers lying within that range.

    Before we start, click on the links below to have a proper working idea of for loop and if statement that this program uses.


    Program to display the Odd Numbers within the range in Java

    //display the odd number in java
    
     import java.util.*;
    
     public class DisplayOddNumbers
     {
        public static void main(String args[]) 
        {
      int n;
      Scanner sc=new Scanner(System.in);
      
      System.out.print("Enter the number: ");
        n=sc.nextInt();
        
      System.out.print("Odd Numbers between 1 to "+n+" are: ");
      
        for (int i = 1; i <= n; i++) 
        {
          // if the number is not divisible by 2 then it is even
          if (i % 2 != 0) 
          {
            System.out.print(i + " ");
          }
        }
      }
     }

    Output:

    Enter the number: 10
    Odd Numbers between 1 to 10 are: 1 3 5 7 9


  • Java Program to Find a Factorial of a Number

    In this example, we will calculate the factorial of a number taking the value from the user. You may also learn to find Factorial of a Number in Java Using recursion.

    Before we begin, you should have the knowledge of the following:

    Factorial of n number: Factorial of n number is the product of all the positive descending integers and is denoted by n!.

    Example:

    factorial of n (n!) = n * (n-1) * (n-2) * (n-3)….1
    factorial of 5 (n!) = 5 * 4 * 3 * 2 * 1
    NOTE: Factorial of 0 (0!) = 1


    Java Program to Find a Factorial of a Number Using for loop.

     //find the factorial of a number in java
    
     import java.util.Scanner;
    
     public class FactorialOfANumber
     {
      public static void main(String args[])
      {
        int fact=1;
        Scanner scanner = new Scanner(System.in);
    
        System.out.print("Enter the number: ");
        int num = scanner.nextInt();
    
        for(int i=1;i<=num;i++)
        {
         fact = fact * i;
        }
        System.out.println("Factorial of entered number is: "+fact);
      }
    
     }

    Output:

    Enter the number: 5
    Factorial of entered number is: 120


  • Java Program to Find the Sum of Natural Numbers

    It is the Basic Java Program to find the sum of N natural numbers. The program below uses the While loop, if you want to learn about the while loop click below.

    Example: If you want to find the sum of 5 natural numbers, the process will be executed in this manner:
    1 + 2 + 3 + 4 + 5
    The result will be: 15


    Java Program to Find the Sum of Natural Numbers using While Loop

    //Sum of Natural numbers in java
      
    public class SumNaturalNumbers 
    {
      public static void main(String[] args) 
       {
    
       int n = 100, count = 1, sum = 0;
    
       while(count <= n)
       {
           sum = sum + count;
           count++;
       }
    
       System.out.println("Sum: "+sum);
       }
    }

    Output:

    Sum: 5050

    In the above program, n is already defined in the program but if you wish to take the input of n from the user, just use the scanner class.


  • Java Program to Check whether the Number is Palindrome or not using While loop

    What is a palindrome number?

    A number is said to be a Palindrome number if it remains the same when its digits are reversed or are the same as forward. It is also applied to the word, phrase, or other sequences of symbols.

    For example: 14141, 777, 272 are palindrome numbers as they remain the same even if reversed. If we take the example of ‘mam’ or ‘madam’, these are also palindrome words.


    Java Program to Check whether the Number is Palindrome or not using While loop

    //check the number for Palindrome using while loop(user inputs) in java 
    
     import java.util.Scanner;
    
     public class PalindromeCheck 
     {
       public static void main(String[] args) 
       {
         int n, rev = 0, remainder, originalNumber;
        
         Scanner sc = new Scanner(System.in);
         
         System.out.print("Enter an integer: ");
         n = sc.nextInt();
             
         originalNumber = n;
             
         // reversing 
         while( n!= 0 )
         {
           remainder = n % 10;
           rev = rev  * 10 + remainder;
           n /= 10;
         }
             
         // Displaying
         if (originalNumber == rev )
             System.out.println(originalNumber + " is a palindrome.");
         else
               System.out.println(originalNumber + " is not a palindrome.");
        }
     }

    Output:

    Enter an integer: 1991
    1991 is a palindrome.


  • Java Program to list Prime Numbers using for Loop

    This post on Java Program to list Prime numbers using for loop is the same as the Java Program to check whether a number is prime or not.

    The explanation is the same as checking for prime numbers, the only difference is that we set the last range number that is we take user input for the last number and check for prime number from 1 to that last number.

    And if the boolean variable isPrime is true after checking for each number, it will print the number for each iteration.


    Java Program to list Prime Numbers using for Loop

     //list Prime numbers using for loop
    
     import java.util.Scanner;
    
     public class ListingPrimeNumbers
     {
      
       public static void main(String[] args) 
       {
         
         Scanner scanner = new Scanner(System.in);
         
         System.out.println("Enter the last number:");
         int num = scanner.nextInt();
     
         System.out.println("Displaying Prime numbers between 1 to " +num);
         
         for(int i = 1; i < num; i++)
         {
           
           boolean isPrime = true;
          
           //checking for prime
           for(int j=2; j < i ; j++)
           {
            
            if(i % j == 0)
            {
             isPrime = false;
             break;
            }
           }
          //Displaying the numbersr 
          if(isPrime)
            System.out.print(i + " ");
        }
      }
     }

    Output:

    Enter the last number:
    50
    Displaying Prime numbers between 1 to 50
    1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47

  • Java Program to Reverse the Number

    The following Java Program uses the Multiplication and Modulus Operator to reverse the number entered by the User. Something like this,

    If the entered Number is 1234567, then the final result will display 7654321.

    If you do not know about the Operator in Java, click the link below.


    Example: Java Program to Reverse the Number using while loop

    //reverse a number in java
    
     import java.util.Scanner;
    
     public class ReverseNumberJava
     {
       public static void main(String args[])
       {
         int n, rev = 0;
    
         Scanner in = new Scanner(System.in);
    
         System.out.print("Enter an integer to reverse: ");
         n = in .nextInt();
    
         while (n != 0)
         {
           rev = rev * 10;
           rev = rev + n % 10;
           n = n / 10;
         }
    
         System.out.println("Reverse of the entered interger is " + rev);
       }
     }

    Output of reversing a number:

    Enter an integer to reverse: 1234567
    Reverse of the entered interger is 7654321