ch-7 8 Program
ch-7 8 Program
import java.util.*;
class Main_301
{
public static void main(String[] arg)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter n: ");
int n = sc.nextInt();
System.out.print("Enter r: ");
int r = sc.nextInt();
Recursion11 r11 = new Recursion11();
int Fact_N = r11.factorial(n);
System.out.println("Factorial of n = "+Fact_N);
int Fact_N_R = r11.factorial(n-r);
System.out.println("Factorial of n-r = "+Fact_N_R);
QB-302 create a class Student with Roll_No ,Name and Mobile_No as data member. Use
necessary method to initialize it and to print. Create at least 5 student. (Use array of
object).
class Main
{
public static void main(String[] arg)
{
Student a[] = new Student[5];
for(int i=0; i<5; i++)
{
a[i] = new Student();
System.out.println("----Enter Student "+(i+1)+" Data:----");
a[i].getData();
}
System.out.println("=============================");
System.out.println("====Student Details=============");
System.out.println("=============================");
for(int i=0; i<5; i++)
{
a[i].printData();
}
}
}
class Student
{
int RN;
String name;
long Mo_no;
void getData()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter Roll no: ");
RN = sc.nextInt();
sc.nextLine();
System.out.print("Enter Student name: ");
name = sc.nextLine();
System.out.print("Enter Mobile no: ");
Mo_no = sc.nextLong();
System.out.println();
}
void printData()
{
System.out.println();
System.out.println("Roll no= "+RN);
System.out.println("Student name= "+name);
System.out.println("Mobile No= "+Mo_no);
}
}
class CO1
{
void print()
{
System.out.println("Hello");
}
}
class Run
{
public static void main(String args[])
{
CO1 ob = new CO1 ();
ob.print();
}
}
QB-304 Write a Java program to find area and perimeter of a circle using class
class CO2
{
double area,r,peri;
double P = Math.PI;
void Area(double r)
{
area = P*r*r;
System.out.println("Area of circle = "+area);
}
void Perimeter(double r)
{
peri = 2*P*r;
System.out.println("Perimeter of circle = "+peri);
}
}
class Run
{
public static void main(String args[])
{
CO2 p1 = new CO2();
p1.Area(20);
p1.Perimeter(20);
}
}
QB-305 Write a Java program to count all digits of an integer number using class
import java.util.*;
class CO3
{
int count=0;
Scanner sc = new Scanner(System.in);
void count(int n)
{
while(n!=0)
{
n=n/10;
count++;
}
System.out.println("Count of digit = "+count);
}
}
class Run
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
CO3 p1 = new CO3();
System.out.println("Enter any integer digit n:");
int n = sc.nextInt();
p1.count(n);
}
}
QB-306 Write a Java program to find product of all digits of an integer number using
class
import java.util.*;
class CO4
{
int sum = 0,r,product=1;
Scanner sc = new Scanner (System.in);
void sumPro()
{
System.out.println("Eneter number n:");
int n = sc.nextInt();
while(n!=0)
{
r = n%10;
product=product*r;
n=n/10;
}
}
}
class Run
{
public static void main(String args[])
{
CO4 p1 = new CO4();
p1.sumPro();
}
}
QB-307 Write a Java program to check whether a given number is palindrome or not
using class
import java.util.*;
class CO5
{
int temp=0,rev=0,r;
void peli(int n)
{
temp = n;
while(n!=0)
{
r=n%10;
rev = (rev*10) + r;
n=n/10;
}
if(temp == rev)
{
System.out.println("Yes Pelindrome");
}
else
{
System.out.println("Not Pelindrome");
}
}
}
class run
{
p1.peli(n);
}
}
QB-308 Write a Java program to check whether a given number is Armstrong or not
using class
import java.util.*;
class CO6
{
double sum=0;
int r,count=0,temp=0;
void arm(int n)
{
temp = n;
while(n!=0)
{
n=n/10;
count++;
}
n=temp;
while(n!=0)
{
r=n%10;
sum= sum +((Math.pow(r,count)));
n=n/10;
}
if(temp == sum)
{
System.out.println("Yes armstrong");
}
else
{
System.out.println("Not armstrong");
}
}
}
class run
{
p1.arm(n);
}
}
QB-309 Create a class name Temperature in which create methods of given name
ferenhit(), celcius() to perform basic conversion. Call all this methods using class named
Main.
class Temperature{
class Main{
public static void main(String[] args) {
Temperature T1 = new Temperature();
T1.ferenhit(100);
T1.celsius(212);
}
}
QB-310 Write a java program to access two variables and two methods with different
name using class.
class Test {
void method1(){
System.out.println("Method 1 Called");
}
void method2(){
System.out.println("Method 2 Called");
}
}
class Main{
public static void main(String[] args) {
Test T1 = new Test();
System.out.println("Variable 1 Accessed: "+T1.var1);
System.out.println("Variable 2 Accessed: "+T1.var2);
T1.method1();
T1.method2();
}
}
QB-311 Write a Java program to perform basic Calculator operations using following
methods catagories: sum() - without arguments without return, minus() - with
arguments without return, multi() - without arguments with return divide() - with
arguments with return
import java.util.*;
class EX311
{
Scanner sc=new Scanner(System.in);
void sum() //without arguments without return,
{
System.out.println("Lets do Addition");
System.out.print("Enter value 1: ");
int a=sc.nextInt();
System.out.print("Enter value 2: ");
int b=sc.nextInt();
System.out.println("sum= "+(a+b));
}
class Run
{
public static void main (String args[])
{
Scanner sc=new Scanner(System.in);
EX311 e = new EX311();
e.sum();
System.out.println("Lets do Substraction");
System.out.print("Enter value 1: ");
int x=sc.nextInt();
System.out.print("Enter value 2: ");
int y=sc.nextInt();
e.minus(x,y);
int ans=e.multi();
System.out.println("multi= "+ans);
System.out.println("Lets do Division");
System.out.print("Enter value 1 : ");
int p=sc.nextInt();
System.out.print("Enter value 2 : ");
int q=sc.nextInt();
double ans2=e.divide(p,q);
System.out.println("division= "+ans2);
}
}
import java.util.*;
class Main
{
public static void main(String[] arg)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter number 1: ");
int n1 = sc.nextInt();
System.out.print("Enter number 2: ");
int n2 = sc.nextInt();
Recursion r = new Recursion();
r.hcf(n1,n2);
}
}
class Recursion
{
void hcf(int n1, int n2)
{
if (n1%n2==0)
{
System.out.print("HCF of Two numbers n1 and n2 is="+n2);
return;
}
else
{
hcf(n2, n1%n2);
}
}
}
class Product
{
int pro_Id;
String pro_name;
void set(){
Scanner sc=new Scanner(System.in);
pro_Id=sc.nextInt();
sc.nextLine();
pro_name=sc.next();
}
void display()
{
System.out.print("Product Id = "+pro_Id + " " + " Product Name = "+pro_name);
System.out.println();
}
}
QB-314 Write a Java Program to Create a class Account. It has three data member
account id, name and balance. Define method to assign value and display value. Define
method that search account number given by the user. If account number exists, print
detail of that account. Write a program using array of object. Declare at least 5 account
and print details.
import java.util.*;
class CO11
{
int id;
String name;
double balance;
Scanner sc =new Scanner(System.in);
void assign()
{
System.out.println("Enter id no:");
id = sc.nextInt();
sc.nextLine();
System.out.println("Enter Name:");
name = sc.nextLine();
System.out.println("Enter balance:");
balance = sc.nextDouble();
}
void display()
{
System.out.println("ID="+id);
System.out.println("Name="+name);
System.out.println("Balance="+balance);
}
int search()
{
System.out.println("enter value of id you want to search x");
int x = sc.nextInt();
return x;
}
}
class run
{
public static void main(String args[])
{
CO11 a[]=new CO11[3];
for(int i=0;i<3;i++)
{
a[i]=new CO11();
a[i].assign();
a[i].display();
}
CO11 s = new CO11();
int x = s.search();
for(int i=0;i<3;i++)
{
if(x==a[i].id)
{
a[i].display(); break;
}
else
{
System.out.println("Not found");
}
}
}
}
QB-315 Design a class named Fan to represent a fan. The class contains: - Three
constants named SLOW, MEDIUM and FAST with values 1, 2 and 3 to denote the fan
speed. - An int data field named speed that specifies the speed of the fan (default
SLOW). - A boolean data field named f_on that specifies whether the fan is on (default
false). - A double data field named radius that specifies the radius of the fan (default 4).
- A data field named color that specifies the color of the fan (default blue). - A no-arg
method that creates a default fan. - A parameterized method initializes the fan objects
to given values. - A method named display() will display description for the fan. If the
fan is on, the display() method displays speed, color and radius. If the fan is not on, the
method returns fan color and radius along with the message “fan is off”. Write a test
program that creates two Fan objects. One with default values and the other with
medium speed, radius 6, color brown, and turned on status true. Display the
descriptions for two created Fan objects.
class fann
{
final int SLOW=1,MEDIUM=2,FAST=3;
int Speed;
boolean f_on;
double radius;
String color;
void defaultfan()
{
Speed=1;
f_on=false;
radius=4;
color="blue";
}
void display()
{
if(f_on==true)
{
System.out.println("Speed = "+Speed);
System.out.println("Radius = "+radius);
System.out.println("Color = "+color);
}
else
{
System.out.println("Fan is off");
System.out.println("Radius = "+radius);
System.out.println("Color = "+color);
}
}
}
class run1
{
public static void main(String args[])
{
fann f1 = new fann();
fann f2 = new fann();
f1.defaultfan();
f2.fann(2,true,6,"brown");
f1.display();
f2.display();
}
}
QB-316 Define the Rectangle class that contains: Two double fields x and y that specify
the center of the rectangle, the data field width and height, A noarg method that creates
the default rectangle with (0,0) for (x,y) and 1 for both width and height. A
parameterized method creates a rectangle with the specified x,y,height and width. -A
method getArea() that returns the area of the rectangle. -A method getPerimeter() that
returns the perimeter of the rectangle. -A method contains(double x, double y) that
returns true if the specified point (x,y) is inside this rectangle. Write a test program that
creates two rectangle objects. One with default values and other with user specified
values. Test all the methods of the class for both the objects.
class rectangle14
{
double x,y,width,hight;
void defaultData()
{
x=0;
y=0;
width=1;
hight=1;
}
void set(double a,double b,double c,double d)
{
x=a;
y=b;
width=c;
hight=d;
}
void getArea()
{
System.out.println("Area = "+(width*hight));
}
void getPeri()
{
System.out.println("Perimeter = "+(2*(width+hight)));
}
boolean search(double x,double y)
{
if(((x<=(width/2)) || (x<=(-(width/2)))) && ((y<=(hight/2)) || (y<=(-
(hight/2)))) )
{
return true;
}
else
{
return false;
}
}
}
class run
{
public static void main(String args[])
{
rectangle14 r1 = new rectangle14();
rectangle14 r2 = new rectangle14();
r1.defaultData();
r2.set(0,0,2,2);
r1.getArea();
r1.getPeri();
r2.getPeri();
r2.getArea();
boolean q = r1.search(1.1,1);
System.out.println(q);
}
}
QB-317 Write a program to calculate factorial using recursion in Java
class RecursiveFactorial {
QB-318 Write a Java program to calculate the power of a number like power (int
number, int power) like power (2, 3) should return 8.
import java.util.*;
class Pow1 {
// Driver code
public static void main(String[] args)
{
Pow1 p=new Pow1();
Scanner sc=new Scanner(System.in);
int N = sc.nextInt();
int P = sc.nextInt();
System.out.println(p.power(N, P));
}
}
QB-319 Write a Java program to convert Decimal to binary using recursion
import java.util.*;
class DtoB
{
else
return (decimal_number % 2 + 10 *
find(decimal_number / 2));
}
// Driver Code
public static void main(String args[])
{
DtoB d=new DtoB();
Scanner sc=new Scanner(System.in);
int decimal_number = sc.nextInt();
System.out.println(d.find(decimal_number));
}
}
QB-320 Write a Java program to find GCD and LCM of two numbers using recursion
import java.util.*;
class GcdLcm {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
GcdLcm g = new GcdLcm();
System.out.print("Enter n1:");
int n1 = sc.nextInt();
System.out.print("Enter n2:");
int n2 = sc.nextInt();
int gcd = g.hcf(n1, n2);
System.out.println("G.C.D of "+n1+ " and "+n2+ " is: "+gcd);
System.out.printf("L.C.M of "+n1+ " and "+n2+ " is: "+((n1*n2)/gcd));
}
QB-321 Write a Java program to print fibbonacci series upto n terms using recursion
class RecursiveFibo {
QB - 363 WAP that calculates area of circle, triangle and square using method
overloading.
class OverloadDemo
{
void area(float x)
{
System.out.println("the area of the square is "+Math.pow(x, 2)+" sq units");
}
void area(float x, float y)
{
System.out.println("the area of the rectangle is "+x*y+" sq units");
}
void area(double x)
{
double z = 3.14 * x * x;
System.out.println("the area of the circle is "+z+" sq units");
}
}
class Overload
{
public static void main(String args[])
{
OverloadDemo ob = new OverloadDemo();
ob.area(5);
ob.area(11,12);
ob.area(2.5);
}
}
QB-364 Write a Java program to sort the numbers by using the concept of passing
arrays to methods
import java.util.*;
class Arr{
void sort(int a[]){
int temp;
for(int i = 0; i<a.length; i++){
for(int j = i+1;j<a.length;j++){
if(a[i]>a[j]){
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
void display(int a[]){
for(int i = 0; i<a.length; i++){
System.out.print(a[i]+" ");
}
}
}
class Run{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
Arr aa = new Arr();
System.out.println("How many Elements:");
int n = sc.nextInt();
int a[] = new int[n];
System.out.println("Enter Elements:");
for(int i = 0; i<a.length; i++){
a[i] = sc.nextInt();
}
aa.sort(a);
System.out.println("After sorting Elements:");
aa.display(a);
}
}
QB- 365 Write a Java program to display elements of one dimensional array using
passing arrays to methods.
class array1D{
void display(int[] array){
System.out.println("Yor array is:");
for(int q: array)
System.out.print(q);
}
void display(int[][] array){
System.out.println("Yor array is:");
for(int j[]: array)
for(int i: j)
System.out.print(i);
}
}
class RunD{
public static void main(String[] args) {
int[] a = {11,12,13};
int[][] b = {{1,2,3},{1,2,3},{1,2,3}};
array1D aone = new array1D();
aone.display(a);
aone.display(b);
}
}
QB- 366 Write a Java program to find sum of n numbers by using the concept of call
by value
import java.util.*;
class Arr{
Scanner sc = new Scanner(System.in);
int sum(int n){
int sum=0;
for(int i = 1; i<= n; i++){
System.out.print("Enter elements: ");
int s = sc.nextInt();
sum = sum + s;
}
return sum;
}
}
class Run{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
Arr aa = new Arr();
System.out.println("How many Elements:");
int n = sc.nextInt();
System.out.println("Sum of "+n+ "numbers:"+aa.sum(n));
}
}
QB- 367 Write a Java program to find the power of number using passing an object to
the method(i.e num=5 and power=3 then ans is5^3 that is 125)
import java.util.*;
class Ex_8_367{
int num,pow,result=1;
return obj1;
}
}
class run{
public static void main(String args[])
{
Ex_8_367 obj2=new Ex_8_367();
Ex_8_367 obj3=new Ex_8_367();
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Number ");
obj2.num=sc.nextInt();
System.out.println("Enter the value of Power");
obj2.pow=sc.nextInt();
obj3=obj2.findPow(obj2);
System.out.println("power of given number is
"+obj3.result);
}
}
QB- 368 Write a Java program to find GCD of two numbers using passing an object to
the method(i.e a=4 and b=6 then GCD is 2)
import java.util.*;
class EX_8_368
{
int a,b;
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("ENTER a and b=");
a=sc.nextInt();
b=sc.nextInt();
}
void gcd(EX_8_368 ob1)
{
while(ob1.a!=ob1.b)
{
if(ob1.a>=ob1.b)
ob1.a=ob1.a-ob1.b;
else
ob1.b=ob1.b-ob1.a;
}
System.out.println("GCD="+ob1.a);
}
}
class Run
{
public static void main(String args[])
{
EX_8_368 ob1 = new EX_8_368();
EX_8_368 ob2 = new EX_8_368();
ob1.input();
ob2.gcd(ob1);
}
}
QB- 369 Write a Java method that accept three integers and check whether they are
consecutive are not. Return true or false. Expected Output: Input the first number: 15
Input the second number: 16 Input the third number: 17 Check whether the three said
numbers are consecutive or not!true
import java.util.Scanner;
class Main {
public static void main(String[] args)
{
Main m=new Main();
Scanner in = new Scanner(System.in);
System.out.print("Input the first number: ");
int x = in.nextInt();
System.out.print("Input the second number: ");
int y = in.nextInt();
System.out.print("Input the third number: ");
int z = in.nextInt();
System.out.print("Check whether the three said numbers are
consecutive or not!");
System.out.println(m.test(x,y,z)); }
import java.util.Scanner;
public class CountPositiveNegative3 {
public static void main(String[] args)
{
int Size, i;
int positiveCount = 0, negativeCount = 0;
Scanner sc = new Scanner(System.in);
}
public static int CountPositive(int [] a, int Size)
{
int i, positiveCount = 0;
System.out.print("\n List of Positive Numbers in this Array are :" );
for(i = 0; i < Size; i++)
{
if(a[i] >= 0)
{
System.out.print(a[i] +" ");
positiveCount++;
}
}
return positiveCount;
}
public static int CountNegative(int [] a, int Size)
{
int i, negativeCount = 0;
System.out.print("\n List of Negative Numbers in this Array are :" );
for(i = 0; i < Size; i++)
{
if(a[i] < 0)
{
System.out.print(a[i] + " ");
negativeCount++;
}
}
return negativeCount;
}
}
QB-371 Create a class comparison which has compare() method which compare two
integer value, character value and double value using method overloading
import java.util.Scanner;
class Compare
{
void compare(int a, int b) {
if (a == b) {
System.out.println("value of "+a+" and "+b+" are same");
}
else {
System.out.println("value of "+a+" and "+b+" are not same");
}
if (a == b) {
System.out.println("value of "+a+" and "+b+" are same");
}
else {
System.out.println("value of "+a+" and "+b+" are not same");
}
if (a == b) {
System.out.println("value of "+a+" and "+b+" are same");
}
else {
System.out.println("value of "+a+" and "+b+" are not same");
}
}
void display(int[][] array){
System.out.println("Yor array is:");
for(int j[]: array)
for(int i: j)
System.out.print(i);
}
}
class RunD{
public static void main(String[] args) {
int[] a = {11,12,13};
int[][] b = {{1,2,3},{1,2,3},{1,2,3}};
aone.display(a);
aone.display(b);
}
}
QB-374 Write a Java program to find sum of first n odd numbers by using the concept
of call by value.
class FindSumOdd {
int sumOfOddNums(int n) {
return n * n;
}
QB-375 Write a Java program to add 5 in original value of array elements and display
modified elements of one dimensional array using pass by value mechanism.
class Array {
void print(int a) {
System.out.println(a);
}
}
QB-376 Write a Java program to change original value, which is given from user by
using appropriate Parameter passing technique.
class CallByReference {
int a, b;
CallByReference object
= new CallByReference();
object.set(10, 20);
System.out.println("Value of a: " + object.a
+ " & b: " + object.b);
object.ChangeValue(object);
QB-377 Write a Java program to Swap two values, which is given from user by using
call by value
import java.util.Scanner;
class SwapNumber
{
public static void main(String args[])
{
int x, y;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the first number: ");
x = sc.nextInt();
System.out.print("Enter the second number: ");
y = sc.nextInt();
System.out.println("Before Swapping\nx = "+x+"\ny = "+y);
swap(x, y);
}
public static void swap(int a, int b)
{
a = a + b;
b = a - b;
a = a - b;
System.out.println("After Swapping\nx = "+a+"\ny = "+b);
}
}
QB-378 Write a Java program to Swap two values, which is given from user by using
call by reference.
class CallByReference {
int a, b;
CallByReference object
= new CallByReference();
object.set(10, 20);
System.out.println("Value of a: " + object.a
+ " & b: " + object.b);
object.swapvalue(object);
QB-379 Write a program using call by reference concept to read an array of integers
and print its elements in reverse order.
class Array{
public static void main(String[] args) {
int a[] = {10,20,30,40,50};
Array obj = new Array();
obj.print(a);
}
QB-380 Write a Java program to copy one array into another array using call by
referrence.
class Array{
public static void main(String[] args) {
int a[] = {10,20,30,40,50};
int b[] = a;
Array obj = new Array();
obj.print(b);
}
System.out.println("Distacne = "+a+"."+b+"KM");
}
if(d4.m>=1000)
{
d4.km+=1;
d4.m-=1000;
}
return d4;
}
}
class run
{
public static void main(String args[])
{
CO7 d1 = new CO7();
CO7 d2 = new CO7();
CO7 d3 = new CO7();
CO7 d4 = new CO7();
d1.get();
d1.show(d1.km,d1.m);
d2.get();
d2.show(d2.km,d2.m);
d4 = d3.add(d1,d2);
d4.show(d4.km,d4.m);
}
}
QB-382 Write a Java Program to Create a class Account. It has three data member
account id, name and balance. Define method to assign value and display value. Define
method that search account number given by the user by passing array to method. If
account number exists, print detail of that account. Write a program using array of
object. Declare at least 5 account and print details.
import java.util.*;
class Ex_8_382 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of accounts: ");
int n = sc.nextInt();
Account1 [] ac = new Account1[n];
for (int i=0;i<ac.length;i++)
{
ac[i]= new Account1();
System.out.print("Enter Account Number: ");
int p = sc.nextInt();
System.out.print("Account Holder name: ");
sc.nextLine();
String q = sc.nextLine();
System.out.print("Account Balance: ");
int r = sc.nextInt();
ac[i].setData(p,q,r);
}
boolean c=false;
for (int i = 0; i < ac.length; i++) {
if ((ac[i].id==a)) {
c = true;
ac[i].getData();
}
}
if (!c)
System.out.println("Account details are not found");
}
}
class Account1
{
int id;
String name;
double bal;
void setData(int x, String y, double z)
{
id =x;
name = y;
bal = z;
}
void getData()
{
System.out.println("----Account Details----");
System.out.println("Account id: "+id);
System.out.println("Account Holder name: "+name);
System.out.println("Account Balance: "+bal);
}
int accountSearch()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter Account number which you want to
search: ");
int a = sc.nextInt();
return a;
}
}
QB-383 Write a java program to find min and max values from a given array using
passing arrays to methods.
import java.util.Scanner;
class Array {
public int max(int[] array) {
int max = 0;
QB-384 Define a class Time with hours and minutes as two instance data members, add
necessary instance methods to initialize and display data of class. Define a instance
method sum() which adds two Time objects. Invoke the statements like T3.sum(T1,T2)
in main ().
import java.util.*;
public class Ex_384 {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.println("Enter time-1 in hour");
int h1=sc.nextInt();
System.out.println("Enter time-1 in minutes");
int m1=sc.nextInt();
System.out.println("Enter time-2 in hour");
int h2=sc.nextInt();
System.out.println("Enter time-2 in minutes");
int m2=sc.nextInt();
Time t1= new Time();
Time t2= new Time();
Time t3= new Time();
t1.input(h1,m1);
t2.input(h2,m2);
t3.sum(t1,t2);
t3.print();
}
}
class Time
{
int hour;
int min;
void print()
{
System.out.println("Total hours:"+hour);
System.out.println("Total minutes:"+min);
}
}
}
}
class Coordinate
{
int x,y,z;
void input (int a, int b, int c)
{
x=a;
y=b;
z=c;
}
void display()
{
System.out.format("\nCoordinates of point is %d, %d,
%d",x,y,z);
}
QB- 386 Write a java program to reverse elements of array using logic of swapping
elements. Here, use concept of passing array as argument to method.
class arrayReverse
{
// function swaps the array's first element with last element, second
element with
void reverse (int a[], int n)
{
int i, k, t;
for (i = 0; i < n / 2; i++) {
t = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = t;
}
System.out.println("Reversed array is: \n");
for (k = 0; k < n; k++) {
System.out.println(a[k]);
}
}