0% found this document useful (0 votes)
22 views20 pages

Week1-4

Uploaded by

pgggg622
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views20 pages

Week1-4

Uploaded by

pgggg622
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

WEEK:1

List of Programs:

1. Write a Java program that prints all real solutions to the quadratic equation ax2 + bx +
c=0. Read in a, b, c and use the quadratic formula. If the discriminant b2 – 4ac is
negative, display a message stating that there are no real solutions.

2. The Fibonacci sequence is defined by the following rule. The first two values in the
sequence are 1 and 1. Every subsequent values is the sum of the two values preceding it.
Write a java program that uses both recursive and non-recursive functions to print the nth
value in the fibonacci sequence.

3. Write a Java program that prompts the user for an integer and then prints out all prime
numbers up to that integer.

4. Write a Java program that prints the following pattern


a. ******
b. *****
c. ****
d. ***
e. **
f. *
5. Write a Java program that calculate mathematical constant ‘e’ using the formula
e=1+1/2!+1/3!+........ up to 5 .

1
Program 1. Write a Java program that prints all real solutions to the quadratic
equation ax2 + bx + c=0. Read in a, b, c and use the quadratic formula. If the
discriminant b2 – 4ac is negative, display a message stating that there are no real
solutions.

SOURCE CODE:

public class Quadratic{


public static void main(String[] args) {
System.out.println("Name: Paras Garg Roll No: 22103116");
int a=Integer.parseInt(args[0]);
int b=Integer.parseInt(args[1]);
int c=Integer.parseInt(args[2]);
double d = b * b - 4 * a * c, x,y;
if (d > 0) {
x = (-b + Math.sqrt(d)) / (2 * a);
y = (-b - Math.sqrt(d)) / (2 * a);
System.out.println("real and distinct roots: " + x + " and " + y);
}
else if (d == 0) {
x = -b / (2 * a);
System.out.println("real and repeated roots: " + x);
}
else
System.out.println("There are no real roots.");
}}

OUTPUT:

Program 2. The Fibonacci sequence is defined by the following rule. The first two values
in the sequence are 1 and 1. Every subsequent values is the sum of the two values
preceding it. Write a java program that uses both recursive and non-recursive functions
to print the nth value in the Fibonacci sequence.
SOURCE CODE:

public class Fibonacci {


public static int fibrec(int n) {

2
if (n <= 1) {
return n;
}
return fibrec(n - 1) + fibrec(n - 2);
}

public static int fibitr(int n) {


if (n <= 1) {
return n;
}
int prev = 0, cur = 1;
for (int i = 2; i <= n; i++) {
int next = prev + cur;
prev = cur;
cur = next;
}
return cur;
}

public static void main(String[] args) {


int n = 10;
System.out.println("Name: Paras Garg Roll No: 22103116");

System.out.println("Fibonacci number (Recursive)" + n + " is: " + fibrec(n));

System.out.println("Fibonacci number (Non-Recursive)" + n + " is: " + fibitr(n));


}}

OUTPUT:

Program 3. Write a Java program that prompts the user for an integer and then
prints out all prime numbers up to that integer.

SOURCE CODE:

class Prime {
static int isPrime(int n){
if(n<=1)
return 0;

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

3
return 0;
}
return 1;
}
public static void main(String[] args) {
System.out.println("Name: Paras Garg Roll No: 22103116");
int b=Integer.parseInt(args[1]);

for(int i=2;i<b;i++){
if(isPrime(i) ==1)
System.out.println(i);
}
}
}

OUTPUT:

Program 4. Write a Java program that prints the following pattern


a. ******
b. *****
c. ****
d. ***
e. **
f. *

SOURCE CODE:

public class StarPat{

public static void main(String[] args) {


System.out.println("Name: Paras Garg Roll No: 22103116");
int rows = 6;
for (int i = rows; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();

4
}}}

OUTPUT:

Program 5. Write a Java program that calculate mathematical constant ‘e’ using the
formula e=1+1/2!+1/3!+........ up to 5 .

SOURCE CODE:

public class CalE {


public static void main(String[] args) {
System.out.println("Name: Paras Garg Roll No: 22103116\n");
double e = 1.0;
for (int i = 2; i <= 5; i++) {
e += 1.0 / factorial(i);
}

System.out.println("The value of e up to 5 terms is: " + e);


}
public static double factorial(int n) {
double fact = 1.0;
for (int i = 1; i <= n; i++) {
fact *= i;
}
return fact;
}}

OUTPUT:

5
WEEK : 2
List of Programs:

1. Write a java program to calculate gross salary & net salary taking the following data.
Input: empno, empname, basic
Process:
DA=50%of basic
HRA=25%of basic
CCA=Rs240/-
PF=10%of basic
PT=Rs100/-

2. Write a Java program to sort the elements using bubble sort.

3. Write a Java program to search an element using binary search.

4. Write a Java program multiplication of two 3X3 matrices.

6
Program 1. Write a java program to calculate gross salary & net salary taking the
following data. Input: empno, empname, basic.

Process:
DA = 50% of basic
HRA = 25%
CCA = Rs240/-
PF = 10% of basic
PT = Rs. 100/-

SOURCE CODE:

public class Salary {


public static void main(String[] args) {
System.out.println("Name: Paras Garg Roll No: 22103116\n");

int empNo = Integer.parseInt(args[0]);


int basic = Integer.parseInt(args[2]);

double da = 0.50 * basic;


double hra = 0.25 * basic;
double cca = 240.0;

double pf = 0.10 * basic;


double pt = 100.0;

double grossSalary = basic + da + hra + cca;


double netSalary = grossSalary - pf - pt;

System.out.println("\nEmployee Number: " + empNo);


System.out.println("Employee Name: " + args[1]);
System.out.println("Basic Salary: Rs " + basic);
System.out.println("Gross Salary: Rs " + grossSalary);
System.out.println("Net Salary: Rs " + netSalary);
}
}

OUTPUT:

7
Program 2. Write a Java program to sort the elements using bubble sort.

SOURCE CODE:

import java.util.Scanner;

public class Bubble {


public static void main(String[] args) {
System.out.println("Name: Paras Garg Roll No: 22103116\n");
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of elements: ");
int n = scanner.nextInt();
int[] arr = new int[n];

System.out.println("Enter the elements:");


for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}

for (int i = 0; i < n - 1; i++) {


for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}

System.out.println("Sorted array:");
for (int i : arr) {
System.out.print(i + " ");
}

scanner.close();
}
}

OUTPUT:

8
Program 3. Write a Java program to search an element using binary search.

SOURCE CODE:

import java.util.Scanner;

public class Bsearch {


public static void main(String[] args) {
System.out.println("Name: Paras Garg Roll No: 22103116\n");
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of elements: ");
int n = scanner.nextInt();
int[] arr = new int[n];

System.out.println("Enter the sorted elements:");


for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}

System.out.print("Enter the element to search: ");


int key = scanner.nextInt();

int low = 0, high = n - 1;


boolean found = false;

while (low <= high) {


int mid = (low + high) / 2;
if (arr[mid] == key) {
found = true;
System.out.println("Element found at index: " + mid);
break;
} else if (arr[mid] < key) {
low = mid + 1;
} else {
high = mid - 1;
}
}

if (!found) {
System.out.println("Element not found.");
}

scanner.close();
}
}

9
OUTPUT:

Program 4. Write a Java program multiplication of two 3X3 matrices.


SOURCE CODE:

import java.util.Scanner;

public class MatrixMul {


public static void main(String[] args) {
System.out.println("Name: Paras Garg Roll No: 22103116\n");
Scanner scanner = new Scanner(System.in);
int[][] matrix1 = new int[3][3];
int[][] matrix2 = new int[3][3];
int[][] result = new int[3][3];

System.out.println("Enter elements of first 3x3 matrix:");


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
matrix1[i][j] = scanner.nextInt();
}
}

System.out.println("Enter elements of second 3x3 matrix:");


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
matrix2[i][j] = scanner.nextInt();
}
}

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


for (int j = 0; j < 3; j++) {
result[i][j] = 0;
for (int k = 0; k < 3; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}

10
System.out.println("Resultant matrix after multiplication:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(result[i][j] + " ");
}
System.out.println();
}

scanner.close();
}
}

OUTPUT:

11
WEEK : 3
List of Programs:

1. Write a Java program that displays area of different Figures (Rectangle, Square, Triangle)
using the method overloading.

2. Write a Java program that displays that displays the time in different formats in the form of
HH,MM,SS using constructor Overloading.

3. Write a Java program that counts the number of objects created by using static variable.

12
Program 1. Write a Java program that displays area of different Figures (Rectangle,
Square, Triangle) using the method overloading.

SOURCE CODE:

public class Area {


double area(double length, double breadth) {
return length * breadth;
}

double area(double side) {


return side * side;
}

double area(double base, double height, boolean istriangle) {


return 0.5 * base * height;
}

public static void main(String[] args) {


Area calculator = new Area();
System.out.println("Name: Paras Garg Roll No: 22103116\n");

System.out.println("Area of Rectangle: " + calculator.area(5.0, 3.0));


System.out.println("Area of Square: " + calculator.area(4.0));
System.out.println("Area of Triangle: " + calculator.area(6.0, 2.0, true));
}
}

OUTPUT:

Program 2. Write a Java program that displays that displays the time in different
formats in the form of HH,MM,SS using constructor Overloading.

SOURCE CODE:

public class Time {


int hrs,min,sec;

Time(int h, int m, int s) {


hrs = h;

13
min = m;
sec = s;
}

Time(int h, int m) {
hrs = h;
min = m;
sec = 0;
}

Time(int h) {
hrs = h;
min = 0;
sec = 0;
}

void displayTime() {
System.out.printf("%02d:%02d:%02d\n", hrs, min, sec);
}

public static void main(String[] args) {


System.out.println("Name: Paras Garg Roll No: 22103116\n");
Time t1 = new Time(12, 45, 40);
Time t2 = new Time(10, 25);
Time t3 = new Time(6);

System.out.println("Time in HH:MM:SS format:");


t1.displayTime();
t2.displayTime();
t3.displayTime();
}
}

OUTPUT:

14
Program 3. Write a Java program that counts the number of objects created by using
static variable.

SOURCE CODE:

public class Staticcnt {


private static int cnt = 0;

Staticcnt() {
cnt++;
}

static int getCount() {


return cnt;
}

public static void main(String[] args) {


System.out.println("Name: Paras Garg Roll No: 22103116\n");

Staticcnt obj1 = new Staticcnt();


Staticcnt obj2 = new Staticcnt();
Staticcnt obj3 = new Staticcnt();

System.out.println("Number of objects created: " + Staticcnt.getCount());


}
}

OUTPUT:

15
WEEK : 4

List of Programs:

1. Write a Java program that reverses a given String.

2. Write a Java program that checks whether a given string is a palindrome or not.

3. Write a Java program to count the frequency of words, characters in the given line of text.

4. Write a Java program for sorting a given list of names in ascending order.

5. Write a Java program that reads a line of integers separated by commas and then displays
each integer and fund the sum of the integers (using String Tokeniser).

16
Program 1: Write a Java program that reverses a given String.
SOURCE CODE:

public class reverse {


public static void main(String[] args) {
System.out.println("Name: Paras Garg Roll No: 22103116\n");
String str = "Hello, World!";
String ans = "";

for (int i = str.length() - 1; i >= 0; i--) {


ans += str.charAt(i);
}
System.out.println(ans);
}}

OUTPUT:

Program 2: Write a Java program that checks whether a given string is a palindrome or
not.
SOURCE CODE:

class Palindrome {
public static void pal(String str){
int j =str.length() - 1;
for(int i=0;i<=str.length()/2;i++){
if(str.charAt(i) != str.charAt(j)){
System.out.println(str+": Not a Palindrome");
break;
}
if(i == j){
System.out.println(str+": Palindrome");
}
j--;
}}
public static void main(String[] args) {
pal("racecar");
pal("raceacar");
}}

17
OUTPUT:

Program 3: Write a Java program to count the frequency of words, characters in the
given line of text.
SOURCE CODE:

import java.util.HashMap;
import java.util.Map;

public class freqcw {


public static void main(String[] args) {
System.out.println("Name: Paras Garg Roll No: 22103116\n");
String input = "hellpo 3478bfjnj";
Map<Character, Integer> charc = new HashMap<>();
Map<String, Integer> wordc= new HashMap<>();

for (char c : input.toCharArray()) {


charc.put(c, charc.getOrDefault(c, 0) + 1); }

for (String word : input.split("\\s+")) {


wordc.put(word, wordc.getOrDefault(word, 0) + 1);
}
System.out.println("Character Frequency: " + charc);
System.out.println("Word Frequency: " + wordc);
}}

OUTPUT:

Program 4: Write a Java program for sorting a given list of names in ascending order.
SOURCE CODE:

public class SortNames{

18
public static void main(String[] args) {
System.out.println("Name: Paras Garg Roll No: 22103116\n");
String[] str = {"Ram","Raj","Sham","Ajay"};
String temp;

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


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

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


System.out.println(str[i]);
}
}
}

OUTPUT:

Program 5: Write a Java program that reads a line of integers separated by commas
and then displays each integer and fund the sum of the integers (using String
Tokeniser).
SOURCE CODE:

import java.util.StringTokenizer;

public class SumIntegers {


public static void main(String[] args) {
System.out.println("Name: Paras Garg Roll No: 22103116\n");
String line = "2,3,5,6,7,9,8,7,55,44";
StringTokenizer st = new StringTokenizer(line, ",");

19
int sum = 0;

while (st.hasMoreTokens()) {
String token = st.nextToken();
int number = Integer.parseInt(token);
System.out.println(number);
sum += number;
}

System.out.println("Sum: " + sum);


}
}

OUTPUT:

20

You might also like