10th Prog comp
10th Prog comp
int oddSum= 0;
num[i]=
in.nextInt();
if(num[i]%2==0)
evenSum+= num[i];
else
{
oddSum+= num[i];
}
}
System.out.println("Sum of even
numbers:"+evenSum); System.out.println("Sum of
odd numbers:"+oddSum);
Output-
Variable Name Data type Description
main() - -
evenSum int To store sum of all even
numbers
oddSum int To store sum of all odd
numbers
num int[] Variable of an Array
of size 20 to store the
numbers
i int Loop variable to iterate
through a range of 20
m[i]= in.nextInt();
}
System.out.print("Prime numbers
are:"); for(i=0;i<20;i++)
c=0;
for(j=1;j<=m[i];j++)
if(m[i]
%j==0)
c=c+1;
}
if(c==2)
System.out.print(" "+m[i]+",");
}
}
}
Variable Data type Description
main() - -
i int Loop variable to store
and check a number
j int Inner loop variable
to check if a number
is
prime or not
c int Counting variable
m int[] Variable of an array to
store 20 numbers
Q3) Write a program to accept 10 different numbers in a
Single Dimensional Array (SDA). Now, enter a number
and by using binary search technique, check whether or
not the number is present in the list of array elements. If
the number is present
then display the message “Search successful”
otherwise, display “Search unsuccessful”.
//program to check a
number import java.util.*;
m[i]= in.nextInt();
p=(ub+lb)/
2;
if(m[p]<ns)
{
lb=p+1;
if(m[p]>ns)
ub=p-1;
if(m[p]==ns)
k=1;
break;
if(k==1)
else
for(j=0;j<4;j++)
m[i][j]=
in.nextInt();
}
for(j=0;j<4;j++)
{
System.out.print(m[i][j]+" ");
}
System.out.println();
}
for(i=0;i<4;i++)
ld= ld+m[i][i];
for(i=0;i<4;i++)
rd= rd+m[i]
[k]; k=k-1;
}
for(j=0;j<3;j++)
m[i][j]=in.nextInt();
}
System.out.println("The numbers of the matrix
are:"); for(i=0;i<3;i++)
for(j=0;j<3;j++)
System.out.print(m[i][j]+" ");
System.out.println();
for(i=0;i<3;i++)
for(j=0;j<3;j++)
if(m[i][j]!=m[j][i])
k=1;
if(k==0)
System.out.println("It is a Symmetric
Matix"); else
System.out.println("It is not a Symmetric Matrix");
}
}
private int
n; void
input()
void fact()
{
int f= 1;
for(int i=1;i<=n;i++)
f=f*i;
ob.fact();
Output:
Variable Name Data type Description
main() - -
n int Stores the number for
which the factorial is
to
be calculated
f int Stores the calculated
factorial
i int Loop variable to
iterate
from 1 to n while
calculating the
factorial
ob Factorial An object of the
factorial class used
to call the member
methods input and fact
for(int j=0;j<3;j++)
m[i][j]= in.nextInt();
void rowsum()
{
int r=0;
for(int j=0;j<3;j++)
r= r+m[i][j];
void colsum()
int c= 0;
for(int j=0;j<3;j++)
c= c+m[j][i];
}
System.out.println("The sum of the numbers of "+(i+1)+" column=
"
+c);
}
public static void main(String args[])
{
Matrix ob= new
Matrix(); ob.getdata();
ob.rowsum();
ob.colsum();
}
Output:
Variable Name Data type Description
main() - -
m int[][] Array of size 3
rows and 3
columns to accept
and store
numbers
i int Loop variable to
iterate through
the
rows of the matrix
j int Loop variable to
iterate through the
columns of the
matrix
r int To store the sum of
numbers of each row
c int To store the sum
of numbers of
each
column
ob Matrix Object of class Matrix
Q8) Write a program to input a number and check
and print whether it is a ‘Pronic’ number or not. Use a
method int Pronic(int n) to accept a number. The
method returns 1, if the number is Pronic, otherwise
zero (0).
//Program to check pronic
number import java.util.*;
public class Pronicc
int Pronic(int n)
int f=0;
for(int i=1;i<=(n-1);i++)
if(i*(i+1)==n)
f= 1;
break;
return (f);
{
Scanner in= new Scanner(System.in);
System.out.println("Enter your
number"); int m= in.nextInt();
Pronicc ob= new
Pronicc(); int a;
a=
ob.Pronic(m);
if(a==1)
System.out.println("1");
else
System.out.println("0");
}
}
Output:
Variable Name Data type Description
main() - -
f int Flag variable
i int Loop variable to
iterate through 1
and the entered
number
n int Accepts the number
from main function
m int To accept number
from user
a int Variable to call
method Pronic
ob Pronicc Object of class Pronic
to call function
{
void calculate(int base,int ht)
a=
in.nextInt();
b=
in.nextInt();
h=
in.nextInt();
Area ob= new
Area();
ob.calculate(base,ht
);
ob.calculate(d1,d2);
ob.calculate(a,b,h);
}
Output:
Member methods:
1) A parameterized conductor to initialize the
data members
2) To accept the details (name of customer and price)
3) To compute the discount
4) To display the name, discount and amount to be
paid after discount.
Write a main method to create an object of the
class and call the member methods.
//Program to compute discounts on
laptop import java.util.*;
String name;
double price;
double dis= 0.0;
double amt= 0.0;
Laptop(String n,double
pr)
{
name= n;
price= pr;
void accept()
System.out.println("Customer name:
"+name); System.out.println("Price:
"+price);
void compute()
if(price<=25000)
dis= price*5.0/100.0;
if(price>=25001&&price<=5000
0) dis= price*7.5/100.0;
if(price>=50001&&price<=1000
00) dis= price*10.0/100.0;
if(price>100000)
dis= price*15.0/100.0;
}
void display()
amt= price-dis;
System.out.println(" Bill
");
System.out.println("Customer
name:"+name);
System.out.println("Discount on
laptop:"+dis);
System.out.println("Amount:"+amt);
}
prc= in.nextDouble();
Output :
String
str; int p;
Arrange(String word)
str = word;
p = str.length();
}
str = word;
p = str.length();
void arrangeAndDisplayAlphabets()
char[] arr =
str.toCharArray(); for (int i =
0; i < arr.length; i++)
char temp =
arr[i]; arr[i] =
arr[j]; arr[j] =
temp;
}
System.out.println("Arranged word: " + new String(arr));
}
Scanner in = new
Scanner(System.in);
System.out.println("Enter a word:
"); String word = in.nextLine();
Arrange ob = new
Arrange(word);
ob.arrangeAndDisplayAlphabets
();
}
Output :
Variable name Data type Description
main() - -
str String Stores word entered
by user
p int Stores length of word
arr char[] Array of characters
i int Loop variable
for iterating
through
character array
j int Loop variable
for iterating
through
character array
temp char Temporary
variable used for
swapping
characters
word String Accepts the word
input by user
ob Arrange Object of
class Arrange
to call
functions
Q12) Write a program in Java to generate the
following pattern:
A
AB
AB
C
ABCD
ABCD
E
//program to generate a
pattern public class Pattern1
{
int i,j;
for(i=65;i<=69;i+
+)
{
for(j=65;j<=i;j++)
System.out.print((char)j);
System.out.println();
}
Output:
int i,j;
for(i=65;i<=69;i+
+)
for(j=65;j<=i;j++)
System.out.print((char)i);
System.out.println();
Output:
Variable name Data type Description
main() - -
i int To iterate through
range of rows
j int To iterate through
range of columns
int i,j;
System.out.println("Enter a character in
Uppercase"); cht= in.next().charAt(0);
int n= (int)cht;
for(i=n+1;i<n+11;i+
+)
{
System.out.println((char)i);
}
}
Output:
System.out.println("Enter string:");
st= in.nextLine();
p= st.length();
for(a=0;a<p;a+
+)
ch= st.charAt(a);
if(ch>='a'&&ch<='z
') lc= lc+1;
else
if(ch>='A'&&ch<='Z')
up= up+1;
else
if(ch>='0'&&ch<='9')
d= d+1;
else
spl= spl+1;
System.out.println("The Output:");
Output:
st=' '+st;
p= st.lastIndexOf('
'); sn=
st.substring(p);
for(i=0;i<p;i++)
{
ch= st.charAt(i);
if(ch==' ')
st1= st1+st.charAt(i+1)+'.';
}
st2= st1+sn;
System.out.println("Initials with
surname:"); System.out.println(st2);
Output:
class Unique_String
for(j=i+1;j<p;j++)
if(str.charAt(i)==str.charAt
(j)) k=0;
if(k==1)
else
}
}
}
Output:
class Name_Telephone
Scanner in = new
Scanner(System.in); int i, a = 0, k =
0;
String b = "";
String[] m = new
String[20]; Long[] n =
new Long[20]; for (i = 0; i
< 20; i++)
in.nextLine();
if (m[i].equalsIgnoreCase(name))
k = 1;
a = i;
b=
m[i];
break;
}
if (k == 1)
} else
{
System.out.println(name + " is not found at any location.");
}
}
Output:
Variable name Data type Description
main() - -
i int Loop variable to
accept 20 names
and
telephone numbers
a int Copy variable for
printing the
name
and number
k int Flag variable to
detect the
name
entered
b String To interchange the
names in loop
m String[] Array to store 20
names
n Long[] Array to store 20
telephone numbers
class Salary
String
name,add,sub; long
ph;
double
sal,tax; void
accept()
Scanner in = new
Scanner(System.in);
System.out.println("Enter name ");
name = in.nextLine();
System.out.println("Enter Address
"); add = in.next();
System.out.println("Enter the Phone Number
"); ph = in.nextLong();
System.out.println("Enter Subject Specialization
"); sub = in.next();
System.out.println("Enter the monthly Salary
"); sal = in.nextDouble();
}
void calculate()
{
if(12*sal>175000)
tax=((sal*12)-
175000)*5/100;
else
tax=0;
}
void display()
System.out.println("Name : "+name);
System.out.println("Address : "+add);
System.out.println("Phone Number : "+ph);
System.out.println("Subject
Specialization ;"+sub);
System.out.println("Monthly Salary :Rs." +sal);
System.out.println("Income Tax :Rs." +tax);
}
Salary ob=new
Salary(); ob.accept();
ob.calculate();
ob.display();
}
}
Output:
String
name; int
ut;
double amt=0.0,scg=0;
Scanner in = new
Scanner(System.in); void accept()
void calculate()
if(ut<=100)
amt=
ut*2.0;
if(ut>100&& ut<=300)
amt= 200+(ut-
100)*3.0;
if(ut>300)
scg= ut*2.5/100.0;
amt= scg+(200+600+(ut-300)*5.0);
}
}
void print()
ElectricityBill ob = new
ElectricityBill(); ob.accept();
ob.calculate()
; ob.print();
}
}
Output: