Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 43

WEEK 5

Problems based on Arrays in Java.


#1 Write a Java program to insert 10, 20, 30… in an array and display them.
Code:
import java.util.Scanner;

public class q1 {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);

int arr[]= new int[10];

for(int i=1; i<10; i++){


System.out.println("Enter element "+i+" of array: ");
arr[i]= sc.nextInt();
}
System.out.println("Array: ");
for(int i=1; i<10; i++){
System.out.print(arr[i]+", ");
}
}
}
Output:

#2 Write a Java program to calculate the sum of all the array elements.
Code:

import java.util.Scanner;

public class q2 {
public static void main(String[] args) {

int arr[]= new int[5];

int sum=0;
Scanner sc= new Scanner(System.in);
System.out.println("Enter 5 elements of array: ");
for(int i=0; i<5; i++){
arr[i]=sc.nextInt();

sum=sum+arr[i];
}

System.out.println("Sum of elements of array= "+sum);


}

Output:

#3 Write a Java program to print the following pattern:


1
121
12321
1234321
123454321

Code:
public class q3 {
public static void main(String[] args) {

int n=5;
for(int i=1; i<=n; i++){
for(int j=1; j<=n-i; j++){
System.out.print(" ");
}

for(int j=1; j<=i; j++){


System.out.print(j);
}

for(int j=i-1; j>=1; j--){


System.out.print(j);
}
System.out.println();
}
}
}

Output:
#4 Write a java program to find the sum of following series where n is input by
the user:
1+1/2+1/3+1/4+...............+1/n.

Code:

import java.util.Scanner;

public class q4 {
public static double seriesSum(int n){
double i, sum=0;
for( i=1; i<=n; i++){
sum=sum+(1/i);
}
return sum;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter value of n: ");
int n= sc.nextInt();

System.out.println("Sum of the series is: "+seriesSum(n));

}
}

Output:

5# Write a Java program and compute the sum of the digits of an integer.

Code:

import java.util.Scanner;

public class q5 {
public static int sumDig(int n){

int sum=0;

while(n>0){

int r=n%10;
n=n/10;
sum=sum+r;
}
return sum;
}
public static void main(String[] args) {

Scanner sc= new Scanner(System.in);


System.out.println("Enter a number: ");
int n=sc.nextInt();

System.out.println("Sum of the digits is: "+ sumDig(n));


}
}

Output:
6# Write a Java program to calculate the factorial of a number.
Code:

import java.util.Scanner;

public class q6 {
public static int fact(int n){

if(n<=1){
return 1;
}
return n*fact(n-1);
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
System.out.print("Enter value of n: ");
int n= sc.nextInt();

System.out.println("Factorial of the number is: "+ fact(n));


}
}

Output:

WEEK 6
Problems Based on If statement/Looping/Array in JAVA.
1# Write a Java program to print the odd numbers from 1 to 99.
Code:

public class q1 {
public static void main(String[] args) {

for(int i=1; i<100; i++){


if(i%2!=0){
System.out.println(i);
}

}
}

Output:
2# Write a Java program to check whether a number is prime or not.
Code:

import java.util.Scanner;

public class q2 {
public static boolean f(int n){

for(int i=2; i<n; i++){


if(n%i==0){
return false;
}
}
return true;
}
public static void main(String[] args) {

Scanner sc =new Scanner(System.in);


System.out.println("Enter a number: ");
int n= sc.nextInt();

if(f(n)==true){
System.out.println("Number is prime");
}
else{
System.out.println("Number is not prime");
}
}
}

Output:

3# Write a Java program to swap the first and last elements of an array.
Code:
import java.util.Arrays;
import java.util.Scanner;
public class q3 {

public static void main(String[] args) {


int temp;
int arr[]= new int[5];

Scanner sc= new Scanner(System.in);


System.out.println("Enter 5 elements of array: ");
for(int i=0; i<5; i++){
arr[i]=sc.nextInt();
}
System.out.println("Original array: "+Arrays.toString(arr));

temp= arr[0];
arr[0]=arr[arr.length-1];
arr[arr.length-1]=temp;

System.out.println("Array after swapping: "+Arrays.toString(arr));


}

}
Output:
4# Write a Java program to find the maximum and minimum among array
elements.
Code:

import java.util.Scanner;

public class q4 {
public static void main(String[] args) {
int a[]= new int[5];

Scanner sc= new Scanner(System.in);


System.out.println("Enter 5 elements of array: ");
for(int i=0; i<5; i++){
a[i]=sc.nextInt();
}

int max=a[0];
int min=a[0];
for(int i=1; i<a.length; i++){
if(max<a[i]){
max=a[i];
}
if(min>a[i]){
min=a[i];
}
}
System.out.println("Maximum element is: "+max);
System.out.println("Minimum element is: "+min);
}

Output:

5# Write a Java program to print all prime numbers between 0 to 100.


Code:
public class q5 {
public static void main(String[] args) {

for(int i=0; i<=100; i++){


if(i%2==0){
System.out.println(i);
}

}
}
Output:

6# Write a Java program to implement linear search.


Code:
import java.util.Scanner;

public class q6 {
public static void main(String[] args) {
int arr[]={7, 18, 2, 31, 45, 60};

Scanner sc = new Scanner(System.in);


System.out.print("Enter a number: ");
int key= sc.nextInt();

for(int i=0; i<arr.length; i++){


if(arr[i]==key){
System.out.println("Element found at index: "+i);

else{
System.out.println("Element not found in the array");
}

}
}
}
Problems Based on HTML:
1# Create the following table in HTML with Dummy Data:
Code:

<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Table</title>
<style>
td{
padding: 10px;
margin: 10px;
}
table, th, td{
border: 1px solid black;
border-collapse: collapse;
}
</style>
</head>

<body>

<table border="1">
<tr>
<th>Name of the train</th>
<th>Place</th>
<th>Destination</th>
<th>Train No.</th>
<th colspan="2">Time</th>
<th>Fare</th>
</tr>

<td></td>
<td></td>
<td></td>
<td></td>
<td>Arrival</td>
<td>Departure</td>
<td></td>
<tr>
<td>Duronto</td>
<td>Ahmedabad</td>
<td>Mumbai</td>
<td>12268</td>
<td>23:40 pm</td>
<td>06:00 am</td>
<td>1065</td>
</tr>
<tr>
<td>Rajdhani</td>
<td>Mumbai</td>
<td>New Delhi</td>
<td>12951</td>
<td>16:35 pm</td>
<td>08:35 am</td>
<td>2985</td>
</tr>
<tr>
<td>Shatabdi</td>
<td>Howrah</td>
<td>Ranchi</td>
<td>12019</td>
<td>06:05 am</td>
<td>13:15 pm</td>
<td>1205</td>
</tr>
<tr>
<td>Jhelum Express</td>
<td>Pune</td>
<td>Jammu Tawi</td>
<td>11077</td>
<td>17:20 pm</td>
<td>10:10 am</td>
<td>490</td>
</tr>
</table>
</body>
</html>

Output:
WEEK 7
Problems Based on If statement/Looping/Array/Strings in JAVA

1# Write a Java program to implement binary search.


Code:

import java.util.Scanner;

public class q1 {
public static void main(String[] args) {

int arr[]={2,65,9,17, 77, 93};


int s=0, e=arr.length-1;
int mid;

Scanner sc= new Scanner(System.in);


System.out.print("Enter a number: ");
int n= sc.nextInt();

while(e>=s){
mid = (s+e)/2;
if(arr[mid]==n){
System.out.println("Number found at index: "+mid);
break;
}

else if(arr[mid]<n){
s=mid+1;
}
else{
e=mid-1;

}
}
if(s>e){
System.out.println("Number not found");

}
}

Output:
2# Write a Java program to arrange the elements of an array in ascending
order (Sorting).
Code:

public class q2 {
public static void main(String[] args) {

int a[]={23, 46, 7, 9, 15, 12};


int temp;
for(int i=0; i<a.length-1; i++){
if(a[i]>a[i+1]){
temp=a[i+1];
a[i+1]=a[i];
a[i]=temp;
i=-1;
}

}
for(int i=0; i<a.length; i++){
System.out.print(a[i]+", ");
}
}
}

Output:
3# Write a program to store “Java is awesome” in a string and display it.
Code:

import java.util.Scanner;

public class q3 {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
System.out.println("Enter a string: ");
String s= sc.nextLine();

System.out.println(s);
}
}

Output:
4# Write a program to reverse a given string.
Code:

import java.util.Scanner;

public class q4 {
public static String revStr(String str) {

String rev = " ";

for (int i = 0; i < str.length(); i++) {

rev = str.charAt(i) + rev;


}

return rev;

}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string : ");
String str = sc.nextLine();

System.out.println("Reversed string: "+revStr(str));


}
}

Output:

5# Write a program to check whether a given string is palindrome or not.


Code:
import java.util.Scanner;

public class q5 {
public static boolean pal(String str) {

String rev = "";

for (int i = 0; i < str.length(); i++) {


rev = str.charAt(i) + rev;
if(rev.equals(str)){

return true;
}

return false;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.print("Enter a string : ");


String str = sc.nextLine();

if(pal(str)==true){
System.out.println("String is a palindrome.");
}
else{
System.out.println("String is not a palindrome.");
}
}
}

Output:

Problems Based on HTML:

2# Write HTML code to generate the following output:


Code:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>image</title>
<style>
table
{
border-collapse: collapse;
}
table tr td
{ border: 1px solid black;
width: 100px;
height: 50px;
}
img{height: 109px;
width:209px;}
</style>

</head>
<body>
<table>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td>5</td>
<td colspan="2" rowspan="2"> <img src="images/amulogo.png" /></td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td>8</td>
</tr>
<tr>
<td>9</td>
<td>10</td>
<td>11</td>
<td>12</td>
</tr>
</table>
</body>
</html>

Output:
WEEK 8
1# Create a class FRUIT which has data members color, taste and price. Also
create a method display() which will print values of FRUIT object. Create three
objects of FRUIT class and call their display() methods.
Code:

class Fruit{
String color;
String taste;
int price;

public void display(){


System.out.println("Color: "+color);
System.out.println("Taste: "+taste);
System.out.println("Price "+price);
}
}
public class q1 {
public static void main(String[] args) {

Fruit apple= new Fruit();


apple.color= "red";
apple.taste= "sweet";
apple.price= 60;

Fruit orange= new Fruit();


orange.color= "orange";
orange.taste="citrusy";
orange.price=30;

Fruit banana= new Fruit();


banana.color="yellow";
banana.taste= "sweet";
banana.price= 10;

System.out.println("Apple- ");
apple.display();

System.out.println("Orange- ");
orange.display();

System.out.println("Banana- ");
banana.display();
}
}
Output:

2# Create a class FRUIT which has data members color, taste and price. It has a
method setDetails() which will set the values of color, taste and price. Also
create a method display() which will print values of FRUIT object.
Code:
class Fruit{
String color;
String taste;
int price;

void setDetails(String c,String t,int p){


color = c;
taste = t;
price = p;
}
void display(){
System.out.println("Color: "+color);
System.out.println("Taste: "+taste);
System.out.println("Price: "+price);
}
}
public class q2 {
public static void main(String[] args) {
Fruit Kiwi= new Fruit();
Kiwi.setDetails("Green", "Citrusy", 110);

Fruit Strawberry= new Fruit();


Strawberry.setDetails("Red", "Sweet/Sour", 90);

Fruit Guava= new Fruit();


Guava.setDetails("Green", "Sweet", 45);
System.out.println("Kiwi-");
Kiwi.display();

System.out.println("Strawberry-");
Strawberry.display();

System.out.println("Guava-");
Guava.display();
}
}
Output:

3# In previous question, set the values of color, taste and price using
Constructor.
Code:
public class q3 {
static class Fruit{
private String color;
private String taste;
private int price;

Fruit(String color, String taste, int price){


this.color = color;
this.taste = taste;
this.price = price;
}

void display(){
System.out.print("Color: " + color);
System.out.print(", Taste: " + taste);
System.out.println(", Price: " + price);
}
}

public static void main(String[] args) {


Fruit Kiwi= new Fruit("Green", "Citrusy", 110);

Fruit Strawberry= new Fruit("Red", "Sweet/Sour", 90);

Fruit Guava= new Fruit("Green", "Sweet", 45);


System.out.println("Kiwi-");
Kiwi.display();

System.out.println("Strawberry-");
Strawberry.display();

System.out.println("Guava-");
Guava.display();
}
}

Output:

4# Add one-argument constructor and two-argument constructor in addition


to default constructor in FRUIT class.
Code:
public class q4 {
static class FRUIT {
private String color;
private String taste;
private int price;
FRUIT(String color, String taste, int price) {
this.color = color;
this.taste = taste;
this.price = price;
}
FRUIT(String color, String taste) {
this.color = color;
this.taste = taste;
this.price = -1;
}
FRUIT(String color) {
this.color = color;
this.taste = "Undefined";
this.price = -1;
}
FRUIT() {
this.color = "Unknown";
this.taste = "Undefined";
this.price = -1;
}

void display() {
System.out.print("Color = " + color);
System.out.print(", Taste = " + taste);
System.out.println(", Price = " + price);
}
}

public static void main(String[] args) {


FRUIT Banana = new FRUIT("Yellow","Sweet",10);
FRUIT Orange = new FRUIT("Orange","Sweet-tart");
FRUIT Apple = new FRUIT("red");
FRUIT fruit1 = new FRUIT();

System.out.println("Banana-");
Banana.display();

System.out.println("Orange-");
Orange.display();

System.out.println("Apple-");
Apple.display();

System.out.println("fruit1-");
fruit1.display();
}
}
Output:
5# Use the concept of constructor-chaining in the previous question using
this().
Code:
public class q5 {
static class FRUIT {
private String color;
private String taste;
private int price;

FRUIT(String color, String taste, int price) {


this.color = color;
this.taste = taste;
this.price = price;
}
FRUIT(String color, String taste) {
this();
this.color = color;
this.taste = taste;
}
FRUIT(String color) {
this();
this.color = color;
}
FRUIT() {
this.color = "Unknown";
this.taste = "Undefined";
this.price = -1;
}

void display() {
System.out.print("Color = " + color);
System.out.print(", Taste = " + taste);
System.out.println(", Price = " + price);

}
}

public static void main(String[] args) {


FRUIT Banana = new FRUIT("Yellow","Sweet",10);
FRUIT Orange = new FRUIT("Orange","Sweet-tart");
FRUIT Apple = new FRUIT("red");
FRUIT fruit1 = new FRUIT();

System.out.println("Banana-");
Banana.display();
System.out.println("Orange-");
Orange.display();

System.out.println("Apple-");
Apple.display();

System.out.println("fruit1-");
fruit1.display();
}
}

Output:

You might also like