Académique Documents
Professionnel Documents
Culture Documents
code:-
import java.util.*;
output:-
2. Read the number and check whether it is divisible by 3 and 5.
code:-
import java.util.*;
output:-
3. Display Subject Name based on room number. If the user enters 604 then
display Java Programming , If the user enters 605 then display Python
programming for any other input display Invalid input to the user
code:-
import java.util.*;
output:-
4. Print the sum of first n numbers. If n is 3 then print the sum of 1+2+3 to
the user. Get n from the user
code:-
import java.util.*;
output:-
2 2 2
5. Print the sum of the series 1 +2 +3 up to n terms
code:-
import java.util.*;
output:-
6. Print the multiplication table by getting the n from the user.
Code:-
import java.util.*;
output:-
7. Provide the option of adding two numbers to the user until the user wants
to exit.
Code:-
import java.util.*;
output:-
8. Print this pattern for n lines
(a) code:-
import java.util.*;
output:-
(b) code:-
import java.util.*;
output:-
(c) code:-
import java.util.*;
output:-
9. Write a Java program to sort an array of positive integers of an given array, in
the sorted array the value of the first element should be maximum, second value
should be minimum value, third should be second maximum, fourth second be
second minimum and so on.
Code:-
import java.util.*;
class sort{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for(int i = 0; i<n; i++){
arr[i] = sc.nextInt();
}
for(int i = 0; i<n-1; i++){
for(int j = 0; j<n-1; j++){
if(arr[j]>arr[j+1]){
int tmp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = tmp;
}
}
}
System.out.print("Sorted array: ");
for(int i = 0; i<n; i++){
if(i%2==0){
System.out.print(arr[n-(i/2)-1] + " ");
}
else{
System.out.print(arr[((i+1)/2)-1]+ " ");
}
}
System.out.println();
}
}
output:-
10. Write a Java program to separate even and odd numbers of an given array of
integers. Put all even numbers first, and then odd numbers.
Code:-
import java.util.*;
class eo{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for(int i =0; i<n; i++){
arr[i] = sc.nextInt();
}
int[] a = new int[n];
int left = 0, right = n-1;
while(left<right){
while(arr[left]%2==0 && left<right){
left++;
}
while(arr[right]%2==1 && left<right){
right--;
}
if(left<right){
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}
}
for(int x:arr){
System.out.print(x+" ");
}
System.out.println();
}
}
output:-
11. Write a Java program to remove the duplicate elements of a given array and
return the new length of the array.
Code:-
import java.util.Scanner;
for(next_element=initial_element+1;next_element<arr_size;){
/* if initial_element matches to next_element
then take next _element and matches till end */
if(arr_elements[initial_element] ==
arr_elements[next_element]){
for(int temp = next_element; temp<arr_size;
++temp){
arr_elements[temp] =
arr_elements[temp+1];
}
arr_size = arr_size-1;
}
else
next_element++;
}
}
Output:-
12. Write a Java program to find the sum of the two elements of a given array
which is equal to a given integer.
Code:-
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
Output:-
13. Write a program to demonstrate the knowledge of students in
multidimensional arrays and looping constructs.
Code:-
class Main
{
public static void main(String[] args)
{
// Declaring 2-D array with 2 rows
int arr[][] = new int[2][];
// Initializing array
int count = 0;
for (int i=0; i<arr.length; i++)
for(int j=0; j<arr[i].length; j++)
arr[i][j] = count++;
Output:-
14. A hash algorithm uses rotation and fold shift methods to compute the
address at which the user input has to be stored. Define a static method to
perform rotation of the data by moving the least significant digit to the most
significant bit position. Also define a non-static method to perform fold shift by
dividing the rotated data into segments of length 2 and then add all the segments
to get the hash address. If the sum has more than 2 digits, delete the most
significant digit and the resulting number is the address. Invoke these methods
from main( ) method.
Eg., If the data is 112286, after rotation it should be 611228 and after fold shift
it should be 61 + 12 + 28=101 =01 (after deleting the most significant digit)
Code:-
import java.util.*;
public class rotate {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
char[] arr=new char[s.length()];
for(int i=0;i<s.length();i++){
if(i==0){
arr[i]=s.charAt(s.length()-1);
}
else{
arr[i]=s.charAt(i-1);
}
}
String rot=new String(arr);
String[] str=new String[s.length()/2];
int sum=0;
int count=0;
for(int i=0;i<s.length();i++){
str[count]=rot.substring(i,i+2);
int x=Integer.parseInt(str[count]);
sum=sum+x;
count++;
i++;
}
if(sum>99){
int d=sum/10;
System.out.print(d%10);
System.out.println(sum%10);
}
else{
System.out.println(sum);
}
}
}
Output:-
Code:-
code:-
import java.util.*;
public class format{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
int xint=str.indexOf("%d");
int xfloat=str.indexOf("%f");
int xstring=str.indexOf("%s");
int a=str.indexOf(",a");
int b=str.indexOf(",b");
int s=str.indexOf(",s");
int x=str.indexOf(",x");
int count=0;
if(xint > xfloat){
if(a<b || x<b){
count++;
}
}
else if(xint < xfloat){
if(a>b || x>b){
count++;
}
}
if(xint > xstring){
if(a<s || x<s){
count++;
}
}
else if(xint < xstring){
if(a>s || x>s){
count++;
}
}
if(xfloat > xstring){
if(b<s){
count++;
}
}
else if(xfloat < xstring){
if(b>s){
count++;
}
}
if(count==0){
System.out.println("correct syntax");
}
else{
System.out.println("wrong syntax");
}
}
}
Output:-
16. Write a Java program to create a class called Student having data members
Regno, Name, Course being studied and current CGPA. Include constructor to
initialize objects. Create array of objects with at least 10 students and find 9-
pointers.
Code:-
public class Main {
int i = 0;
int j = 0;
int k = 0;
int l = 0;
do {
System.out.println("Student Project Grades are:" + " " +
st.getProjectScore(k));
k++;
} while (k <= 14);
do {
System.out.println("Student Quiz Grades are:" + " " + st.getQuizScore(l));
l++;
} while (l <= 9);
17. Design a class named Rectangle to represent a rectangle. The class contains:
Two double data fields named width and height that specify the width and
height of the rectangle. The default values are 1 for both width and height.
(i)A default constructor that creates a default rectangle.
(ii)A constructor that creates a rectangle with the specified width and
height.
(iii)A method named getArea() that returns the area of this rectangle.
(iv)A method named getPerimeter() that returns the perimeter.
Implement the class. Write a test program that creates two Rectangle objects—
one with width 5 and height 50 and the other with width 2.5 and height 45.7.
Display the width, height, area, and perimeter of each rectangle in this order.
Code:-
public class RectangleDemo {
private class Rectangle {
private double height;
private double width;
private String color;
public Rectangle(double wid, double high){
height = high;
width = wid;
}
public Rectangle(){
height = 1;
width = 1;
color = "White";
}
public void setHeight(double high){
height = high;
}
public void setWidth(double wid){
width = wid;
}
public void setColor(String col){
color = col;
}
public double getArea(){
return height*width;
}
public double getPerimeter(){
return 2*(height + width);
}
public void getColor(){
System.out.println("Color is: " + color +"\n");
return;
}
}
18. Write a Java program that displays that displays the time in different formats
in the form of HH,MM,SS using constructor Overloading.
Code:-
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
try {
Date strToDate = format.parse(DateToStr);
System.out.println(strToDate);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
Output:-