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

10th Program

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

10th Program

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Q1) Write a program in Java to store 20 numbers (even and

odd) in a Single Dimensional Array (SDA). Calculate and


display the sum of all even numbers and all odd numbers
separately.
//program to store and calculate sum
import java.util.*;
public class sumSDA
{
public static void main(String args[])
{
Scanner in= new Scanner(System.in);
int evenSum= 0;
int oddSum= 0;
int[] num= new int[20];
System.out.println("Enter 20 numbers (even and odd):");
for(int i=0;i<20;i++)
{
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 to
store the numbers
i int Loop variable

Q2) Write a program in Java to store 20 numbers in a Single


Dimensional Array (SDA). Display the numbers which are
prime.
//program to display prime numbers
import java.util.*;
public class primeno
{
public static void main(String args[])
{
Scanner in= new Scanner(System.in);
int i,j,c;
int m[]= new int[20];
System.out.println("Enter numbers in the cell");
for(i=0;i<20;i++)
{
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 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.*;
public class binary
{
public static void main(String args[])
{
Scanner in= new Scanner(System.in);
int i,k=0,p=0,lb=0,ub=9,ns;
int m[]=new int[10];
System.out.println("Enter the numbers in ascending order");
for(i=0;i<10;i++)
{
m[i]= in.nextInt();
}
System.out.println("Enter the number to be searched:");
ns= in.nextInt();
while(lb<=ub)
{
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)
System.out.println("The search successful and the number is
present");
else
System.out.println("The search unsuccessful and the number is not
present");
}
}
Variable Data type Description
main() - -
i int Loop variable to iterate
through a range of 10
k int Flag variable to check if
number is present or
not
p int Stores the middle
element of the array
lb int Stores the first
element of the array
ub int Stores the last element
of the array
ns int To store the number
which is to be searched
m int[] Variable of an array to
store 10 numbers

Q4) Write a program in Java to create a 3*3 Square Matrix


and store numbers in it. The programmer wants to check
whether the Matrix is Symmetric or not. A Square Matrix is
said to be Symmetric if the element of the ith row and jth
column is equal to the element of the jth row and ith column.
//Program to check a matrix
import java.util.*;
public class sym
{
public static void main(String args[])
{
Scanner in= new Scanner(System.in);
int i,j,k;k=0;
int m[][]= new int[3][3];
int n[][]= new int[3][3];
System.out.println("Enter the numbers of the matrix:");
for(i=0;i<3;i++)
{
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");
}
}
Variable Data type Description
main() - -
i int Loop variable for rows
j int Loop variable for
columns
k int Flag variable to
indicate whether
matrix is symmetric or
not
m int[][] 2D array to store the
input matrix
n int[][] Unused 2D array

Q5) Write a program in Java to store the numbers in 4*4


matrix in a Double Dimensional Array. Find the sum of the
numbers of the left diagonal and the sum of the numbers of
the right diagonal of the matrix by using an input statement.
//program to store numbers
import java.util.*;
public class Diag
{
public static void main(String args[])
{
Scanner in= new Scanner(System.in);
int i,j,ld,rd,k;ld=0;rd=0;
int m[][]=new int[4][4];
System.out.println("Enter the numbers of the matrix");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
m[i][j]= in.nextInt();
}
System.out.println("The numbers of the matrix are:");
for(i=0;i<4;i++)
{
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];
}
System.out.println("The sum of the left diagonal elements="+ld);
k=3;
for(i=0;i<4;i++)
{
rd= rd+m[i][k];
k=k-1;
}
System.out.println("The sum of the right diagonal elements="+rd);
}
}
Variable Data type Description
main() - -
i int Loop variable for rows
in the matrix
j int Loop variable for
columns in the matrix
ld int To store sum of left
diagonal elements
rd int To store sum of right
diagonal elements
k int Counter for accessing
elements in the right
diagonal
m int[][] 2D array to store the
matrix

You might also like