Java Practical
Java Practical
PAGE
S.No. TOPIC
No.
1 2
Write a Java program to take an integers input and print it
2 3
Write a Java code to illustrate String
3 4
Write A Java program to demonstrate that a method can return multiple values
of same type by returning an array
4 6
Write a Java Program to illustrate calling a no-argument constructor
5 8
Write a java program to find Sum of two numbers
6 9
Write a java program to Check a number odd or even
7 10
Write a java Program: It Prints Floyd's triangle based on user inputs
8 12
Write a Java program to reverse a number
9 13
Write a java program Palindrome string
10 15
Write a java program to find factorial of a number
11 16
Write a java program for Binary search
12 18
Write a java Program to calculate area and circumference of Circle
13 Write a Java Program to Calculate Area of Rectangle 19
14 20
Write a Java program to calculate area of Square
15 Write a Java program to calculate area of Triangle 21
Page | 1
Program 1] Write a Java program to take an integer
as input and print it.
Solution code:
importjava.io.*;
importjava.util.Scanner;
classGFG {
publicstaticvoidmain(String[] args)
{
OUTPUT:
Enter the integer: 10
Entered integer is: 10
Page | 2
Program 2 // Write a Java code to illustrate String
Solution code:
import java.io.*;
importjava.lang.*;
class Test {
public static void main(String[] args)
{
// Declare String without using new operator
String s = "GeeksforGeeks";
// Driver method
public static void main(String[] args)
{
int[] ans = getSumAndSub(100, 50);
System.out.println("Sum = " + ans[0]);
System.out.println("Sub = " + ans[1]);
}
}
Page | 4
OUTPUT:
Sum = 150,Sub = 50
Page | 5
Program 4 // Write a Java Program to illustrate calling a no-argument
constructor
Solution code:
import java.io.*;
class Geek {
intnum;
String name;
Page | 6
OUTPUT:
Constructor called
null0
Page | 7
Program 5 // Write a java program to find Sum of two numbers
Solution code:
// Java Program to implement
// Direct Addition to Add two Numbers
import java.io.*;
// Driver Class
class GFG {
public static int sum(int num1, int num2)
{
return num1+num2;
}
// main function
public static void main(String[] args)
{
GFG ob = new GFG();
int res = ob.sum(28, 49);
System.out.println(res);
}
}
Output:
77
Page | 8
Program 6 // Write a java program to Check number is odd or even.
Solution code:
importjava.util.Scanner;
publicclassJavaExample
{
publicstaticvoid main(Stringargs[])
{
intnum;
System.out.print("Enter an Integer number: ");
OUTPUT:
Page | 9
Program 7 // Write a java Program: It Prints Floyd's triangle based on
user inputs.
Solution code:
importjava.util.Scanner;
classFloydTriangleExample
{
publicstaticvoid main(Stringargs[])
{
int rows, number =1, counter, j;
//To get the user's input
Scanner input =newScanner(System.in);
System.out.println("Enter the number of rows for floyd's triangle:");
//Copying user input into an integer variable named rows
rows=input.nextInt();
System.out.println("Floyd's triangle");
System.out.println("****************");
for( counter =1; counter <= rows ; counter++)
{
for( j =1; j <= counter ; j++)
{
System.out.print(number+" ");
//Incrementing the number value
number++;
}
//For new line
System.out.println();
}
}
Page | 10
OUTPUT:
Enter the number of rows forfloyd's triangle:
6
Floyd's triangle
****************
1
23
456
78910
1112131415
161718192021
Page | 11
Program 8 // Write a java program to find a Reverse of a number.
Solution code:
importjava.util.Scanner;
classReverseNumberWhile
{
publicstaticvoid main(Stringargs[])
{
intnum=0;
intreversenum=0;
System.out.println("Input your number and press enter: ");
//This statement will capture the user input
Scannerin=newScanner(System.in);
//Captured input would be stored in number num
num=in.nextInt();
//While Loop: Logic to find out the reverse number
while(num!=0)
{
reversenum=reversenum*10;
reversenum=reversenum+ num%10;
num=num/10;
}
Page | 12
Program 9// Write a java program of Palindrome string.
Solution code:
package beginnersbook.com;
importjava.util.Scanner;
classPalindromeCheck
{
//My Method to check
publicstaticbooleanisPal(String s)
{// if length is 0 or 1 then String is palindrome
if(s.length()==0||s.length()==1)
returntrue;
if(s.charAt(0)==s.charAt(s.length()-1))
/* check for first and last char of String:
* if they are same then do the same thing for a substring
* with first and last char removed. and carry on this
* until you string completes or condition fails
* Function calling itself: Recursion
*/
returnisPal(s.substring(1,s.length()-1));
publicstaticvoid main(String[]args)
{
//For capturing user input
Scannerscanner=newScanner(System.in);
System.out.println("Enter the String for check:");
Page | 13
Stringstring=scanner.nextLine();
/* If function returns true then the string is
* palindrome else not
*/
if(isPal(string))
System.out.println(string+" is a palindrome");
else
System.out.println(string+" is not a palindrome");
}
}
Output 1:
Page | 14
Program 10// Write a java program to find factorial of a number.
Solution code:
importjava.util.Scanner;
classFactorialDemo{
publicstaticvoid main(Stringargs[]){
//Scanner object for capturing the user input
Scannerscanner=newScanner(System.in);
System.out.println("Enter the number:");
//Stored the entered value in variable
intnum=scanner.nextInt();
//Called the user defined function fact
int factorial = fact(num);
System.out.println("Factorial of entered number is: "+factorial);
}
staticint fact(int n)
{
int output;
if(n==1){
return1;
}
//Recursion: Function calling itself!!
output= fact(n-1)* n;
return output;
}
}
Output:
Page | 15
Program 11 // Write a java program forBinary search.
Solution code:
importjava.util.Scanner;
classLinearSearchExample
{
publicstaticvoid main(Stringargs[])
{
int counter,num, item, array[];
//To capture user input
Scanner input =newScanner(System.in);
System.out.println("Enter number of elements:");
num=input.nextInt();
//Creating array to store the all the numbers
array=newint[num];
System.out.println("Enter "+num+" integers");
//Loop to store each numbers in array
for(counter =0; counter <num; counter++)
array[counter]=input.nextInt();
Page | 16
}
Output 1:
Output 2:
Page | 17
Program 12 // Write a Java Program to calculate area and
circumference of Circle.
Solution code:
classJavaExample
{
publicstaticvoid main(Stringargs[])
{
int radius =3;
//formula to calculate area of circle
double area =Math.PI*(radius * radius);
System.out.printf("Area is: %.2f", area);
Page | 18
Program 13 // Write a Java Program to Calculate Area of
rectangle.
Solution code:
importjava.util.Scanner;
classAreaOfRectangle{
publicstaticvoid main (String[]args)
{
Scannerscanner=newScanner(System.in);
System.out.println("Enter the length of Rectangle:");
double length =scanner.nextDouble();
System.out.println("Enter the width of Rectangle:");
double width =scanner.nextDouble();
//Area = length*width;
double area = length*width;
System.out.println("Area of Rectangle is:"+area);
}
}
Output:
Enter the length of Rectangle:
2
Enter the width of Rectangle:
8
Area of Rectangleis:16.0
Page | 19
Program 14 // Write a Java program to calculate area of
square.
Solution code:
importjava.util.Scanner;
classSquareAreaDemo{
publicstaticvoid main (String[]args)
{
System.out.println("Enter Side of Square:");
//Capture the user's input
Scannerscanner=newScanner(System.in);
//Storing the captured value in a variable
double side =scanner.nextDouble();
//Area of Square = side*side
double area = side*side;
System.out.println("Area of Square is: "+area);
}
}
Output:
Page | 20
Program 15// Write a Java program to calculate area of
triangle
importjava.util.Scanner;
classAreaTriangleDemo{
publicstaticvoid main(Stringargs[]){
Scannerscanner=newScanner(System.in);
//Area = (width*height)/2
double area =(base* height)/2;
System.out.println("Area of Triangle is: "+ area);
}
}
Output:
********************************************************
Page | 21