Week1-4
Week1-4
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.
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:
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:
2
if (n <= 1) {
return n;
}
return fibrec(n - 1) + fibrec(n - 2);
}
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:
SOURCE CODE:
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:
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/-
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:
OUTPUT:
7
Program 2. Write a Java program to sort the elements using bubble sort.
SOURCE CODE:
import java.util.Scanner;
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;
if (!found) {
System.out.println("Element not found.");
}
scanner.close();
}
}
9
OUTPUT:
import java.util.Scanner;
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:
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:
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);
}
OUTPUT:
14
Program 3. Write a Java program that counts the number of objects created by using
static variable.
SOURCE CODE:
Staticcnt() {
cnt++;
}
OUTPUT:
15
WEEK : 4
List of Programs:
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:
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;
OUTPUT:
Program 4: Write a Java program for sorting a given list of names in ascending order.
SOURCE CODE:
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;
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;
19
int sum = 0;
while (st.hasMoreTokens()) {
String token = st.nextToken();
int number = Integer.parseInt(token);
System.out.println(number);
sum += number;
}
OUTPUT:
20