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

Computer Program (2) .1695725325483

The document contains 9 questions about Java programs to perform string operations. Each question provides sample code to implement a string operation like reversing a string, finding the smallest and largest word, checking for palindromes, separating vowels and consonants, checking if a word starts and ends with a vowel, finding the percentage of vowels in a word, prefixing vowels and consonants with an asterisk, counting the number of spaces, and counting the number of words. For each question, it provides an explanation of the variables used in the code.

Uploaded by

Mayank Sahu
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)
21 views

Computer Program (2) .1695725325483

The document contains 9 questions about Java programs to perform string operations. Each question provides sample code to implement a string operation like reversing a string, finding the smallest and largest word, checking for palindromes, separating vowels and consonants, checking if a word starts and ends with a vowel, finding the percentage of vowels in a word, prefixing vowels and consonants with an asterisk, counting the number of spaces, and counting the number of words. For each question, it provides an explanation of the variables used in the code.

Uploaded by

Mayank Sahu
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/ 63

Ques 1:- Write a program to reverse the string.

import java.util.*;

class string2

{public static void main()

{int i,l;

String a;

Scanner sc=new Scanner(System.in);

System.out.println("Enter Your String");

a=sc.nextLine();

l=a.length();

for(i=l-1;i>=0;i--)

char b=a.charAt(i);

System.out.print(b);

Output:-

Variable Description Table:

Variable Name Variable Type Purpose


l integer To store the length of string
a String To store the String
b character To store the character of string
i integer To initiate the loop

Ques 2-Write a program to print smallest and largest word of string.

import java.util.*;
public class SmallestLargestWord

public static void main(String[] args){

Scanner sc=new Scanner(System.in);

System.out.println("enter string");

String string = sc.nextLine();

String word = "", small = "", large="";

String[] words = new String[100];

int length = 0;

string = string + " ";

for(int i = 0; i < string.length(); i++){

if(string.charAt(i) != ' '){

word = word + string.charAt(i);

else{

words[length] = word;

length++;

word = "";

small = large = words[0];

for(int k = 0; k < length; k++){

if(small.length() > words[k].length())

small = words[k];

if(large.length() < words[k].length())

large = words[k];

System.out.println("Smallest word: " + small);

System.out.println("Largest word: " + large);


} }

Variable Description table

Variable Name Variable Type Purpose


length integer To store the length of string
string String To store the String
small String To store the smallest word of
string
large String To store the largest word of
string
i integer To initiate loop

Ques 3:- Program to check a string whether it is palindrome or not.

import java.util.*;

class string3

public static void main()

{
int i,l;

String a,s="";

Scanner sc=new Scanner(System.in);

System.out.println("Enter Your String");

a=sc.nextLine();

l=a.length();

for(i=l-1;i>=0;i--)

char b=a.charAt(i);

s=s+b;

if(s.equals(a))

System.out.println("Palindrome");

else

System.out.println("Not a Palindrome");

Output

Enter Your String

naman

Palindrome

Variable Name Variable Type Purpose


L integer To store the length of string
A String To store the String
B character To store the character of string
I integer To initiate the loop
S String To store reverse String

Ques 4-Program to print vowel first and then followed by constants.

import java.util.*;
class string4

public static void main()

int i,l;

String a,s="",s1="";

Scanner sc=new Scanner(System.in);

System.out.println("Enter Your String");

a=sc.nextLine();

l=a.length();

a=a.toLowerCase();

for(i=0;i<l;i++)

char b=a.charAt(i);

if( b=='a'||b=='e'||b=='i'||b=='o'||b=='u')

s=s+b;

else

s1=s1+b;

System.out.print(s+s1);

Variable Name Variable Type Purpose


L integer To store the length of string
A String To store the String
B character To store the character of string
I integer To initiate the loop
S String To store another String

Ques 5-Program to check whether word is start and end with vowel or not.
import java.util.*;

class string5

public static void main()

int l;

String a;

Scanner sc=new Scanner(System.in);

System.out.println("Enter Your String");

a=sc.nextLine();

l=a.length();

char z=a.charAt(0);

char m=a.charAt(l-1);

if((m=='a'||m=='e'||m=='i'||m=='o'||m=='u')&&(z=='a'||z=='e'||z=='i'||z=='o'||z=='u'))

System.out.println("True");

else

System.out.println("False");

Variable Name Variable Type Purpose


L integer To store the length of string
A String To store the String
Z character To store first character of String
I integer To initiate the loop
M character To store last character of String

Ques 6 Program to print percentage of vowels in Word

import java.util.*;
class string6

public static void main()

int i,l,p,s=0;

String a;

Scanner sc=new Scanner(System.in);

System.out.println("Enter Your String");

a=sc.nextLine();

l=a.length();

a=a.toLowerCase();

for(i=0;i<l;i++)

char b=a.charAt(i);

if(b=='a'||b=='e'||b=='i'||b=='o'||b=='u')

s=s+1;

p=(s*100)/l;

System.out.println("Percentage is"+p);

Variable Name Variable Type Purpose


L integer To store the length of string
A String To store the String
B character To store the character of string
I integer To initiate the loop
S String To store another String
P integer To store the percentage
Ques 7: Program to print * preceded by vowel and constant of word

import java.util.*;

class string7

public static void main()

{int i,l;

String a;

Scanner sc=new Scanner(System.in);

System.out.println("Enter Your String");

a=sc.nextLine();

l=a.length();

a=a.toLowerCase();

for(i=0;i<l;i++)

{ char b=a.charAt(i);

if( b=='a'||b=='e'||b=='i'||b=='o'||b=='u')

System.out.print(b+"*");

else

System.out.print(b);

Variable Name Variable Type Purpose


L integer To store the length of string
A String To store the String
B character To store the character of string
I integer To initiate the loop
Ques 8: Program to print number of spaces in string.

import java.util.*;

class string8

public static void main()

int i,l,s=0;

String a;

Scanner sc=new Scanner(System.in);

System.out.println("Enter Your String");

a=sc.nextLine();

l=a.length();

a=a.toLowerCase();

for(i=0;i<l;i++)

char b=a.charAt(i);

if(b == ' ')

s=s+1;

System.out.print(“No. of spaces is”+s);

Variable Name Variable Type Purpose


L Integer To store the length of string
A String To store the String
B Character To store the character of string
I Integer To initiate the loop
S String To store another String
Ques 9: Program to print number of words in string.

import java.util.*;

class string9

public static void main()

int i,l,s=0;

String a;

Scanner sc=new Scanner(System.in);

System.out.println("Enter Your String");

a=sc.nextLine();

l=a.length();

a=a.toLowerCase();

for(i=0;i<l;i++)

char b=a.charAt(i);

if(b== ' ')

s=s+1;

System.out.println("Number Of Word Is"+(s+1));

Variable Name Variable Type Purpose


L Integer To store the length of string
A String To store the String
B Character To store the character of string
I Integer To initiate the loop
S String To store another String
Ques 10: Program to check if word is special or not.

import java.util.*;

class string10

public static void main()

String a;

int l;

Scanner sc=new Scanner(System.in);

System.out.println("Enter Your String");

a=sc.nextLine();

l=a.length();

char m=a.charAt(0);

char n=a.charAt(l-1);

if(m==n)

System.out.println("Special Word");

else

System.out.println("Not a Special Word");

Output:

Enter Your String

oppo

Special Word
Enter Your String

awasthi

Not a Special Word

Variable Name Variable Type Purpose


L Integer To store the length of string
A String To store the String
N Character To store first character of String
I Integer To initiate the loop
M Character To store last character of String

Ques 11- Program to print a reverse of number.

import java.util.*;

class number1

public static void main()

int i, n,s=0;

Scanner sc=new Scanner(System.in);

System.out.println("Enter Your Number");

n=sc.nextInt();

for(i=n;i>0;i=i/10)

s=s*10+i%10;

System.out.println("Reverse is"+"\t"+s);

Output:

Enter Your Number


123

Reverse is 321

Variable Description Table

Variable Name Variable Type Purpose


n integer To store the number
s integer To store the reverse of number
i integer To initiate the loop

Ques 12: Program to check Armstrong number.

import java.util.*;

class number2

{public static void main()

int n,s=0;

Scanner sc=new Scanner(System.in);

System.out.println("Enter Your Number");

n=sc.nextInt();

int b=n;

while(n!=0)

int a=n%10;

s=s+a*a*a;

n=n/10;

if(s==b)

System.out.println("Armstrong Number");

else

System.out.println("Not a Armstrong Number");


}

Output:

Enter Your Number

121

Not a Armstrong Number

Enter Your Number

153

Armstrong Number

Variable Name Variable Type Purpose


N Integer To store the number
S Integer To store the another number
B Integer To store the copy of n
A Integer To store digits of n

Ques 13-Program to check palindrome number of three digit without loop.

import java.util.*;

class number3

public static void main()

int n,a,j,num,s;

Scanner sc=new Scanner(System.in);

System.out.println("Enter Your Number");

n=sc.nextInt();

num=n;

a=n%10;

n=n/10;

j=n%10;
n=n/10;

s=a*100+j*10+n;

if(s==num)

System.out.println("Palindrome Number");

else

System.out.println("Not a Palindrome Number");

Output:-

Enter Your Number

152

Not a Palindrome Number

Enter Your Number

121

Palindrome Number

Variable Name Variable Type Purpose


N Integer To store the number.
A Integer To store the one’s digit of
number.
J Integer To store the ten’s digit of
number.
S Integer To store the reverse of number.
Num Integer To store the copy of n.

Ques 14 Program to check spy number or not.

import java.util.*;

class number4

{ public static void main()

int n,a,p=1,d,s=0;

Scanner sc=new Scanner(System.in);

System.out.println("Enter Your Number");


n=sc.nextInt();

a=n;

while(n!=0)

{ d=n%10;

s=s+d;

p=p*d;

n=n/10;

if(p==s)

System.out.println("Spy Number");

else

System.out.println("Not a Spy Number");

Output:

Enter Your Number

48

Not a Spy Number

Enter Your Number

1124

Spy Number

Variable Name Variable Type Purpose


N Integer To store the number
S Integer To store the sum of digits
P Integer To store the products of digits
D Integer To store the digits

Ques 15 Program to check prime number or not.

import java.util.*;
class number5

public static void main()

int n,count=0;

Scanner sc=new Scanner(System.in);

System.out.println("Enter Your Number");

n=sc.nextInt();

for(int i=2;i<=n/2;i++)

{if(n%i==0)

count++;

break;

if(count==0)

System.out.println("Prime Number");

else

System.out.println("Not a prime Number");

Output:

Enter Your Number

Prime Number

Variable Name Variable Type Purpose


N Integer To store the number
Count Integer To store the counter
I Integer To initiate the loop
Ques 16: Program to print days of week using switch case

import java.util.*;

class switch1

public static void main()

int ch;

Scanner sc=new Scanner(System.in);

System.out.println("Enter Your Number");

ch=sc.nextInt();

switch(ch)

case 1:

System.out.println("Monday");

break;

case 2:

System.out.println("Tuesday");

break;

case 3:

System.out.println("Wednesday");

break;

case 4:

System.out.println("Thursday");

break;

case 5:

System.out.println("Friday");

break;
case 6:

System.out.println("Saturday");

break;

case 7:

System.out.println("Sunday");

break;

default:

System.out.println("Wrong Choice");

Output:

Enter Your Number

Wednesday

Enter Your Number

Wrong choice

Variable Description Table

Variable Name Variable Type Purpose


Ch integer To store the choice of user
Ques 17: Program to calculate addition,substraction and multiplication using switch case.

import java.util.*;

class switch2

public static void main()

int ch;

Scanner sc=new Scanner(System.in);

System.out.println("Enter Your 1 for Addition");

System.out.println("Enter Your 2 for Substraction");

System.out.println("Enter Your 3 for MUltiplication");

ch=sc.nextInt();

switch(ch)

case 1:

int a,b,p=0;

System.out.println("Enter Your a & b");

a=sc.nextInt();

b=sc.nextInt();

p=a+b;

System.out.println("Addition is"+ p);

break;

case 2:

int l,m,n=0;

System.out.println("Enter Your l and m");

l=sc.nextInt();

m=sc.nextInt();
p=l-m;

System.out.println("Substract is"+ p);

break;

case 3:

int r,s,t=0;

System.out.println("Enter Your r & s");

r=sc.nextInt();

s=sc.nextInt();

t=r*s;

System.out.println("Multiplication is"+t);

default:

System.out.println("Wrong Choice");

Output:

Enter Your 1 for Addition

Enter Your 2 for Substraction

Enter Your 3 for MUltiplication

Enter Your a & b

50

10

Addition is 60

Variable Name Variable Type Purpose


Ch Integer To store the choice of user
a Integer To store the number for
addition
B Integer To store the number for
addition
P Integer To store the sum of a and b
l Integer To store the number for
substraction
m integer To store the number for
substraction
N integer To store the substaction of l and
m
R integer To store the number for
multiplication
S Integer To store the number for
multiplication
T integer To store the multiplication of r
and s

Ques 18: Program to calculate volume of cuboid,sphere and cube using switch case.

import java.util.*;

class switch3

public static void main()

int ch;

Scanner sc=new Scanner(System.in);

System.out.println("Enter 1 for volume of cuboid");

System.out.println("Enter 2 for volume of Sphere");

System.out.println("Enter Your 3 for volume of cube");

ch=sc.nextInt();

switch(ch)

{
case 1:

int l,b,h,v=0;

System.out.println("Enter length,breadth & height");

l=sc.nextInt();

b=sc.nextInt();

h=sc.nextInt();

v=l*b*h;

System.out.println("volume of cuboid is"+ v);

break;

case 2:

double r,vo=0;

System.out.println("Enter radius");

r=sc.nextDouble();

vo=(4*22*r*r*r)/(7*3);

System.out.println("volume of sphere is"+ vo);

break;

case 3:

int a,vol=0;

System.out.println("Enter side");

a=sc.nextInt();

vol=a*a*a;

System.out.println("volume of cube is"+vol);

default:

System.out.println("Wrong Choice");

Output:

Enter 1 for volume of cuboid


Enter 2 for volume of Sphere

Enter Your 3 for volume of cube

Enter radius

volume of sphere is523

Variable Name Variable Type Purpose


Ch Integer To store the choice of user
L Integer To store the length of cuboid
B Integer To store the breadth of cuboid
h Integer To store the height of cuboid
V Integer To store the volume of cuboid
R Double To store the radius of sphere
Vo Integer To store the volume of sphere
A Integer To store the side of cube
Vol Integer To store the volume of cube

Ques 19: Program to calculate surface area of cuboid,sphere and cube using switch case.

import java.util.*;

class switch4

{
public static void main()

int ch;

Scanner sc=new Scanner(System.in);

System.out.println("Enter 1 for surface area of cuboid");

System.out.println("Enter 2 for surface area of Sphere");

System.out.println("Enter Your 3 for surface area of cube");

ch=sc.nextInt();

switch(ch)

case 1:

int l,b,h,s=0;

System.out.println("Enter length,breadth & height");

l=sc.nextInt();

b=sc.nextInt();

h=sc.nextInt();

s=2*(l*b+b*h+l*h);

System.out.println("surface area of cuboid is"+ s);

break;

case 2:

double r,sa=0;

System.out.println("Enter radius");

r=sc.nextDouble();

sa=(4*22*r*r)/7;

System.out.println("surface area of sphere is"+ sa);

break;

case 3:

int a,sar=0;

System.out.println("Enter side");
a=sc.nextInt();

sar=a*a*6;

System.out.println("surface area of cube is"+sar);

Break;

default:

System.out.println("Wrong Choice");

Output:

Enter 1 for surface area of cuboid

Enter 2 for surface area of Sphere

Enter Your 3 for surface area of cube

10

Wrong Choice

Enter 1 for surface area of cuboid

Enter 2 for surface area of Sphere

Enter Your 3 for surface area of cube

Enter side

65

surface area of cube is25350

Variable Name Variable Type Purpose


Ch Integer To store the choice of user
L Integer To store the length of cuboid
B Integer To store the breadth of cuboid
H Integer To store the height of cuboid
S Integer To store the surface area of
cuboid
R Double To store the radius of sphere
Sa Integer To store the surface area of
sphere
A Integer To store the side of cube
Sar Integer To store the surface area of
cube

Ques 20: Program to print pattern using switch case

import java.util.*;

class switch5

public static void main()

int ch;

Scanner sc=new Scanner(System.in);

System.out.println("Enter 1 for star pattern");

System.out.println("Enter 2 for rectangle pattern");

System.out.println("Enter Your 3 for number");

ch=sc.nextInt();

switch(ch)

case 1:

for(int i=0;i<=5;i++)

{for(int j=0;j<=i;j++)

System.out.print("*");

System.out.println();

break;

case 2:

for(int i=0;i<=5;i++)

{for(int j=0;j<=5;j++)
System.out.print("*");

System.out.println();

break;

case 3:

for(int i=0;i<=5;i++)

{for(int j=0;j<=i;j++)

System.out.print(i);

System.out.println();

break;

default:

System.out.println("Wrong Choice");

Output:

Enter 1 for star pattern

Enter 2 for rectangle pattern

Enter 3 for number pattern

******

******

******

******

Variable Description Table

Variable Name Variable Type Purpose


ch integer To store the choice of user
i integer To initiate the outer loop
j integer To initiate the inner loop

Ques 21 Program to print sum by using function

import java.util.*;

class function1

{int a,b;

function1()

{a=0;

b=0;

} public void input()

{Scanner sc=new Scanner(System.in);

System.out.println("Enter Your a and b");

a=sc.nextInt();

b=sc.nextInt();

public void addition()

{ int p=0;

p=a+b;

System.out.println("Addition is"+ p);

} public static void main()

{ function1 ob=new function1();

ob.input();

ob.addition();

}}

Output:
Enter your a and b

10

20

Addition is 30

Ques 22 Program to calculate substraction using function

import java.util.*;

class function2

{ int a,b;

function2()

{a=0;

b=0;

Variable Name Variable Type Purpose


A Integer To store the first number
B Integer To store the second number
P Integer To store the sum of number

}public void input()

{Scanner sc=new Scanner(System.in);

System.out.println("Enter Your a and b");

a=sc.nextInt();

b=sc.nextInt();

}public void substraction()

{int p=0;

p=a-b;

System.out.println("Substract is"+ p); }

public static void main()

{function2 ob=new function2();

ob.input();

ob.substraction();
}}

Output:

Enter Your a and b

40

20

Substract is 20

Variable Name Variable Type Purpose


A Integer To store the first number
B Integer To store the second number
P Integer To store the difference of
number

Ques 23 Program to calculate perimeter of rectangle using function

import java.util.*;

class function3

{int l,b;

function3()

{l=0;

b=0;

}public void input()

{Scanner sc=new Scanner(System.in);

System.out.println("Enter Your a and b");

l=sc.nextInt();

b=sc.nextInt();

}public void perimeter()

{int p=0;

p=2*(l+b);
System.out.println("Perimeter is"+ p);

}public static void main()

{function3 ob=new function3();

ob.input();

ob.perimeter();

Output:

Enter Your a and b

40

52

Perimeter is 184

Variable Name Variable Type Purpose


A Integer To store the length of rectangle
B Integer To store the breadth of
rectangle
P Integer To store the perimeter of
rectangle

Ques 24 Program to check palindrome using function

import java.util.*;

class function4

{ int l;

String a,b;

function4()

a="";

b="";

}
public void input()

Scanner sc=new Scanner(System.in);

System.out.println("Enter STRING");

a=sc.nextLine();

l=a.length();

public void palindrome()

int i;

for(i=l-1;i>=0;i--)

char c=a.charAt(i);

b=b+c;

if(b.equals(a))

System.out.println("Palindrome");

else

System.out.println("Not a Palindrome");

public static void main()

function4 ob=new function4();

ob.input();

ob.palindrome();

}
Output:

Enter STRING

adarsh

Not a Palindrome

Enter STRING

naman

Palindrome

Variable Description Table

Variable Name Variable Type Purpose


l integer To store the length of string
a String To store the String
b character To store the character of string
i integer To initiate the loop
s String To store reverse String

Ques 25: Program to calculate divide using function

import java.util.*;

class function5

{int a,b;

function5()

{a=1;

b=1;

}public void input()

{Scanner sc=new Scanner(System.in);

System.out.println("Enter Your a and b");


a=sc.nextInt();

b=sc.nextInt();

}public void divide()

{int p=0;

p=a/b;

System.out.println("Divide is"+ p);

}public static void main()

{function5 ob=new function5();

ob.input();

ob.divide();

}}

Output:

Enter Your a and b

50

Divide is10

Ques 26 Program to calculate multiplication using function

import java.util.*;

class function6

{int a,b;

function6()

{a=0;

b=0;

}public void input()

{Scanner sc=new Scanner(System.in);

System.out.println("Enter Your a and b");


a=sc.nextInt();

b=sc.nextInt();

}public void multiply()

{int p=0;

p=a*b;

System.out.println("Product is"+ p);

}public static void main()

{function6 ob=new function6();

ob.input();

ob.multiply();

}}

Output:

Enter Your a and b

15

100

Product is1500

Variable Name Variable Type Purpose


A Integer To store the first number
B Integer To store the second number
P Integer To store the divide of number

Ques 27 Program to calculate simple interest using function

import java.util.*;

class function7

int p,r,t;

public void function7()

{ p=0,r=0,t=0;
} public void input()

{Scanner sc=new Scanner(System.in);

System.out.println("Enter Your priciple & rate & time");

p= sc.nextInt();

r=sc.nextInt();

t=sc.nextInt();

}public void calculate()

{int si=0;

si=(p*r*t)/100;

System.out.println("Simple Intrest"+si);

}public static void main()

{function7 ob=new function7();

ob.input();

ob.calculate();

Output:

Enter Your principle & rate & time

1000

12

Simple Intrest:-240

Variable Name Variable Type Purpose


p Integer To store the principal
R Integer To store the rate
T Integer To store the time
si Integer To store the simple interest

Ques 28 Program to check prime number using function.

import java.util.*;
class function8

int n;

function8()

n=0;

public void input()

Scanner sc=new Scanner(System.in);

System.out.println("Enter Your Number");

n=sc.nextInt();

public void checkprime()

int i,t=0;

for(i=1;i<=n;i++)

if(n%i==0)

t=t++;

if(t==2)

System.out.println("Prime");

else

System.out.println("Not Prime");

public static void main()


{

function8 ob=new function8();

ob.input();

ob.checkprime();

Output:

Enter Your Number

10

Not Prime

Enter Your Number

Prime

Variable Description Table:

Variable Name Variable Type Purpose


i integer To initiate the loop
n integer To store the number
t integer To store the counter

Ques 29 Program to print factorial of number using function.

import java.util.*;
class function9

{int n;

function9()

n=0;

}public void input()

{Scanner sc=new Scanner(System.in);

System.out.println("Enter Your Number");

n=sc.nextInt();

}public void factorial()

{int i,t=1;

for(i=1;i<=n;i++)

{t=t*i;

System.out.println("Factorial is"+t);

}public static void main()

{function9 ob=new function9();

ob.input();

ob.factorial();

Output:

Enter Your Number

Factorial is 120

Variable Name Variable Type Purpose


i Integer To initiate the loop
N Integer To store the number
T Integer To store the factorial of number
Ques 30 Program to calculate some operation using function.

import java.util.*;

class function10

int l,b,r,s;

public void function10()

l=0;

b=0;

r=0;

s=0;

public void input()

Scanner sc=new Scanner(System.in);

System.out.println("Enter Your Length & Breadth & radius & side");

l=sc.nextInt();

b=sc.nextInt();

r=sc.nextInt();

s=sc.nextInt();

public void rectangle()

int ar=0;

ar=l*b;

System.out.println("Area of Rectangle"+ar);
}

public void square()

int ar=0;

ar=s*s;

System.out.println("Area of Square"+ar);

public void circle()

double ar=0;

ar=3.14*r*r;

System.out.println("Area of Circle"+ar);

public static void main()

{function10 ob=new function10();

ob.input();

ob.rectangle();

ob.square();

ob.circle();

Output:

Enter Your Length & Breadth & radius & side

20

30

40

Area of Rectangle600
Area of Square1600

Area of Circle78.5

Ques 31 Program to print array.

import java.util.*;

class array1

public static void main()

int i;

Scanner sc=new Scanner(System.in);

int m[]=new int [5];

for(i=0;i<5;i++)

{System.out.println("Enter element of Array");

m[i]=sc.nextInt();

for(i=0;i<5;i++)

{System.out.print(m[i]+"\t");

} }

Output:

Enter element of Array

Enter element Array

10

Enter element of Array

15

Enter element of Array

20

Enter element of Array


12

5 10 15 20 12 6

Variable Name Variable Type Purpose


L Integer To store the length of rectangle
B Integer To store the breadth of
rectangle
R Integer To store the radius of circle
S Integer To store the side of circle
Ar Integer To store the area

Ques 32 Program to perform binary search operation.

import java.util.*;

class binary

{ public static void main()

int a[]=new int[5];

int x;

int index=-1;

Scanner sc=new Scanner(System.in);

for(int i=0;i<=5-1;i++)

System.out.println("Enter Your Element");

a[i]=sc.nextInt();

System.out.print("enter element to be searched");

x=sc.nextInt();
int low=0,high=4;

while(low<high)

{int mid=(low+high)/2;

if(a[mid]==x)

index=mid;

break;

else if(a[mid]>x)

high=mid-1;

else

low=mid+1;

if(index==-1)

System.out.print("element is not present");

else

System.out.print("element is present at"+(index+1));

Output:

Enter Your Element

Enter Your Element

10
Enter Your Element

15

Enter Your Element

30

Enter Your Element

25

enter element to be searched15

element is present at 3

Ques 33 Program to find sum of array

import java.util.*;

class array3

{public static void main()

{int i,s=0;

Scanner sc=new Scanner(System.in);

int m[]=new int [5];

for(i=0;i<5;i++)

{System.out.println("Enter Your Array");

m[i]=sc.nextInt();}

for(i=0;i<5;i++)

{System.out.println(m[i]+"\t");

}for(i=0;i<5;i++)

{s=s+m[i];

System.out.println("Sum is"+"\t"+s);

Output:
Enter Your Array

10

15

20

25

5 10 15 20 25

Sum is 75

Variable Name Variable Type Purpose


I integer To initiate the loop
a[] integer To store the integer array
X integer To store the element to be
searched
Index integer To store the index of searched
element
Low integer To store first index of array
High integer To store last index of array

Ques 34 Program to check if two array is same or not

import java.util.*;

class array4

public static void main()

int i,j;

Scanner sc=new Scanner(System.in);

int m[]=new int [5];

for(i=0;i<5;i++)

System.out.println("Enter Your Array");


m[i]=sc.nextInt();

for(i=0;i<5;i++)

System.out.println(m[i]+"\t");

int p[]=new int[5];

for(j=0;j<5;j++)

System.out.println("Enter Your Second Array");

p[j]=sc.nextInt();

for(j=0;j<5;j++)

System.out.println(p[j]+"\t:");

for(i=0;i<5;i++)

if(m[i]==p[i])

System.out.println("Both Array Are Same");

else

System.out.println("Both Array Are Not Same");

Output:

Enter Your Array

5
10

15

20

25

Enter Your Second Array

10

15

20

25

Both Array Are Same

Both Array Are Same

Both Array Are Same

Both Array Are Same

Both Array Are Same

Variable Name Variable Type Purpose


I integer To initiate the loop
m[] integer To store the integer array

Ques 35 Program to print 2-D array

import java.util.*;

class array5

public static void main()

int i,j;

Scanner sc=new Scanner(System.in);

int m[][]=new int[3][3];


for(i=0;i<3;i++)

for(j=0;j<3;j++)

System.out.println("Enter Array");

m[i][j]=sc.nextInt();

for(i=0;i<3;i++)

for(j=0;j<3;j++)

System.out.print(m[i][j]+"\t");

System.out.println();

Output:

Enter Array

7
8

1 2 3

4 5 6

7 8 9

Variable Description Table:

Variable Name Variable Type Purpose


i integer To initiate the loop for rows
m[][] integer To store the 2D integer array
j integer To initiate the loop for columns

Ques 36 Program to print sum of 2-D array

import java.util.*;

class array6

public static void main()

int i,j,s=0;

Scanner sc=new Scanner(System.in);


int m[][]=new int[3][3];

for(i=0;i<3;i++)

for(j=0;j<3;j++)

System.out.println("Enter Your Array");

m[i][j]=sc.nextInt();

for(i=0;i<3;i++)

for(j=0;j<3;j++)

System.out.print(m[i][j]+"\t");

System.out.println();

} for(i=0;i<3;i++)

for(j=0;j<3;j++)

s=s+m[i][j];

}System.out.println("Sum is"+"\t"+s);

Output

Enter Your Array

1
Enter Your Array

Enter Your Array

Enter Your Array

Enter Your Array

Enter Your Array

Enter Your Array

Enter Your Array

Enter Your Array

1 2 3

4 5 6

7 8 9

Sum is 45

Variable Name Variable Type Purpose


I Integer To initiate the loop for rows
m[][] Integer To store the 2D integer array
J Integer To initiate the loop for columns
S Integer To store the sum of 2D array

Ques 37 Program to print sum of left diagonal of2-D array

import java.util.*;

class array7

{public static void main()


{

int i,j,s=0;

Scanner sc=new Scanner(System.in);

int m[][]=new int[3][3];

for(i=0;i<3;i++)

for(j=0;j<3;j++)

System.out.println("Enter Your Array");

m[i][j]=sc.nextInt();

for(i=0;i<3;i++)

for(j=0;j<3;j++)

System.out.print(m[i][j]+"\t");

System.out.println();

for(i=0;i<3;i++)

for(j=0;j<3;j++)

if(i==j)

s=s+m[i][j];

}System.out.println("Sum of left diagonal is"+"\t"+s);


}

Output:

Enter Your Array

Enter Your Array

Enter Your Array

Enter Your Array

Enter Your Array

Enter Your Array

Enter Your Array

Enter Your Array

Enter Your Array

1 2 3

4 5 6

7 8 9

Sum of left diagonal is 15

Variable Name Variable Type Purpose


I Integer To initiate the loop for rows
m[][] Integer To store the 2D integer array
J Integer To initiate the loop for columns
S Integer To store the sum of left diagonal
of 2D array
Ques 38 Program to print sum of right diagonal of 2-D array

import java.util.*;

class array8

public static void main()

int i,j,s=0;

Scanner sc=new Scanner(System.in);

int m[][]=new int[3][3];

for(i=0;i<3;i++)

for(j=0;j<3;j++)

{System.out.println("Enter Your Array");

m[i][j]=sc.nextInt();

for(i=0;i<3;i++)

for(j=0;j<3;j++)

System.out.print(m[i][j]+"\t");

System.out.println();

for(i=0;i<3;i++)

for(j=0;j<3;j++)

{
if(i+j==2)

s=s+m[i][j];

}System.out.println("Sum of Right Diagonal"+"\t"+s);

Output

Enter Your Array

Enter Your Array

Enter Your Array

Enter Your Array

Enter Your Array

Enter Your Array

Enter Your Array

Enter Your Array

Enter Your Array

1 2 3

4 5 6

7 8 9
Sum of Right Diagonal 15

Variable Name Variable Type Purpose


I integer To initiate the loop for rows
m[][] integer To store the 2D integer array
J integer To initiate the loop for columns
S integer To store the sum of right
diagonal of 2D array

Ques 39 Program to change the column of 2-d Array

import java.util.*;

class array9

public static void main()

int i,j,s=0;

Scanner sc=new Scanner(System.in);

int m[][]=new int[3][3];

for(i=0;i<3;i++)

for(j=0;j<3;j++)

System.out.println("Enter Your Array");

m[i][j]=sc.nextInt();

for(i=0;i<3;i++)

for(j=0;j<3;j++)

{
System.out.print(m[i][j]+"\t");

System.out.println();

for(i=0;i<3;i++)

for(j=2;j>=0;j--)

System.out.print(m[i][j]+"\t");

System.out.println();

Output:

Enter Your Array

Enter Your Array

Enter Your Array

Enter Your Array

Enter Your Array

Enter Your Array

6
Enter Your Array

Enter Your Array

Enter Your Array

1 2 3

4 5 6

7 8 9

3 2 1

6 5 4

9 8 7

Variable Description Table:

Variable Name Variable Type Purpose


I integer To initiate the loop for rows
m[][] integer To store the 2D integer array
J integer To initiate the loop for columns
Ques 40 Program to calculate sum of boundary elements

import java.util.*;

class array10

public static void main()

int i,j,s=0;

Scanner sc=new Scanner(System.in);

int m[][]=new int[3][3];

for(i=0;i<3;i++)

for(j=0;j<3;j++)

System.out.println("Enter Your Array");

m[i][j]=sc.nextInt();

for(i=0;i<3;i++)

for(j=0;j<3;j++)

System.out.print(m[i][j]+"\t");
}

System.out.println();

for(i=0;i<3;i++)

{for(j=0;j<3;j++)

if(i==0||j==0||i==2||j==2)

s=s+m[i][j];

}System.out.println("Sum is"+"\t"+s);

Output:

Enter Your Array

Enter Your Array

Enter Your Array

Enter Your Array

Enter Your Array

Enter Your Array

Enter Your Array

Enter Your Array

8
Enter Your Array

1 2 3

4 5 6

7 8 9

Sum is 40

Variable Name Variable Type Purpose


I Integer To initiate the loop for rows
m[][] Integer To store the 2D integer array
J Integer To initiate the loop for columns
S Integer To store the sum of boundary
element

You might also like