BSC Program 3rd Year
BSC Program 3rd Year
Vth Semester
S.No. College Roll No. University Roll No. Names
1 55004 17003582099 VIVEK JOSHI
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");
}
}
}
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); }
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
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.");
}
}
}