Computer
Computer
YEAR - 2019-20
2 check()
3 area()
4 Railway Ticket
5 Income Tax
6 print()
7 series(0
8 Arrange Letters
9 mobike
10 Electricity
11 to arrange characters
12 word from first letter
13 patterns
14 to find number of double
letters
15 start with vowel and end with
consonant
16 start with capital letter and
end with small letter
17 Narcissistic number
18 Amicable and twisted prime
number
19 to remove zero from number
20 emirp number
21 patterns
22 to find frequency of digits
23 Automorphic number and
smallest digit in number
24 array program
25 binary search
26 bubble sort
27 selection sort
28 double dimensional array(sum
of all elements)
29 double dimensional array(sum
of rows and columns)
30 double dimensional array(sum
of left and right diagonal)
Bonafide Certificate
This is to certify that all Java Programs written in this book has been
performed by the student satisfactorily.
Name- Name-
Date- Date-
Signature Signature
PROGRAM 1
Design a class to overload a function polygon() as follows:
(i) void polygon(int n, char ch) : with one integer argument and one character type argument
that draws a filled square of side n using the character stored in ch.
(ii) void polygon(int x, int y) : with two integer arguments that draws a filled rectangle of length
x and breadth y, using the symbol ‘@’
(iii) void polygon() : with no argument that draws a filled triangle shown below:
*
**
***
class program01
{
void polygon(int n, char ch)
{
for (int i=1;i<=4;i++)
{
for (int j=1;j<=4;j++)
{
System.out.print(ch);
}
System.out.println("");
}
}
void polygon(int x, int y)
{
for (int i=1;i<=x;i++)
{
for (int j=1;j<=y;j++)
{
System.out.print("@");//to print
}
System.out.println("");
}
}
void polygon()
{
for (int i=1;i<=3;i++)
{
for (int j=1;j<=i;j++)
{
System.out.print("*");
}
System.out.println("");
}
}
public static void main()
{
program01 obj=new program01();//to create an object of the class
obj.polygon();
obj.polygon(8,10);
obj.polygon(6,'&');
}
}
Output
Variable Description
PROGRAM 2
Design a class to overload a function check() as follows:
(i) void check(String str, char ch) – to find and print the frequency of a character in a string.
Example: Input : str = “success” ch=’s’
Output : number of s present is =3
(ii) void check(String s1) – to display only vowels from string s1, after converting it to lower
case.
Example: Input : “computer”
Output : oue
(iii) void check(String s1, char ch1, char ch2) – display string s1 after converting ch1
character with ch2.
Example: Input : “Application” ch1=’p’ ch2=’*’
Output : A**lication
class program02
{
void check (String str, char ch)
{
int ctr=0;
for (int i=0;i<str.length();i++)
{
char ch1=str.charAt(i);
if (ch1==ch)
ctr++;
}
System.out.println("Number of "+ch+" present is = "+ctr);
}
void check(String s1)
{
s1=s1.toLowerCase();//coverts the string to lower case
for (int i=0;i<s1.length();i++)
{
char ch=s1.charAt(i);
if (ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
System.out.print(ch);
}
System.out.println();
}
void check(String s1, char ch1, char ch2)
{
System.out.println(s1.replace(ch1,ch2));
}
public static void main()
{
program02 obj=new program02();//to create an object of the class
obj.check("success",'s');
obj.check("COMPUTER");
obj.check("Application",'p','*');
}
}
OUTPUT
Variable Description
where s =
ii. double area(int a, int b, int height) with three integer arguments, returns the area
area =
class program03
{
double area(double a, double b, double c)
{
double s=(a+b+c)/3;
double area=Math.sqrt(s*(s-a)*(s-b)*(s-c));
return area;
}
double area(int a, int b, int height)
{
double area=0.5*height*(a+b);
return area;
}
double area(double diagonal1, double diagonal2)
{
double area=0.5*(diagonal1*diagonal2);//to calculate the area
return area;
}
public void main()
{
program03 obj=new program03();//to create an object of the class
System.out.println("The Area is : "+area(3.0,4.0,5.0));
System.out.println("The Area is : "+area(4,6,10));
System.out.println("The Area is : "+area(8.0,10.0));
}
}
OUTPUT
Variable Description
import java.util.*;
class RailwayTicket
{
String name="";
String coach="";
long mobno=0;
int amt=0;
int totalamt=0;
Scanner sc=new Scanner (System.in);
void accept()
{
System.out.println("Enter your Name:");
name =sc.nextLine();
System.out.println("Enter your Coach:");
coach =sc.next();
System.out.println("Enter your Mobile Number:");
mobno=sc.nextLong();
System.out.println("Enter the Basic Amount of Ticket:");
amt=sc.nextInt();
}
void update()
{
System.out.println("Enter 1 to select First AC:");
System.out.println("Enter 2 to select Second AC:");
System.out.println("Enter 3 to select Third AC:");
System.out.println("Enter 4 to select Sleeper:");
System.out.println("Enter your choice:");//to accept user’s choice
int coach =sc.nextInt();
if (coach==1)
totalamt=amt+700;
if (coach==12)
totalamt=amt+500;
if (coach==3)
totalamt=amt+250;
if (coach==4)
totalamt=amt;
}
void display()
{
System.out.println("Name : "+name);//to print the name
System.out.println("Coach : "+coach);
System.out.println("Total Amount : Rs"+totalamt);
System.out.println("Mobile Number : "+mobno);
}
public static void main()
{
RailwayTicket obj=new RailwayTicket();//to create an object of the class
obj.accept();
obj.update();
obj.display();
}
}
OUTPUT
Variable Description
Variable Description
import java.util.*;
class program06
{
void Print(String str)
{
String w=””;
for (int i=0;i<str.length();i++)
{
char ch=str.charAt(i);
if (ch!=’ ’)
w=w+ch;
str=str.replace(‘ch’,’ ’);
}
System.out.println(“Word after removing all duplicate characters:”+w);
}
void Print(char c , int n)
{
for (int i=0;i<n;i++)
System.out.print(c);
System.out.println();
}
void Print(int n)
{
int num=0;
for (int i=0;i<n;i++)
{
num=num*10+1;to calculate the number
System.out.print(num+",");
}
}
public static void main()
{
program06 obj=new program06();//to create an object of the class
obj.Print("Missippi");
obj.Print('@',10);
obj.Print(10);
}
}
OUTPUT
Variable Description
class program07
{
int series(int n)
{
int fact=1;
for (int i=1;i<n;i++)
fact = fact*i;
return fact;
}
double series(int x, int a)
{
double sum=0;
for (int i=1;i<=a;i++)
sum=sum+(Math.pow(x,a)/a);
return sum;
}
double series(double a, double n)
{
double sum=1.0;
double no=4.0;
for (double i=1;i<n;i++)
{
sum=sum+(n/Math.pow(a,n-2));//to calculate sum
no=no+3.0;
}
return sum;
}
public void main()
{
program07 obj=new program07();//to create an object of the class
System.out.println("The Sum of Series is : "+series(6));
System.out.println("The Sum of Series is : "+series(3,4));
System.out.println("The Sum of Series is : "+series(4.0,3.0));
}
}
OUTPUT
Variable Description
import java.util.*;
class ArrangeLetters
{
public static void main()
{
Scanner sc = new Scanner (System.in);
System.out.println("Enter the Word :");
String word=sc.next();
System.out.println("Original Word : "+word);
word=word.toUpperCase();
System.out.println("Words in Capitals : "+word);
String w="";
for (int i=1;i<=200;i++)
{
char ch=(char)i;
for (int j=0;j<word.length();j++)
{
char ch1=word.charAt(j);//to extract the character at that position
if (ch==ch1)
w=w+ch;
}
}
System.out.println("Words after sorting : "+w);//to print
}
}
OUTPUT
Variable Description
import java.util.*;
class mobike
{
int bno,phno,days,charge;
String name;
void input()
{
Scanner sc = new Scanner (System.in);
System.out.println("Enter Name : ");too accept name
name=sc.nextLine();
System.out.println("Enter Phone Number : ");
phno=sc.nextInt();
System.out.println("Enter Bike Number : ");
bno=sc.nextInt();
System.out.println("Enter Number of Days the Bike is Taken on rent : ");
days=sc.nextInt();
}
void computer()
{
if (days<=5)
charge=500*days;
if (days>5 && days<=10)
charge=(500*5)+((days-5)*400);//to calculate charge
if (days>10)
charge=(500*5)+(400*5)+((days-10)*200);
}
void display()
{
System.out.println("Bike No. Phone No. No. of Days Charge");
System.out.println(bno+" "+phno+" "+days+" "+charge);
}
public static void main()
{
mobike obj=new mobike();//to create an object of the class
obj.input();
obj.computer();
obj.display();
}
}
OUTPUT
Variable Description
import java.util.*;
class Electricity
{
int omr,nmr,cr,rent;//local variables
double cost, sc;
Electricity()//constructor
{
omr=0;
nmr=0;
cr=0;
rent=0;
cost=0.0d;
sc=0.0d;
}
void input()
{
Scanner sa =new Scanner (System.in);
System.out.println("Enter Old Meter Reading : ");//to accept old meter readings
omr=sa.nextInt();
System.out.println("Enter New Meter Reading : ");
nmr=sa.nextInt();
}
void calculate()
{
cr=nmr-omr;
if (cr<=100)
cost=0.80*cr;
if (cr>100 && cr<=250)
cost=80+((cr-100)*1.20);
if (cr>250)
cost=80+180+((cr-250)*2.00);
cost=cost+200;
sc=0.05*cost;
}
void display()
{
System.out.println("Total Billing Cost : "+cost);
System.out.println("Surcharge : "+sc);
System.out.println("Net Cost : "+(cost+sc));
}
public static void main()
{
Electricity obj=new Electricity();//to create an object of the class
obj.input();
obj.calculate();
obj.display();
}
}
OUTPUT
Variable Description
6. double sc Surcharge
PROGRAM 11
WAP to accept the String and print all capital letters followed by small letter and followed by
digits. If
Input: MuMbAi-53
Output : MMAubi53
import java.util.*;
class program11
{
public static void main()
{
Scanner sc = new Scanner (System.in);
System.out.println("Enter Sentence : ");
String sentence=sc.nextLine();
for (int i=0;i<sentence.length();i++)
{
char ch=sentence.charAt(i);
if (Character.isUpperCase(ch))
System.out.print(ch);
}
for (int i=0;i<sentence.length();i++)
{
char ch=sentence.charAt(i);
if (Character.isLowerCase(ch))
System.out.print(ch);//to print the word
}
for (int i=0;i<sentence.length();i++)
{
char ch=sentence.charAt(i);
if (Character.isDigit(ch))//to check if the character entered is digit
System.out.print(ch);
}
}
}
OUTPUT
Variable Description
import java.util.*;
class program12
{
public static void main()
{
Scanner sc = new Scanner (System.in);
System.out.println("Enter Sentence : ");
String sent=sc.nextLine();
sent=" "+sent;
String word="";
for (int i=0;i<sent.length()-1;i++)
{
if (sent.charAt(i)==' ')
word=word+sent.charAt(i+1);
}
word=word.toUpperCase();//to convert the word to upper case
System.out.println(word);//to print the word
}
}
OUTPUT
Variable Description
import java.util.*;
class program13
{
public static void main()
{
Scanner sc= new Scanner (System.in);
System.out.println("1. Number Pattern");
System.out.println("2. Letter Pattern");
System.out.println("Enter choice");
int ch=sc.nextInt();//to accept user’s choice
switch (ch)
{
case 1:
{
for (int i=1;i<=5;i++)
{
for (int j=1;j<=i;j++)
{
if (j%2!=0)
System.out.print("1");
else
System.out.print("0");
}
System.out.println("");
}
}
break;
case 2:
int a=65;
{
for (int i=1;i<=5;i++)
{
for (int j=1;j<=i;j++)
{
System.out.print((char)a);
a++;//to increament
}
System.out.println("");
}
}
break;
default:System.out.println("Wrong Choice");//to print wrong choice if choice is wrong
}
}
}
OUTPUT
Variable Description
PROGRAM 14
Write a program to accept a string. Convert it to uppercase. Count and output the number of
double letter that exist in the string.
Sample Input: “SHE WAS FEEDING THE LITTLE RABBIT WITH AN APPLE”
Sample Output: 4
import java.util.*;
class program14
{
public static void main()
{
Scanner sc = new Scanner (System.in);
System.out.println("Enter Sentence : ");
String sent=sc.nextLine();
sent=sent.toUpperCase();
int ctr=0;
for (int i=0;i<sent.length()-1;i++)
{
if (sent.charAt(i)==sent.charAt(i+1))
ctr++;
}
System.out.println(ctr);
}
}
OUTPUT
Variable Description
PROGRAM 15
Write a program to accept a sentence. Print all the words that start with a vowel and end with a
consonant.
For example : Input :“the purpose of education is to replace an empty mind with an open one”
Output : of
education
is
an
empty
an
open
import java.util.*;
class program15
{
public static void main()
{
Scanner sc=new Scanner (System.in);
System.out.println("Enter Sentence : ");
String sent=sc.nextLine();//to Accept the sentence
String w="";
sent=sent+" ";
for (int i=0;i<sent.length();i++)
{
char ch=sent.charAt(i);
if (ch==' ')
{
char c1=w.charAt(0);
char c2=w.charAt(w.length()-1);
if((c1=='A'||c1=='E'||c1=='I'||c1=='O'||c1=='U'||c1=='a'||c1=='e'||c1=='i'||c1=='o'||
c1=='u') && (c2!='A'||c2!='E'||c2!='I'||c2!='O'||c2!='U'||c2!='a'||c2!='e'||c2!='i'||c2!='o'||c2!
='u'))
System.out.println(w);
w="";
}
else
w=w+ch;//to store the word
}
}
}
OUTPUT
Variable Description
import java.util.*;
class program16
{
public static void main()
{
Scanner sc=new Scanner (System.in);
System.out.println("Enter Sentence : ");
String sent=sc.nextLine();//to accept the sentence
String w="";
sent=sent+" ";
for (int i=0;i<sent.length();i++)
{
char ch=sent.charAt(i);
if (ch==' ')
{
char c1=w.charAt(0);
char c2=w.charAt(w.length()-1);
if (Character.isUpperCase(c1) && Character.isLowerCase(c2))
System.out.print(w+" ");
w="";
}
else
w=w+ch;//to store the word
}
}
}
OUTPUT
Variable Description
import java.util.*;
class program17
{
public static void main()
{
Scanner sc = new Scanner (System.in);
System.out.println("Enter Number : ");
int n=sc.nextInt();//to accept the number from user
int copy=n;
int ctr=0;
while (copy>0)
{
copy=copy/10;
ctr++;
}
copy=n;
double sum=0.0d;
while (n>0)
{
int rem=n%10;
n=n/10;
sum=sum+Math.pow(rem,ctr);
}
if (copy==sum)
System.out.println("It is a Narcissistic Number");
else
System.out.println("It is Not a Narcissistic Number");//to print if the condition is false
}
}
OUTPUT
Variable Description
import java.util.*;
class program18
{
public static void main()
{
Scanner sc=new Scanner (System.in);
System.out.println("1. Amicable number");
System.out.println("2. Twisted Prime Number");
System.out.println("Enter Choice");
int ch=sc.nextInt();
switch (ch)
{
case 1:
{
System.out.println("Enter First Number : ");
int n1=sc.nextInt();//to accept the number
System.out.println("Enter Second Number : ");
int n2=sc.nextInt();
int sum1=0;
int sum2=0;
for (int i=1;i<n1;i++)
{
if (n1%i==0)
sum1=sum1+i;
}
for (int i=1;i<n2;i++)
{
if (n2%i==0)
sum2=sum2+i;
}
if (sum1==n2 && sum2==n1)
System.out.println("They are Amicable number");
else
System.out.println("They are Not Amicable number");
}
break;
case 2:
{
System.out.println("Enter Number:");
int n=sc.nextInt();
int ctr=0,rev=0,ctr2=0;
for (int i=1;i<=n;i++)
{
if (n%i==0)
ctr++;
}
while (n>0)
{
int rem=n%10;
n=n/10;
rev=rev*10+rem;//to reverse the number
}
for (int i=1;i<=rev;i++)
{
if (rev%i==0)
ctr2++;
}
if (ctr==2&&ctr2==2)
System.out.println("It is an Twisted Prime Number");
else
System.out.println("It is Not a Twisted Prime Number");
}
break;
default:System.out.println("Wrong Choice");
}
}
}
OUTPUT
Variable Description
import java.util.*;
class program19
{
public static void main()
{
Scanner sc = new Scanner (System.in);
System.out.println("Enter Number");
int n=sc.nextInt();//to accept the number
int n1=0;
while (n>0)
{
int rem=n%10;
n=n/10;
if (rem!=0)
n1=n1*10+rem;
}
while (n1>0)
{
int rem=n1%10;
n1=n1/10;//to extract the number’s digits
if (rem!=0)
n=n*10+rem;
}
System.out.println("The Number is : "+n);
}
}
OUTPUT
Variable Description
import java.util.*;
class Emrip
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Number:");
int n=sc.nextInt();//to accept the number
int ctr=0,rev=0,ctr2=0;
for (int i=1;i<=n;i++)
{
if (n%i==0)
ctr++;
}
while (n>0)
{
int rem=n%10;
n=n/10;
rev=rev*10+rem;//to reverse the number
}
for (int i=1;i<=rev;i++)
{
if (rev%i==0)
ctr2++;
}
if (ctr==2&&ctr2==2)
System.out.println("It is an Emrip Number");
else
System.out.println("It is Not a Emrip Number");
}
}
OUTPUT
Variable Description
import java.util.*;
class program21
{
public static void main()
{
Scanner sc= new Scanner (System.in);
System.out.println("1. Special Characters Pattern");
System.out.println("2. Number Pattern");
System.out.println("Enter choice");
int ch=sc.nextInt();//to accept the choice
switch (ch)
{
case 1:
{
for (int i=1;i<=5;i++)
{
for (int j=1;j<=i;j++)
{
if (j%2!=0)
System.out.print("*");
else
System.out.print("#");
}
System.out.println("");
}
}
break;
case 2:
{
for (int i=1;i<=5;i++)
{
for (int j=5;j>=i;j--)
{
System.out.print(j);
}
System.out.println("");
}
}
break;
default:System.out.println("Wrong Choice");");//to print wrong choice if choice is wrong
}
}
}
OUTPUT
Variable Description
import java.util.*;
class program22
{
public static void main()
{
Scanner sc= new Scanner (System.in);
System.out.println("Enter Number : ");
int n=sc.nextInt();//to accept the number
System.out.println("Digit Frequency");
System.out.println("===================");
for (int i=0;i<=9;i++)
{
int ctr=0;
int copy=n;
while (n>0)
{
int rem=n%10;
n=n/10;
if (i==rem)
ctr++;
}
if (ctr>0)
System.out.println(i+" "+ctr);
n=copy;//to create a copy of the number
}
}
}
OUTPUT
Variable Description
import java.util.*;
class program23
{
public static void main()
{
Scanner sc= new Scanner (System.in);
System.out.println("1. Automorphic Number");
System.out.println("2. Smallest Digit of an Integer");
System.out.println("Enter choice");
int ch=sc.nextInt();//to accept the number
System.out.println("Enter Number");
int n=sc.nextInt();
switch (ch)
{
case 1:
{
int rem=(n*n)%100;
if (n==rem)
System.out.println("It is an Automorphic Number");
else
System.out.println("It is Not an Automorphic Number");
}
break;
case 2:
{
int min=9;
while (n>0)
{
int rem=n%10;
n=n/10;
if (rem<min)
min=rem;
}
System.out.println("Smallest Digit is : "+min);
}
break;
default:System.out.println("Wrong Choice");//to print wrong choice if choice is wrong
}
}
}
OUTPUT
Variable Description
import java.util.*;
class program24
{
public static void main()
{
Scanner sc=new Scanner (System.in);
String con[]=new String[10];
String pac[]=new String[10];
for (int i=0;i<con.length;i++)
{
System.out.println("Enter Name of the Country : ");
con[i]=sc.nextLine();//to accept the name of country
System.out.println("Enter the Name of Famous Place : ");
pac[i]=sc.nextLine();
}
System.out.println("Enter the Name of Country you want to search : ");
String ser=sc.nextLine();
int pos=-1;
for (int i=0;i<con.length;i++)
{
if (ser.equalsIgnoreCase(con[i]))
pos=i;
}
if (pos!=-1)
{
System.out.println("Search Successful ");
System.out.println("Name of the Country : "+con[pos]);//to print the name of country
System.out.println("Name of Famous Place : "+pac[pos]);
}
else
System.out.println("Search Unsuccessful ");
}
}
OUTPUT
Variable Description
import java.util.*;
class program25
{
public static void main()
{
Scanner sc=new Scanner (System.in);
int year[]={1982, 1987, 1993, 1996, 1999, 2003, 2006, 2007, 2009, 2010};
System.out.println("Enter the year of graduation from school : ");
int target=sc.nextInt();//to accept year to be searched
int left=0;
int right=year.length-1;
int result=-1;
while (left<=right)
{
int middle=(left+right)/2;
if (year[middle]==target)
{
result=middle;
break;
}
else if (year[middle]>target)
{
right=middle-1;
}
else
{
left=middle+1;
}
}
if (result!=-1)
System.out.println("Record exists");//to print if the year is found
else
System.out.println("Record does not exists");
}
}
OUTPUT
Variable Description
import java.util.*;
class program26
{
public static void main()
{
Scanner sc=new Scanner (System.in);
String name[]=new String[10];
String temp="";
for (int i=0;i<name.length;i++)
{
System.out.println("Enter Name of the Student : ");
name[i]=sc.nextLine();//to accept name
}
for (int i=0;i<name.length;i++)
{
for (int j=0;j<name.length-1;j++)
{
if (name[j].compareTo(name[j+1])>0)
{
temp=name[j];
name[j]=name[j+1];
name[j+1]=temp;
}
}
}
System.out.println("Names of the Students in alphabetical order is : ");
for (int i=0;i<name.length;i++)
{
System.out.println(name[i]);//to print name
}
}
}
OUTPUT
Variable Description
Sr. No. Variable Type Variable Name Variable Description
import java.util.*;
class program27
{
public static void main()
{
Scanner sc=new Scanner (System.in);
int n[]=new int[35];
int temp,max,pos;
for (int i=0;i<n.length;i++)
{
System.out.println("Enter Integer : ");
n[i]=sc.nextInt();//to accept an integer
}
for (int i=0;i<n.length;i++)
{
max=n[i];
pos=i;
for (int j=i+1;j<n.length;j++)
{
if (n[j]>max)
{
max=n[j];//to find the maximum if the array
pos=j;
}
}
temp=n[i];
n[i]=n[pos];
n[pos]=temp;
}
System.out.println("Array of Integer in decending order is : ");//to print the array
for (int i=0;i<n.length;i++)
{
System.out.println(n[i]);//to print the array
}
}
}
OUTPUT
Variable Description
Sr. No. Variable Type Variable Name Variable Description
PROGRAM 28
Write a program to accept integer elements in a double dimensional array of size 4x4 and find
the sum of the elements.
7345
5461
6942
3275
Sum of the elements is :73
import java.util.*;
class sum
{
static void main()//to create main method
{
Scanner sc=new Scanner(System.in);
int a[][]=new int[4][4];
int sum=0;//a variable to store the sum of all elements
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a.length;j++)
{
System.out.println("ENTER AN INTEGER");
a[i][j]=sc.nextInt();//accepting the elements
sum+=a[i][j];//calculate the sum
}
}
System.out.println();
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a.length;j++)
{
System.out.print(a[i][j]+" ");//to print the array in matrix form
}
System.out.println();
}
System.out.println();
System.out.println("THE SUM OF ALL ELEMENTS IS :"+sum);//to print the sum
}
}
VARIABLE DESCRIPTION
PROGRAM 29
Write a program to accept integer elements in a double dimensional array of size 3x4 and find
the sum of the each row and each column elements.
Example:
Input :
7345
5461
6942
Output :
Sum of row 1 is : 19
Sum of row 2 is : 16
Sum of row 3 is : 21
Sum of column 1 is : 18
Sum of column 2 is : 16
Sum of column 3 is : 14
Sum of column 4 is : 8
import java.util.*;
class row_column
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int a[][]=new int[3][4];//to declare an array of 3 x 4
int s1=0;
int s2=0;
for(int i=0;i<3;i++)
{
for(int j=0;j<4;j++)
{
System.out.println("ENTER AN INTEGER");//to accept the elements of array
a[i][j]=sc.nextInt();
}
}
for(int i=0;i<3;i++)
{
for(int j=0;j<4;j++)
{
System.out.print(a[i][j]+" ");//to print the array
}
System.out.println();
}
for(int i=0;i<3;i++)
{
for(int j=0;j<4;j++)
{
s1=s1+a[i][j];//to calculate the sum of numbers in a row
}
System.out.println("SUM OF ROW "+(i+1)+" IS :"+s1);
s1=0;
}
System.out.println();
for(int i=0;i<4;i++)
{
for(int j=0;j<3;j++)
{
s2=s2+a[j][i];//to calculate the sum of numbers in a column
}
System.out.println("SUM OF COLUMN "+(i+1)+" IS :"+s2);
s2=0;
}
}
}
VARIABLE DESCRIPTION
PROGRAM 30
WAP in java to find out the sum of the two main(Left and Right) diagonals of the given multi-
dimensional array x[][]={{6,9,0,8},{3,2,1,5},{2,9,0,1},{4,1,9,6}}.
class diagonal
{
public static void main()
{
int x[][]={{6,9,0,8},{3,2,1,5},{2,9,0,1},{4,1,9,6}};//to declare a 2D array;
int s3=0;
int s4=0;
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
System.out.print(x[i][j]+" ");//to print the array
}
System.out.println();
}
for(int i=0;i<4;i++)
{
s3=s3+x[i][i];//to calculate the sum numbers in left diagonal
}
System.out.println("SUM OF LEFT DIAGONAL IS :"+s3);//to print the sum of left
diagonal
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
if((i+j)==3)
s4=s4+x[i][j];//to calculate the sum numbers in right diagonal
}
}
System.out.println();
System.out.println("SUM OF RIGHT DIAGONAL IS :"+s4);//to print the sum of right
diagonal
}
}
VARIABLE DESCRIPTION