You are on page 1of 9

Subject Name : OBC 520 Java & Unix Programming Lab

Enrollment Number : A1922214003(el)


Roll Number : A1922214003(el)
Student Name : NAVEEN NAGALINAM
Semester : VI

INSTRUCTIONS

a) All answer script is to be completed as typed in word/pdf.


b) Questions are required to be attempted as mentioned in the Question paper.
c) Answer script is to be completed by due date and need to be submitted for
evaluation by Amity University at addoepractical@amityonline.com
d) Fill this page and attach as the front page of your answer sheet and No answer
sheet would be accepted if this cover page would not be attached with the sheet.
END TERM PRACTICAL EXAMINATIONS, JUNE 2017
BACHELOR of COMPUTER APPLICATION
JAVA & UNIX LAB

TIME: 10.00 to 12.00 MAX MARKS: 70

Note: Choose one question from each section. 35x2

Section A

Q1. Write a program in JAVA to find the sum of the digit of the
given number.

vim sum.java

import java.util.Scanner;

public class sum

public static void main(String args[])

int a, b, sum =0;

Scanner s = new Scanner(System.in);

System.out.print("Enter the number:");

a = s.nextInt();

while(a > 0)

b = a % 10;

sum = sum + b;
a = a / 10;

System.out.println("Sum of given digits is " +sum);

OUTPUT:

PS C:\Users\Naveen> javac sum.java

PS C:\Users\Naveen> java sum

Enter the number:123456789

Sum of given digits is 45


Q2. Write a program in JAVA for addition of two M*N Matrix.

vim addMatrix.java

import java.util.Scanner;

class AddMatrix

public static void main(String args[])

int a, b, c, d;

Scanner in = new Scanner(System.in);

System.out.println("Enter the number of rows and columns of


matrix");

a = in.nextInt();

b = in.nextInt();

int first[][] = new int[a][b];

int second[][] = new int[a][b];

int sum[][] = new int[a][b];

System.out.println("Enter the elements of first matrix");

for ( c = 0 ; c < a ; c++ )

for ( d = 0 ; d < b ; d++ )

first[c][d] = in.nextInt();
System.out.println("Enter the elements of second matrix");

for ( c = 0 ; c < a ; c++ )

for ( d = 0 ; d < b ; d++ )

second[c][d] = in.nextInt();

for ( c = 0 ; c < a ; c++ )

for ( d = 0 ; d < b ; d++ )

sum[c][d] = first[c][d] + second[c][d];

System.out.println("Sum of entered matrices:");

for ( c = 0 ; c < a ; c++ )

for ( d = 0 ; d < b ; d++ )

System.out.print(sum[c][d]+"\t");

System.out.println();

OUTPUT:

C:\Users\Naveen>javac AddMatrix.java

C:\Users\Naveen>java AddMatrix
Enter the number of rows and columns of matrix

33

Enter the elements of first matrix

123123123

Enter the elements of second matrix

123123123

Sum of entered matrices:-

2 4 6

2 4 6

2 4 6
Section B:

Q3. Write a Shell script for addition of two given numbers.

vim sum.sh

echo "Enter two numbers"

read x y

sum=$((x + y))

echo "The sum is=$sum"

OUTPUT:

root@DESKTOP-QMAQVBR:~# sh sum.sh

Enter two numbers

37 91

The sum is=128


Q4. Write a Shell script for printing the table of given number.

Dear ExaminerI have interpreted the word table in the


question as multiplication tables.

vim x.sh

echo "Enter a Number"

read n

i=0

while [ $i -le 10 ]

do

echo " $n x $i = `expr $n \* $i`"

i=`expr $i + 1`

done

OUTPUT:

root@DESKTOP-QMAQVBR:~# sh x.sh

Enter a Number

5x0=0

5x1=5

5 x 2 = 10

5 x 3 = 15
5 x 4 = 20

5 x 5 = 25

5 x 6 = 30

5 x 7 = 35

5 x 8 = 40

5 x 9 = 45

5 x 10 = 50

You might also like