Sachin Mishra Assignment-01 2023ugcs118
Week-01
Assignment-01
Date-22-08-2025
1. Java Programming Basics
a) Write a program in java to initialize an integer value n and compute 4096n , (1/1024)n by using shift
operator.
Code:-
import java.util.*;
class shift{
public static void main(String[] args){
Scanner ok=new Scanner(System.in);
int n=ok.nextInt();
System.out.println("4096n: "+(n<<12));
System.out.println("1/1024n: "+(n>>10));
ok.close();
}
}
b) Write a program to perform following convertions:
i) Reads a number in feet, converts it to meters.
Code:-
import java.util.Scanner;
class feet{
public static void main(String[] args){
Scanner ok=new Scanner(System.in);
System.out.print("Enter feet: ");
double feet=ok.nextDouble();
Sachin Mishra Assignment-01 2023ugcs118
System.out.println("meter: "+feet*0.3048);
ok.close();
}
}
ii) Enter the minutes (e.g., 1 billion) and displays the number of years and days for the minutes.
Code:-
import java.util.Scanner;
class minutes{
public static void main(String[] args){
Scanner ok=new Scanner(System.in);
System.out.print("Enter minutes: ");
int n=ok.nextInt();
int remyr=n/(1440*365);
int day=n%(1440*365);
System.out.println("Year: "+remyr+", day: "+day/1440);
ok.close();
}
}
c) Write a program in java to create and initialize base and height of a triangle and compute its perimeter.
Code:-
import java.util.Scanner;
class triangle{
public static void main(String[] args){
Scanner ok=new Scanner(System.in);
System.out.print("Enter base ");
double base=ok.nextDouble();
System.out.print("Enter height ");
Sachin Mishra Assignment-01 2023ugcs118
double height=ok.nextDouble();
double peri=base+height+Math.sqrt(base*base+height*height);
System.out.println("perimeter: "+peri);
ok.close();
}
}
d) Write a java program to apply mod operation for each entry and display multiplication table of modulo n.
The example for multiplication table modulo 4 is given below.
Code:-
import java.util.Scanner;
class modulo{
public static void main(String[] args){
Scanner ok=new Scanner(System.in);
System.out.print("Enter n: ");
int n=ok.nextInt();
System.out.print(n+" ");
for(int i=0;i<n;i++) System.out.print(i+" ");
System.out.println();
for(int i=0;i<n;i++){
System.out.print(i+" ");
for(int j=0;j<n;j++){
Sachin Mishra Assignment-01 2023ugcs118
System.out.print((i*j)%n+" ");
}
System.out.println();
}
ok.close();
}
}
e) Write a program to input center and radius of a circle with an arbitrary point and print whether the point
is inside, outside, or on the boundary of the circle.
Code:-
import java.util.Scanner;
class circle{
public static void main(String[] args){
Scanner ok=new Scanner(System.in);
System.out.print("Enter coordinates of centre: ");
int x_coor=ok.nextInt();
int y_coor=ok.nextInt();
System.out.print("Enter radius: ");
int rad=ok.nextInt();
System.out.print("Enter coordinates of point: ");
int x=ok.nextInt();
int y=ok.nextInt();
int d=(x-x_coor)*(x-x_coor)+(y-y_coor)*(y-y_coor);
if(d<rad*rad) System.out.println("inside");
else if(d==rad*rad) System.out.println("on");
else System.out.println("outside");
ok.close();
}
Sachin Mishra Assignment-01 2023ugcs118
f) Write a java program to input two vector of length n using array and compute the dot product using for-
each loop.
Code:-
import java.util.Scanner;
class dot{
public static void main(String[] args) {
Scanner ok=new Scanner(System.in);
System.out.println("Enter the size of the vectors:");
int n=ok.nextInt();
int vector1[] = new int[n];
System.out.println("Enter the elements of the first vector:");
for(int i = 0; i < n; i++) {
vector1[i] = ok.nextInt();
}
int vector2[] = new int[n];
System.out.println("Enter the elements of the second vector:");
for(int i = 0; i < n; i++) {
vector2[i] = ok.nextInt();
}
int ans = 0;
int j = 0;
for(int i : vector1){
ans += i * vector2[j++];
}
System.out.println("The Dot Product Is : " +ans);
ok.close();
}
Sachin Mishra Assignment-01 2023ugcs118
g) Write a program to input endpoints of two lines and display whether two lines are parallel or intersecting
each other.
Code:-
import java.util.Scanner;
class parallel{
public static void main(String[] args){
Scanner ok=new Scanner(System.in);
System.out.print("Enter coordinates of first point of line 1 (x1 y1): ");
int x1=ok.nextInt();
int y1=ok.nextInt();
System.out.print("Enter coordinates of second point of line 2 (x2 y2): ");
int x2=ok.nextInt();
int y2=ok.nextInt();
System.out.print("Enter coordinates of first point of line 1 (x1 y1): ");
int xx1=ok.nextInt();
int yy1=ok.nextInt();
System.out.print("Enter coordinates of second point of line 2 (x2 y2): ");
int xx2=ok.nextInt();
int yy2=ok.nextInt();
double slope1 = (double)(y2 - y1) / (x2 - x1);
double slope2 = (double)(yy2 - yy1) / (xx2 - xx1);
if (slope1 == slope2) {
System.out.println("The lines are parallel.");
} else {
System.out.println("The lines are not parallel.");
}
ok.close();
Sachin Mishra Assignment-01 2023ugcs118
}
}
h) Write a program in java to input an integer n and approximate the value of e using formula e = 1+ 1/1!
+1/2! ... + 1/n!
Code:-
import java.util.Scanner;
class e{
public static void main(String[] args){
Scanner ok=new Scanner(System.in);
System.out.print("Enter n: ");
int n=ok.nextInt();
int fac=1;
double sum=1.0;
for(int i=1;i<=n;i++){
fac*=i;
sum+= (double)1/fac;
}
System.out.println("the value of e predicted till n: " + sum);
ok.close();
}
}