0% found this document useful (0 votes)
5 views

Java Practical

Uploaded by

txmxrma9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Java Practical

Uploaded by

txmxrma9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

INDEX

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)
{

// Declare the variables


intnum;
// Input the integer
System.out.println("Enter the integer: ");

// Create Scanner object


Scanner s = newScanner(System.in);

// Read the next integer from the screen


num = s.nextInt();

// Display the integer


System.out.println("Entered integer is: "
+ num);
}
}

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";

// Prints the String.


System.out.println("String s = " + s);

// Declare String using new operator


String s1 = new String("GeeksforGeeks");

// Prints the String.


System.out.println("String s1 = " + s1);
}
}
OUTPUT:
String s = GeeksforGeeks
String s1 = GeeksforGeeks
Page | 3
Program 3 // Write a Java program to demonstrate that a method can return
multiple values of same type by returning an array
Solution code:
class Test {
// Returns an array such that first element
// of array is a+b, and second element is a-b
staticint[] getSumAndSub(int a, int b)
{
int[] ans = new int[2];
ans[0] = a + b;
ans[1] = a - b;

// returning array of elements


returnans;
}

// 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;

// this would be invoked while an object


// of that class is created.
Geek() { System.out.println("Constructor called"); }
}
class GFG {
public static void main(String[] args)
{
// this would invoke default constructor.
Geek geek1 = new Geek();

// Default constructor provides the default


// values to the object like 0, null
System.out.println(geek1.name);
System.out.println(geek1.num);
}
}

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: ");

//The input provided by user is stored in num


Scanner input =newScanner(System.in);
num=input.nextInt();

// If number is divisible by 2 then it's an even number


//else it is an odd number
if(num%2==0)
System.out.println(num+" is an even number.");
else
System.out.println(num+" is an odd 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;
}

System.out.println("Reverse of input number is: "+reversenum);


}
}
OUTPUT:
Input your number and press enter:
145689
Reverse of input number is:986541

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));

/* If program control reaches to this statement it means


* the String is not palindrome hence return false.
*/
returnfalse;
}

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:

Enter the String for check:


qqaabb
qqaabb is not a palindrome
Output 2:

Enter the String for check:


cocoococ
cocoococ is a palindrome

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:

Enter the number: 5


Factorial of entered number is: 120

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();

System.out.println("Enter the search value:");


item=input.nextInt();

for(counter =0; counter <num; counter++)


{
if(array[counter]== item)
{
System.out.println(item+" is present at location "+(counter+1));
/*Item is found so to stop the search and to come out of the
* loop use break statement.*/
break;
}
}
if(counter ==num)
System.out.println(item +" doesn't exist in array.");
}

Page | 16
}

Output 1:

Enter number of elements:


6
Enter 6 integers
22
33
45
1
3
99
Enter the search value:
45
45is present at location 3

Output 2:

Enter number of elements:


4
Enter 4 integers
11
22
4
5
Enter the search value: 99
99 doesn't exist in array.

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);

//formula to calculate circumference of circle


double circumference=Math.PI*2*radius;
System.out.printf("\nCircumference is:
%.2f",circumference);
}
}
Output:

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:

Enter Side of Square:


2.5
Area of Squareis:6.25

Page | 20
Program 15// Write a Java program to calculate area of
triangle

importjava.util.Scanner;
classAreaTriangleDemo{
publicstaticvoid main(Stringargs[]){
Scannerscanner=newScanner(System.in);

System.out.println("Enter the width of the Triangle:");


doublebase=scanner.nextDouble();

System.out.println("Enter the height of the Triangle:");


double height =scanner.nextDouble();

//Area = (width*height)/2
double area =(base* height)/2;
System.out.println("Area of Triangle is: "+ area);
}
}
Output:

Enter the width of the Triangle:


2
Enter the height of the Triangle:
2
Area of Triangleis:2.0

********************************************************

Page | 21

You might also like