0% found this document useful (0 votes)
86 views16 pages

BSC Program 3rd Year

This document contains a list of 57 students enrolled in the 5th semester of the Bsc. Physical Science (With Computer Science) program. It includes each student's college roll number, university roll number, and name. It also provides 8 Java programming problems and their solutions related to arrays, strings, matrices, prime numbers, Fibonacci series, and other mathematical operations.

Uploaded by

vikas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views16 pages

BSC Program 3rd Year

This document contains a list of 57 students enrolled in the 5th semester of the Bsc. Physical Science (With Computer Science) program. It includes each student's college roll number, university roll number, and name. It also provides 8 Java programming problems and their solutions related to arrays, strings, matrices, prime numbers, Fibonacci series, and other mathematical operations.

Uploaded by

vikas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

-Practical Assignment

Bsc. Physical Science


(With Computer Science)

Vth Semester
S.No. College Roll No. University Roll No. Names
1 55004 17003582099 VIVEK JOSHI

2 55005 17003582100 PANKAJ JANGRA

3 55008 17003582101 ANIRUDH TYAGI

4 55009 17003582102 ANOOP SHARMA

5 55013 17003582104 VARUN KUMAR

6 55017 17003582106 ASHWANI KANSAL

7 55021 17003582107 ASHISH CHATURVEDI

8 55023 17003582108 AJAY KUMAR PANDEY

9 55025 17003582109 VAIBHAV GUPTA

10 55026 17003582110 PRATEEK KUMAR TIWARI

11 55028 17003582111 SHUBHAM UPADHYAY

12 55029 17003582112 ADITYA

13 55030 17003582113 PAWAN KUMAR

14 55033 17003582114 RITIK SHARMA

15 55043 17003582116 SATENDRA SINGH

16 55044 17003582117 CHANDRA SHEKHAR AJAD YADAV

17 55045 17003582118 GULAB SINGH

18 55047 17003582119 AKSHAT GUPTA

19 55049 17003582120 DISHANK DAWRA

20 55051 17003582121 ANNU YADAV

21 55052 17003582122 KOMAL YADAV

22 55054 17003582123 RAMKIRPAL YADAV

23 55057 17003582125 MADHAV AVASTHI

24 55060 17003582127 SACHIN KUMAR

25 55062 17003582129 SACHIN JAJORIA

26 55064 17003582130 AJAY SINGH

27 55065 17003582131 DUSHYANT ROLYANA

28 55069 17003582134 MAYANK VERMA

29 55070 17003582135 MAHIMA YADAV


30 55071 17003582136 NAINA

31 55072 17003582137 SHASHVAT

32 55073 17003582138 TARUN

33 55074 17003582139 RAHUL KUMAR

34 55075 17003582140 RAVI

35 55076 17003582141 UMESH

36 55077 17003582142 VIKAS KUMAR RANA

37 55078 17003582143 SHUBHAM

38 55079 17003582144 TARUN JINGALIA

39 55080 17003582145 AMAN CHAND SINGH

40 55081 17003582146 KARAN

41 55083 17003582147 NEERAJ

42 55084 17003582148 SUMIT KUMAR YADAV

43 55085 17003582149 AJAY KUMAR

44 55086 17003582150 MINTU

45 55087 17003582151 SAGAR

46 55088 17003582152 ANKIT KUMAR

47 55089 17003582153 PANKIT KUMAR

48 55090 17003582154 HARASH VARDHAN NAGAR

49 55091 17003582155 RAHUL

50 55092 17003582156 VIKRAM

51 55094 17003582157 JASLEEN KAUR

52 55095 17003582158 NITIN CHAUHAN

53 55096 17003582159 ASHISH

54 55097 17003582160 DINESH SARWAN

55 55098 17003582161 ISHWAR YADAV

56 55100 17003582162 SHUBNIT KATHURIA

57 55101 17003582163 SHUBHAM SHARMA


Software Lab based on Java (Solutions):-

1. WAP to find the largest of n natural numbers.


import java.util.Scanner; class
Largest_Number { public static void
main(String[] args){ int n, max;
Scanner s = new Scanner(System.in);
System.out.print("Enter number of elements in the array:");
n = s.nextInt(); int a[] = new int[n];
System.out.println("Enter elements of array:");
for(int i = 0; i < n; i++){ a[i]
= s.nextInt();
} max = 0; for(int i = 0;
i < n; i++){ if(a[max] <
a[i]){ max = i;
}
}
System.out.println("Maximum value:- "+a[max]+" at position "+(max+1)); }
}

2. WAP to find whether a given number is prime or not.


import java.util.Scanner; class prime {
public static void main(String[] args){
Scanner number = new Scanner(System.in);
System.out.print("Enter the number:-"); int
usernumber = number.nextInt();
int i=2; int a=0; if(
usernumber == 0){
System.out.println("The number is not prime.");
}
while(i<Math.sqrt(usernumber)){
if(usernumber%i == 0){ a=a+1;
} i++;
}
if(a==0){
System.out.println("The number is prime.");
} else{
System.out.println("The number is not prime.");}
}
}

3. Write a menu driven program for following:-


a. Display a Fibonacci series.
b. Compute Factorial of a number.
c. WAP to check whether a given number is odd or even.
d. WAP to check whether a given string is palindrome or not.
import java.util.Scanner;
class menu_driven { static
void fabonaci(){
Scanner number = new Scanner(System.in);
System.out.print("Enter the number upto which you want Fabonaci series:-");
int n=number.nextInt();
int a=0;
int b=1;
int i; int
c;
System.out.println(a);
System.out.println(b);
for(i=2;i<n;i++){ c=a+b;
System.out.println(c);
a=b; b=c; } }
static void factorial(){
Scanner number = new Scanner(System.in);
System.out.print("Enter the number upto which you want Factorial:-"); int
n=number.nextInt();
int fact=1;
if(n==0){
System.out.println("Factorial is 0.");} else{
for(int a=1;a<n+1;a++){
fact=fact*a;
}
System.out.println(fact);}
} static void
even_odd(){
Scanner number = new Scanner(System.in);
System.out.print("Enter the number:-");
int n = number.nextInt(); if (n%2==0){
System.out.println("The "+ n +" is even number");
} else{
System.out.println("The "+ n +" is odd number");
}}
static void palindrom(){
Scanner str = new Scanner(System.in);
System.out.print("Enter the string:-");
String s = str.nextLine();
String reverse="";
int i;

for (i=s.length()-1;i>=0;i--){ reverse=reverse+s.charAt(i);


}
if( s.equals(reverse)){
System.out.println("String is a palindrome string");
}
else{
System.out.println("String is not a palindrome string");
}
} public static void main(String[] args){ Scanner
number = new Scanner(System.in);
System.out.println("****** MENU DRIVEN ****** \n 1)Fabonaci of a number. \n
2)Factorial of a number.
\n 3) Even or odd number. \n 4)Palindrome of a string.");
System.out.print("Enter your choice:-");
int num = number.nextInt();
if(num==1){ fabonaci();
}
else if(num==2){ factorial();
}
else if(num==3){ even_odd();
} else{
palindrom();
}
}
}

4. WAP to print the sum and product of digits of an Integer and reverse the
Integer. import java.util.Scanner; class sum_product {
public static void main(String[] args){
Scanner number = new Scanner(System.in);
System.out.println("Enter the number:-");
int n = number.nextInt(); int
a,rev=0,sum=0,product=1; while(n>0){
a=n%10;
rev=rev*10+a;
sum=sum+a;
product=product*a; n=n/10;
}
System.out.println("Sum of the numbers is:- " + sum);
System.out.println("Reverse of the number is:- " + rev);
System.out.println("Product of the numbers is:- " + product); }
}

5. Write a program to create an array of 10 integers. Accept values from the user in that
array. Input another number from the user and find out how many numbers are
equal to the number passed, how many are greater and how many are less than the
number passed. import java.util.Scanner; class array {
public static void main(String[] args){
int n=10,grt=0,less=0,equ=0; Scanner
s = new Scanner(System.in);
int a[] = new int[n];
System.out.println("This is array of 10 elements \n Enter elements of array:");
for(int i = 0; i < n; i++){
System.out.print("Enter the element:-");
a[i] = s.nextInt();
}
Scanner no = new Scanner(System.in);
System.out.print("Enter the number:-");
int num=no.nextInt(); for(int i=0;i<n-
1;i++){ if(a[i]==num){
equ+=1;
} if(a[i]>=num){
grt+=1;
}
if(a[i]<num){
less+=1;
}
}
System.out.println("Numbers equal to "+ num +" is:- "+equ);
System.out.println("Numbers grater than "+ num +" is:- "+ grt);
System.out.println("Numbers less than "+ num +" is:- "+ less); }
}

6. Write a program that will prompt the user for a list of 5 prices. Compute the
average of the prices and find out all the prices that are higher than the calculated
average. import java.util.Scanner;
class list {
public static void main(String[] args){ int
n,sum,avg=0,count=0;
Scanner s = new Scanner(System.in);
System.out.println("This a list of 5
prices:"); n = 5; int list[] = new int[n];
for(int i = 0; i < n; i++){
System.out.print("Enter prices of list:");
list[i] = s.nextInt();
} sum=0
;
for(int i: list){ sum+=i;
} avg=sum/n;

for(int i: list){
if(i>avg){ count+=1;
}
}
System.out.println("Average value of list prices: "+avg);
System.out.println("Number of prices greter than average value : "+count);
}
}

7. Write a program in java to input N numbers in an array and print out the
Armstrong numbers from the set. import java.util.Scanner; class armstrong {
private static boolean isArmstrong(int number){
double result=0; int num=number;
double length=String.valueOf(number).length();
while(number != 0){ double
remainder=number%10; double
rem=Math.pow(remainder,length);
result = result+rem;
number=number/10;
}
if(num==result){
return true; }
return false;
}
public static void main(String args[]){
System.out.println("Entar the number to find if its an armstrong number or not:-"); int
number = new Scanner(System.in).nextInt();
if(isArmstrong(number)){
System.out.println("Number :"+ number +" is an armstrong number");
} else{
System.out.println("Number :"+ number +" is not an armstrong number");
}
}
}

8. Write java program for the following matrix operations:-

a. Addition of two matrices


import java.util.Scanner; class
add{
public static void main(String[] args){
Scanner num = new Scanner(System.in);
System.out.println("Enter the number of rows:-"); int
row = num.nextInt();
System.out.println("Enter the number of column:-"); int
col = num.nextInt();
System.out.println("Enter the 1st matrix:-");
int twoD1[][]=new int[row][col]; int i,j;
for(i=0;i<row;i++){ for(j=0;j<col;j++){
System.out.print("Enter the elements of matrics:-"); twoD1[i][j]=num.nextInt();
;
}
}
System.out.println("Enter the 2nd matrix:-");
int twoD2[][]=new int[row][col];
for(i=0;i<row;i++){ for(j=0;j<col;j++){
System.out.print("Enter the elements of matrics:-");
twoD2[i][j]=num.nextInt();;
;
} } int result[][]=new
int[row][col]; for
(i=0;i<row;i++){
for(j=0;j<col;j++)
result[i][j]=twoD1[i][j] + twoD2[i][j];
}
System.out.println("The Resulted matrix is:-");
for(i=0;i<row;i++){ for(j=0;j<col;j++)
System.out.print(result[i][j]+" ");
System.out.println();
}
}
}

b. Summation of two matrices


import java.util.Scanner; class
sub{
public static void main(String[] args){
Scanner num = new Scanner(System.in);
System.out.println("Enter the number of rows:-"); int
row = num.nextInt();
System.out.println("Enter the number of column:-"); int
col = num.nextInt();
System.out.println("Enter the 1st matrix:-");
int twoD1[][]=new int[row][col]; int i,j;
for(i=0;i<row;i++){ for(j=0;j<col;j++){
System.out.print("Enter the elements of matrics:-");
twoD1[i][j]=num.nextInt();
;
}
}
System.out.println("Enter the 2nd matrix:-");

int twoD2[][]=new int[row][col]; System.out.print(result[i][j]+" ");


System.out.println();
}
}
}

c. Transpose of a matrix import


java.util.Scanner; class transpose{
public static void main(String[] args){
Scanner num = new Scanner(System.in);
System.out.println("Enter the number of rows:-"); int
row = num.nextInt();
System.out.println("Enter the number of column:-"); int
col = num.nextInt();
int twoD[][]=new int[row][col];
int i,j,k;
for(i=0;i<row;i++){ for(j=0;j<col;j++){
System.out.print("Enter the elements of matrics:-"); k=num.nextInt();
twoD[i][j]=k;
;
}
}
System.out.println("The above matrix before Transpose is");
for(i=0;i<row;i++){ for(j=0;j<col;j++)
System.out.print(twoD[i][j]+" ");
System.out.println();
}
System.out.println("The above matrix after Transpose is");
for(i=0;i<col;i++){ for(j=0;j<row;j++)
System.out.print(twoD[j][i]+" ");
System.out.println();
}
}
}

d. Input the elements of matrices from


user. import java.util.Scanner; class array{
public static void main(String[] args){
Scanner num = new Scanner(System.in);
System.out.println("Enter the number of rows:-"); int
row = num.nextInt();
System.out.println("Enter the number of column:-"); int
col = num.nextInt();
int twoD[][]=new int[row][col];
int i,j,k;

for(i=0;i<row;i++){ for(j=0;j<col;j++){
System.out.print("Enter the elements of matrics:-"); k=num.nextInt();
twoD[i][j]=k;
;
}
}
System.out.println("The above matrix is:-"); for(i=0;i<row;i++){
for(j=0;j<col;j++)
System.out.print(twoD[i][j]+" ");
System.out.println();
}
}
}

9. Write a java program that computes the area of a circle, rectangle and a Cylinder
using function overloading. import java.util.Scanner; class overload {
double area(float l,float w,float h){
return l*w*h;
} double area(float
r){ return 3.14*r*r;
} double area(float r,float
h){ return 3.14*r*r*h;
} } class
Area {
public static void main(String args[]){
overload load = new overload(); double
rectanglebox = load.area(5,8,9);
System.out.println("Area of rectangular box is:" + rectanglebox);
System.out.println(""); double
circle = load.area(5);
System.out.println("Area of circle is:" + circle);
System.out.println(""); double cylinder =
load.area(6,12);
System.out.println("Area of cylinder is:" + cylinder); }
}

10. Write a Java for the implementation of Multiple inheritance using interfaces to
calculate the area of a rectangle and triangle. import java.util.Scanner; interface area{ float
compute(float x,float y); }

class rectangle implements area{ public


float compute(float x,float y){
return (x*y);
}}
class triangle implements area{ public float
compute(float x,float y){
return (x*y/2);
}}
class interface_area{ public static void
main(String args[]){
rectangle rect = new rectangle(); triangle
tri = new triangle();
area temp; temp=rect;
System.out.println("Area of rectangle = "+temp.compute(1,2)); temp=tri;
System.out.println("Area of rectangle = "+temp.compute(10,2));
}
}

11. Write a java program to create a frame window in an Applet. Display your name,
address and qualification in the frame window. import java.awt.*; import java.applet.*;
/*
<applet code="program11th" width="200" height="150">
</applet>
*/
class sampleframe extends Frame { sampleframe(String
title) {
super(title);
}
public void paint(Graphics g) {
g.drawString("Name :- Yatin",10,50);
g.drawString("Address :- Rewari (Haryana)",10,70);
g.drawString("Qualification :- B Sc. applied physical science ",10,90);
}}
public class program11th extends Applet {
Frame f; public
void init(){
f = new sampleframe("A Frame Window");
f.setSize(250,250);
f.setVisible(true);
}
public void start(){
f.setVisible(true);

} public void
stop(){
f.setVisible(false);
}
public void paint(Graphics g) {
g.drawString("A Simple Applet ",20,20);
}
}

12. Write a java program to draw a line between two coordinates in a window.
import java.awt.*; import
java.applet.*;
public class program12th extends Applet { public
void paint(Graphics g) { g.setColor(Color.cyan);
g.drawLine(10,10,150,150);
}}
/*
<applet code="program12th" width="200" height="150">
</applet>
*/

13. Write a java program to display the following graphics in an applet window. a.
Rectangles
b. Circles
c. Ellipses
d. Arcs
e. Polygons import java.awt.*; import java.applet.*; public class program13th extends Applet
{
TextField text1; public
void init(){ text1 = new
TextField(8);
add(text1);
text1.setText("0");
} public void
paint(Graphics g) { int a=0;

String s;
g.drawString("Input your choice in box:-",10,50);
g.drawString("1)Rectangle ",10,70);
g.drawString("2)Circle ",10,90);
g.drawString("3)Arc ",10,110);
g.drawString("4)Oval ",10,130);
g.drawString("5)Polygon ",10,150);
s=text1.getText();
a=Integer.parseInt(s); if(a==1){
g.setColor(Color.cyan);
g.drawRect(80,110,180,130);
} else if(a==2){
g.setColor(Color.cyan);
g.drawRoundRect(70,80,90,90,200,200);
} else if(a==3){
g.setColor(Color.cyan);
g.drawArc(150,50,80,90,0,360);
} else if(a==4){
g.setColor(Color.cyan);
g.drawOval(100,100,150,100); } else
if(a==5){ int x[]={
110,120,140,250,230,280 }; int y[]={
280,230,250,140,120,110 }; int
numberofpoints=6;
g.setColor(Color.blue);
g.drawPolygon(x,y,numberofpoints);
} else
{

}
g.drawString("You entered a wrong choice",10,180);
}}
/*
<applet code="program13th" width="500" height="400">
</applet>
*/

14. Write a program that reads two integer numbers for the variables a and b. If any
other
character except number (0-9) is entered then the error is caught by

NumberFormatException object. After that ex.getMessage() prints the information


about the error occurring causes.
import java.io.*; class
exception {
public static void main(String[] args) throws Exception{
try{ int a,b;
BufferedReader in= newBufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the 1st number:- "); a
= Integer.parseInt(in.readLine());
System.out.print("Enter the 2nd number:- ");
b = Integer.parseInt(in.readLine());
System.out.print("Their is no exception in "+a+" & "+b);
}
catch(NumberFormatException ex){
System.out.println(ex.getMessage() + " is not a numeric value.");
System.exit(0);
}
}
}

15. Write a program for the following string operations:


a. Compare two strings
b. Concatenate two strings
c. Compute length of a string import java.util.Scanner;
class string { static
void compare(){
Scanner s = new Scanner(System.in);
System.out.print("Enter your 1st string:-");
String s1=s.nextLine();
System.out.println("Enter your 2nd string:-"); String
s2=s.nextLine();
boolean result1 = s1.equalsIgnoreCase(s2); if
(result1){
System.out.println("Strings are equal");} else{
System.out.println("Strings are not equal");
} } static void
concatinate(){
Scanner s = new Scanner(System.in);
System.out.println("Enter your 1st string:-");
String s1 = s.nextLine();
System.out.println("Enter your 2nd string:-");
String s2=s.nextLine();
String result2 = s1.concat(s2);
System.out.println("Concatenated strings:- "+result2);
}
static void length1(){

Scanner s = new Scanner(System.in);


System.out.println("Enter your string for computing length of it:-");
String s1=s.nextLine();
int result = s1.length();
System.out.println("Lenghth of string:- "+result);
}
public static void main(String[] args){
System.out.println(" 1. Compare two strings \n 2. Concatenate two strings \n 3. Compute length
of a string");
Scanner s = new Scanner(System.in);
System.out.println("Enter your choice:-");
int choice=s.nextInt();
if (choice==1){ compare();
} else
if(choice==2){
concatinate();
}
else if(choice==3){
length1(); } else{
System.out.println(
"You entered
wrong choice");}
}
}

16. Create a class called Fraction that can be used to represent the ratio of two integers.
Include appropriate constructors and methods. If the denominator becomes zero,
throw and handle an exception. import java.util.Scanner; class Fraction {
public static void main(String[] args) { int
a, b, ratio;
Scanner num = new Scanner(System.in);
System.out.println("Input two integers for getting their ratio:-");
System.out.print("Enter the 1st number:- "); a
= num.nextInt();
System.out.print("Enter the 2nd number:- "); b
= num.nextInt();
try {
ratio = a/b;
System.out.println("Ratio of "+a+" & "+b+" is:- " + ratio);
}
catch (ArithmeticException e) {
System.out.println("Exception caught: Division by zero.");
}
}
}

You might also like