Professional Documents
Culture Documents
Week 5-8
Week 5-8
public class q1 {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
#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 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];
}
Output:
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(" ");
}
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();
}
}
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) {
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);
}
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) {
}
}
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){
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 {
temp= arr[0];
arr[0]=arr[arr.length-1];
arr[arr.length-1]=temp;
}
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];
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:
}
}
Output:
public class q6 {
public static void main(String[] args) {
int arr[]={7, 18, 2, 31, 45, 60};
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
import java.util.Scanner;
public class q1 {
public static void main(String[] args) {
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) {
}
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) {
return rev;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string : ");
String str = sc.nextLine();
Output:
public class q5 {
public static boolean pal(String str) {
return true;
}
return false;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
if(pal(str)==true){
System.out.println("String is a palindrome.");
}
else{
System.out.println("String is not a palindrome.");
}
}
}
Output:
</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;
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;
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;
void display(){
System.out.print("Color: " + color);
System.out.print(", Taste: " + taste);
System.out.println(", Price: " + price);
}
}
System.out.println("Strawberry-");
Strawberry.display();
System.out.println("Guava-");
Guava.display();
}
}
Output:
void display() {
System.out.print("Color = " + color);
System.out.print(", Taste = " + taste);
System.out.println(", Price = " + price);
}
}
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;
void display() {
System.out.print("Color = " + color);
System.out.print(", Taste = " + taste);
System.out.println(", Price = " + price);
}
}
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: