Wap To input numbers into a array of 3 rows And 3 coloumns . find the sum of the numbers in the diagonal elements . no element should be repeated twice
Evergreen Publication Model paper 1 Q8
import java.io.*;
class SumDiagonal
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int a[][]=new int[3][3];
int Sum=0;
System.out.println(" Enter 6 elements for array one :");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
a[i][j]=Integer.parseInt(br.readLine());
}
}
System.out.println(" Matrix ");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(i==j)
Sum = Sum + a[i][j];
if(i+j==2&&i!=j)
Sum = Sum + a[i][j];
}
}
System.out.println("Sum of Diagonal = "+Sum);
}
}
Showing posts with label 2D Array. Show all posts
Showing posts with label 2D Array. Show all posts
Wednesday, January 16, 2008
[2D array]Sum of Even and Odd
WAP a program to find the product of all the even elements and all the odd elements present in the array
//Wap to Store 12 elements in a array of size 3X4 and find the sum of all the elements
//and product of even elements and odd elements.
import java.io.*;
class EvenOdd
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int a[][]=new int[3][4];
int s=0,even=1,odd=1;
System.out.println(" Enter 12 elements for array one :");
for(int i=0;i<3;i++)
{
for(int j=0;j<4;j++)
{
a[i][j]=Integer.parseInt(br.readLine());
s=s+a[i][j];
if(a[i][j]%2==0)
even=even*a[i][j];
else
odd=odd*a[i][j];
}
}
System.out.println(" Sum = " +s);
System.out.println(" Product of Even = " + even );
System.out.println(" Product of odd = " + odd);
}
}
//Wap to Store 12 elements in a array of size 3X4 and find the sum of all the elements
//and product of even elements and odd elements.
import java.io.*;
class EvenOdd
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int a[][]=new int[3][4];
int s=0,even=1,odd=1;
System.out.println(" Enter 12 elements for array one :");
for(int i=0;i<3;i++)
{
for(int j=0;j<4;j++)
{
a[i][j]=Integer.parseInt(br.readLine());
s=s+a[i][j];
if(a[i][j]%2==0)
even=even*a[i][j];
else
odd=odd*a[i][j];
}
}
System.out.println(" Sum = " +s);
System.out.println(" Product of Even = " + even );
System.out.println(" Product of odd = " + odd);
}
}
Subscribe to:
Posts (Atom)