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

26. Program to find ASCII number and Value of entered character:

package sam2;

/* Program to find ASCII Number and Value of entered character
 * 
 */

import java.util.Scanner;

public class AsciiValue {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		
		System.out.println("Enter a Character");
		char ch = sc.next().charAt(0);
		
		int asciinumber = ch;
		int asciivalue = (int)ch;
		System.out.println("Ascii Number of "+ch+" = "+asciinumber);
		System.out.println("Ascii Value of "+ch+" = "+asciivalue);
		

	}

}

27. Program to convert Celcius to Fahrenheit.

package sam2;
/*Java Program to Convert Celsius to Fahrenheit
 * By Rahul Kundu
 * °F = °C * (9/5) + 32
 */

import java.util.Scanner;

public class Celcius_Farenhite {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
        System.out.println("Enter the temperature in Celsius : ");
        float c = sc.nextFloat();
        
        // Converting Celsius to Fahrenheit
        float f = c * (9.0f/5.0f) + 32;
        
        System.out.println("The temperature is "+f+" degree Fahrenheit.");
        

	}

}

28. Program to find factorial of a number:

package sam2;
/*
 * Java Program to Find Factorial of a Number

 */

import java.util.Scanner;

public class Factorial {

	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();
	    
	    if(num < 0){
	        System.out.println("Factorial of negative numbers cannot be calculated.");
	    } else if(num == 0){
	        System.out.println("Factorial of 0 is 1.");
	    }
	    else{
	        long factorial = 1;
	    
	        for(int i=num; i>1 ; i--){
	            factorial *= i;
	        }
	    
	        System.out.println("Factorial of "+num+" is "+factorial);
	    }

	}

}

29. Program to print fibonacci series.

package sam2;

import java.util.Scanner;
/*
 * Java Program to Print Fibonacci Series Using Loops
 */

public class Fibonacci {

	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 n = sc.nextInt();
        int nextValue = 0;
        int first = 0;
        int second = 1;
        System.out.print("0, 1, ");
        for(int i=3; i<=n ;i++){
           
            nextValue = first+second;
            first = second;
            second = nextValue;
            System.out.print(""+nextValue+", ");
        }

	}

}

30. Program to check if entered year is leap year or not:

package sam2;

/* Program to check whether entered year is Leap year or not
 * By Rahul Kundu
 */

import java.util.Scanner;

public class LeapYear {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
        
        System.out.println("Enter the Year : ");
        int year = sc.nextInt();
        
        boolean isLeap = false;
        
        if( ((year%4 == 0)&&(year%10!=0))||(year%400 == 0)){
            
            isLeap = true;
            
        }else{
            
           isLeap = false;
           
        }
        
        if(isLeap == true){
            System.out.println(""+year+" is a Leap Year");
        }else{
            System.out.println(""+year+" is Not a Leap Year");
        }
        

	}

}
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 – 268
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