Skip to content
testingbuddy
  • Home
  • Manual Testing
    • Test Cases
  • Mobile Testing
  • Automation Testing
    • Selenium
    • Assignments
    • Appium
    • Frameworks
  • API Testing
    • Postman
    • Rest Assured
  • ChatGPT
  • Java
    • Sample Java Programs
  • Python
    • Sample Python Program
  • Downloads
  • About Us

Sample Java Programs

Java

Sample Java Programs

31. Program to check if number is prime or not:

package sam2;

import java.util.Scanner;

public class Prime {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Scanner scn = new Scanner(System.in);
	    //Asking for Input
	    System.out.println("Enter the number you want to check : ");
	    int number = scn.nextInt();
	    if (number == 2) {
	      System.out.println("2 is a Prime Number !!!");
	    }
	    else {
	      int count = 0;
	        //logic
	      for (int div = 2; div * div <= number ; div++) {
	        if (number % div == 0) {
	          count++;
	        }
	      }
	      if (count == 0) {
	        System.out.println(""+number+" is a Prime Number !!!");
	      } else {
	        System.out.println(""+number+" is NOT a Prime Number !!!");
	      }
	    }

	}

}

32. Program to print reverse of a number.

package sam2;

import java.util.Scanner;
/*
 * Java Program To Reverse a Number
 */

public class Reverse {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter value of n: ");
	     int num = sc.nextInt();
	     
	     int originalNumber = num;
	     int reversedNumber = 0;
	     
	     while(num > 0){
	         int digit = num % 10;
	         num = num / 10;
	         reversedNumber = reversedNumber * 10 + digit; 
	     }
	     
	     System.out.println("Original Number : "+originalNumber);
	     System.out.println("Reversed Number : "+reversedNumber);
	    }

	

}

33. Program to swap two number.

package sam2;

import java.util.Scanner;

public class SwapNumber {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Scanner sc = new Scanner(System.in);
        System.out.println("Enter first number: ");
        int firstNum = sc.nextInt();
        System.out.println("Enter second number: ");
        int secondNum = sc.nextInt();
        
        //Before Swapping two numbers
        System.out.println("Before Swapping :");
        System.out.println("First number is "+firstNum);
        System.out.println("Second number is "+secondNum);
        
        //Swapping the numbers
        int temp = firstNum;
        firstNum = secondNum;
        secondNum = temp;
        
        //After Swapping two numbers-
        System.out.println("After Swapping :");
        System.out.println("First number is "+firstNum);
        System.out.println("Second number is "+secondNum);
        

	}

}

33. Program to add two numbers using function.

package sam2;
/*
 * Java Program to Add Two Numbers Using Functions
 * By Rahul Kundu
 */

import java.util.Scanner;

public class SumFunction {
	
	public static int sum(int n1, int n2) {
		int ans = n1 + n2;
		return ans;
		
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Scanner sc = new Scanner(System.in);
        
        System.out.println("Enter the first number: ");
        int n1 = sc.nextInt();
        
        System.out.println("Enter the second number: ");
        int n2 = sc.nextInt();
        
        int sum = sum(n1,n2);
        
        System.out.println("The sum of "+n1+" and "+n2+" is "+sum);
        

	}

}

34. Program to find sum of array:

package sam2;

public class SumArray {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int arr[] = new int[] {1, 3, 4, 6, 7, 8};
		int sum = 0;
		for (int i = 0; i<arr.length;i++) {
			sum = sum + arr[i];
		}
		System.out.println("Sum of all the elements of an array: " + sum); 

	}

}

35. Program to find Reverse of String.

package sam2;

import java.util.Scanner;

public class StringRev01 {

	public static void main(String[] args)throws java.lang.Exception
	{
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter a word: ");
		String str = sc.nextLine();
		// Convert String to character array
		char ch[]=str.toCharArray();  

		// Reversing the String
    		String rev="";  
    		for(int i=ch.length-1;i>=0;i--){  
        		rev+=ch[i];  
    		}  
  
		System.out.println("Reverse of "+str+ " is " +rev);

	}

}
Pages: 1 2 3 4 5 6 7 8

Recent Posts

  • Mobile application testing and ADB commands
  • Delete Method in Rest Assured
  • Put and Patch method in Rest Assured
  • Post Method in Rest Assured
  • Get Request in Rest Assured

Recent Comments

  • yourtestingbuddy@gmail.com on Prototype Model
  • Открыть учетную запись в binance on Clear text from text area using selenium WebDriver.
  • James_onern on Spiral Model
  • odpri racun na binance on How to obtain the tagname of the parent element in Selenium webdriver?
  • binance koda on How to scroll down a webpage in selenium using Java?

Archives

  • November 2023
  • July 2023
  • June 2023
  • May 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • August 2022
  • April 2022
  • January 2022
  • April 2021
  • March 2021
  • January 2021

Find Us

Address
Hyderabad, 500084, India

Hours
Monday–Friday: 9:00AM–5:00PM
Saturday & Sunday: 11:00AM–3:00PM

Visitors online – 270
The maximum number of visits was – 2025-03-16
Visitors Count – 0

Copyright 2020 - 2025 || www.testingbuddy.co.in || Rahul Kundu
Theme by Colorlib Powered by WordPress