Java Journal Program Unit - 1
UNIT - 1
//PROGRAM - 21/***********************************Write a program to remove duplicates from an array in Java.***********************************/import java.util.*;
class program21{ public static void main(String[] args) { Scanner obj = new Scanner(System.in);
int a[] = new int[5]; int flag = 0; int n = a.length; for (int i = 0;i < n; i++) { System.out.print("Enter Any No. :"); a[i] = obj.nextInt(); } for (int i = 0;i < n; i++) { for (int j = i + 1;j < n; j++) { if (a[i] == a[j]) {
a[j] = a[n - 1]; n--; j--; } } } int arry2[] = Arrays.copyOf(a,n); for (int i = 0;i < arry2.length; i++) { System.out.println(arry2[i]); } }}OUTPUT:Enter Any No. :20Enter Any No. :20Enter Any No. :23Enter Any No. :22Enter Any No. :20202223_______________________________________________________//PROGRAM - 20/************************************Design and Develop a java program toread marks of a student and print thetotal and average of marks usingscanner class.*************************************/
import java.util.*;
class program20{ public static void main(String[] args) { Scanner obj = new Scanner(System.in); int m1, m2, m3, total; double avg; System.out.print("Enter marks1 : "); m1 = obj.nextInt();
System.out.print("Enter marks2 : "); m2 = obj.nextInt();
System.out.print("Enter marks3 : "); m3 = obj.nextInt();
total = m1 + m2 + m3; avg = total / 3;
System.out.println("Total : " + total); System.out.println("Average : " + avg); }}OUTPUT:Enter marks1 : 88Enter marks2 : 80Enter marks3 : 85Total : 253Average : 84.0_______________________________________________________
//PROGRAM- 19/***************************************Write a Java program that checks whethera given string is a palindrome or not. Ex: MADAM is a palindrome?***********************************/import java.util.*;
class program19{ public static void main(String[] args) { Scanner obj = new Scanner(System.in);
System.out.print("Enter String : "); String str = obj.nextLine(); String rev = ""; int length = str.length();
for (int i = length - 1; i >= 0 ;i--) rev = rev + str.charAt(i); if (str.equals(rev)) { System.out.println(str + " is palindrome"); } else { System.out.println(str + " is not palindrome"); } }}OUTPUT:Enter String : MADAMMADAM is palindrome_______________________________________________________//PROGRAM - 18/************************************Write a Java program that prompts theuser for an integer and then printsoutall the prime numbers up to that Integer?************************************/
import java.util.Scanner;
class program18{ public static void main(String[] args) { int n; int p; Scanner s=new Scanner(System.in); System.out.print("Enter a number: "); n=s.nextInt(); for(int i=2;i<n;i++) { p=0; for(int j=2;j<i;j++) { if(i%j==0) p=1; } if(p==0) System.out.println(i); } }}OUTPUT:Enter a number: 202357111317_______________________________________________________//PROGRAM - 17/**************************************Write a Java program to replace a string "python" with "java" and "java"with "python" in a given string.Expected Output:Input the string:python is more propular than javaNew string:java is more propular than python**************************************///import java.io.*;import java.util.*;
class program17{ public static void main(String[] args) { Scanner obj = new Scanner(System.in);
System.out.println("Input the String"); String str1 = obj.nextLine(); str1 = str1.replaceAll("java","py_thon"); str1 = str1.replaceAll("python","java"); str1 = str1.replaceAll("py_thon","python");
System.out.println("New string : \n" + str1); }}OUTPUT:python is more propular than javaNew string :java is more propular than python_______________________________________________________//PROGRAM - 16/************************************Write a Java program to take the last three characters from a given stringand add the three characters at both the front and back of the string.String length must be greater than three and more.Test data: "Python" will be "honPythonhon"Sample Output:honPythonhon************************************/import java.util.*;import java.io.*;
public class program16{ public static void main(String[] args) { String str1 = "Python"; int slength = 3;
if (slength > str1.length()) { slength = str1.length(); } String subpart = str1.substring(str1.length() - 3); System.out.println(subpart + str1 + subpart); }}OUTPUT:honPythonhon_______________________________________________________ //PROGRAM - 15/********************************************Write a Java program to capitalize thefirst letter of each word in a sentence.Sample Output:Input a Sentence:the quick brown fox jumps over the lazy dog.The Quick Brown Fox Jumps Over The Lazy Dog.********************************************/import java.util.*;
class program15{ public static void main(String[] args) { Scanner obj = new Scanner(System.in);
System.out.println("Input a Sentence:"); String line = obj.nextLine();
String upper_case_line = ""; Scanner lineScan = new Scanner(line); while(lineScan.hasNext()) { String word = lineScan.next(); upper_case_line += Character.toUpperCase(word.charAt(0)) + word.substring(1) + " "; } System.out.println(upper_case_line.trim()); }}OUTPUT:Input a Sentence:the quick brown fox jumps over the lazy dog.The Quick Brown Fox Jumps Over The Lazy Dog._______________________________________________________//PROGRAM - 13
/*****************************************Write a Java program to test if a given number (positive integer ) is a perfect square or not.
Expected Output:Input a positive integer: 6Is the said number perfect square? false*****************************************/import java.util.*;class program13{ public static void main(String[] args) { int n, flag = 0; Scanner obj = new Scanner(System.in);
System.out.print("Enter Any Number : "); n = obj.nextInt();
for (int i = 1;i <= 100; i++) { if (i * i == n) { flag = 1; } } if (flag == 1) { System.out.println(n + " Number is perfect square"); } else { System.out.println(n + " Number is not perfect square"); } }}OUTPUT:Enter Any Number : 2525 Number is perfect square OR
Enter Any Number : 1212 Number is not perfect square _______________________________________________________//PROGRAM - 12
/****************************************Write a Java program to accept a positive number and repeatedly add all its digitsuntil the result has only one digit.****************************************/
import java.util.*;
class program12{ public static void main(String[] args) { Scanner obj = new Scanner(System.in);
System.out.print("Enter Any Number : "); int n = obj.nextInt();
while(n > 9) { int sum_digit = 0; while(n != 0) { sum_digit += n % 10; n /= 10; } n = sum_digit; } System.out.println("Sum of digits : " + n); }}OUTPUT:Enter Any Number : 789Sum of digits : 6____________________________________________________________________//PROGRAM - 11/********************************************Write a Java program to print numbers between1 to 100 which are divisible by 3, 5 and by both.
Sample Output:Divided by 3:3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36,39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99,
Divided by 5:5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95,
Divided by 3 & 5:15, 30, 45, 60, 75, 90,*********************************************/
class program11{ public static void main(String[] args) { int i; System.out.println("Divided by 3 : "); for (i = 1; i <= 100; i++) { if (i % 3 == 0) { System.out.print(i + ","); } } System.out.print("\n\n\n"); System.out.println("Divided by 5 : "); for (i = 1; i <= 100; i++) { if (i % 5 == 0) { System.out.print(i + ","); } } System.out.print("\n\n\n"); System.out.println("Divided by 3 & 5 : "); for (i = 1; i <= 100; i++) { if (i % 3 == 0 && i % 5 == 0) { System.out.print(i + ","); } } }}OUTPUT:Divided by 3 :3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87,90,93,96,99,
Divided by 5 :5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100,
Divided by 3 & 5 :15,30,45,60,75,90,_______________________________________________________//PROGRAM - 10
/********************************************Write a Java program to compute the number oftrailing zeros in a factorial.7! = 5040, therefore the output should be 1********************************************/
import java.util.Scanner;public class program10 { public static void main(String[] arg) {
Scanner in = new Scanner(System.in); System.out.print("Input a number: "); int n = in.nextInt(); int n1 = n; long ctr = 0; while (n != 0) { ctr += n / 5; n /= 5; } System.out.printf("Number of trailing zeros of the factorial %d is %d ",n1,ctr); System.out.printf("\n"); } }_______________________________________________________//PROGRAM - 9
/******************************Write a Java program to converta decimal number to binary number.Input Data:Input a Decimal Number : 5Expected OutputBinary number is: 101****************************/
import java.util.*;class program9{ public static void main(String[] args) { int dec_num, i = 1; int bin_num[] = new int[100];
Scanner obj = new Scanner(System.in);
System.out.print("Input a Decimal Number : "); dec_num = obj.nextInt();
while(dec_num != 0) { bin_num[i++] = dec_num % 2; dec_num = dec_num / 2; } System.out.print("Binary number is : "); for (int j = i - 1; j > 0; j--) { System.out.print(bin_num[j]); } System.out.println("\n"); }}OUTPUT:
Input a Decimal Number : 5Binary number is : 101_______________________________________________________
//PROGRAM - 8
/****************************
Write a Java program to check
whether an given integer is a
power of 4 or not.
Given num = 64, return true. Given num = 6, return false.
*****************************/
import java.util.*;
class program8
{
public static void main(String[] args) {
Scanner obj = new Scanner(System.in);
int i, no1, no2;
System.out.print("Enter Any Number : ");
no2 = obj.nextInt();
int flag = 0;
for (i = 1; i <= 100 ; i ++ ) {
no1 = i * i * i * i;
if (no2 == no1) {
flag = 1;
}
}
if (flag == 1) {
System.out.println(no2 + " is power of 4");
}
else
{
System.out.println(no2 + " is not power of 4");
}
}
}
//Program - 7
/***********************
Write a Java program to
multiply two given int-
egers without using the
multiply operator(*).
***********************/
import java.util.*;
class program7
{
public static void main(String[] args) {
Scanner obj = new Scanner(System.in);
int no1, no2;
int mul = 0, i;
System.out.print("Enter Value of A : ");
no1 = obj.nextInt();
System.out.print("Enter Value of B : ");
no2 = obj.nextInt();
for (i = 0; i < no2; i++) {
mul = add(mul, no1);
}
System.out.println("multiplication : " + mul);
}
static int add(int num1,int num2){
for(int i=0; i<num2; i++)
num1++;
return num1;
}
}
_______________________________________________________
//PROGRAM - 6
/**********************************
Write a Java program to print the
ascii value of a given character.
Expected Output
The ASCII value of Z is :90
**********************************/
import java.util.*;
class program6
{
public static void main(String[] args) {
char ch;
Scanner obj = new Scanner(System.in);
System.out.print("Enter any Character : ");
ch = obj.next().charAt(0); // for get A character.
int ascii = ch;
System.out.print("ASCII value of " + ch + " is " + ascii);
}
}
/***********************************
Write a Java program to convert seconds
to hour, minute and seconds.
Sample Output:
Input seconds: 86399
23:59:59
************************************/
import java.util.*;
class program5
{
public static void main(String[] args) {
int h, m, s , r;
Scanner obj = new Scanner(System.in);
System.out.print("Enter Seconds : ");
s = obj.nextInt();
h = s / 3600;
r = s % 3600;
m = r / 60;
s = r % 60;
System.out.println(h + ":" + m + ":" + s );
}
}
OUTPUT:
_______________________________________________________
//PROGRAM - 4
/***********************************
Write a Java program to find the
numbers greater than the average of
the numbers of a given array.
***********************************/
import java.util.*;
class program4
{
public static void main(String[] args) {
Integer nums[] = new Integer[]{ 1, 2, 4, 45, 56, 67, 87, 22, 32, 25};
int sum = 0;
System.out.println("Arrays : " + Arrays.toString(nums));
for (int i = 0; i < nums.length ;i++ ) {
sum = sum + nums[i];
}
double avg = sum / nums.length;
System.out.println("Sum of Array : " + sum);
System.out.println("Average : " + avg);
for (int i = 0; i < nums.length ;i++ ) {
if(avg < nums[i])
System.out.println("Greater numbers of than average : " + nums[i]);
}
}
}
OUTPUT:
Arrays : [1, 2, 4, 45, 56, 67, 87, 22, 32, 25]
Sum of Array : 341
Average : 34.0
Greater numbers of than average : 45
Greater numbers of than average : 56
Greater numbers of than average : 67
Greater numbers of than average : 87
_______________________________________________________
//PROGRAM - 3
/**********************************
Write a Java program to add two
numbers without using any arithmetic
operators.
Given x = 10 and y = 12; result = 22
***********************************/
class math
{
int Add(int x, int y)
{
if (y == 0)
return x;
else
return Add( x ^ y, (x & y) << 1);
}
}
class program3
{
public static void main(String[] args) {
math obj = new math();
System.out.println("Addition : " + obj.Add(10, 15));
}
}
OUTPUT:
Addition : 25
_______________________________________________________
//PROGRAM - 2
/**************************************
Write a Java program to print the sum
(addition), multiply, subtract, divide
and remainder of two numbers.
***************************************/
class program2
{
public static void main(String[] args) {
int a = 5, b = 6, c;
c = a + b;
System.out.println("Addition = " + c);
c = a - b;
System.out.println("Subtraction = " + c);
c = a * b;
System.out.println("multiplication = " + c);
c = a / b;
System.out.println("divition = " + c);
c = a % b;
System.out.println("Remainder = " + c);
}
}
OUTPUT:
Addition = 11
Subtraction = -1
multiplication = 30
divition = 0
Remainder = 5
_______________________________________________________
/**********************************************
Write a Java program to print 'Hello World' on
screen and then print your name on a separate
line.
***********************************************/
//PROGRAM - 1
class program1
{
public static void main(String[] args) {
System.out.println("Hello World");
System.out.println("Milan Bakotra");
}
}
OUTPUT:
Hello World
Milan Bakotra
_______________________________________________________
Good
ReplyDelete