Program:: Q1. Write A Program That Accepts Two Strings As Command Line Arguments and Generate The Output
Program:: Q1. Write A Program That Accepts Two Strings As Command Line Arguments and Generate The Output
Program:: Q1. Write A Program That Accepts Two Strings As Command Line Arguments and Generate The Output
Write a Program that accepts two Strings as command line arguments and generate the output
in the required format.
Example1)
If the two command line arguments are Wipro and Bangalore then the output generated should be
Wipro Technologies Bangalore.
Example2)
If the command line arguments are ABC and Mumbai then the output generated should be ABC
Technologies Mumbai
PROGRAM:
public class CompanyLocation {
if (args.length != 2) {
return;
System.out.println(output);
OUTPUT:
Q2)Write a Program to accept a String as a command line argument and print a Welcome message
as given below.
Example1)
PROGRAM:
public class Two{
if (args.length != 1) {
return;
OUTPUT:
Q3 Write a Program to accept two integers as command line arguments and print the sum
of the two numbers
Example1)
C:\>java Sample 10 20
O/P Expected : The sum of 10 and 20 is 30
PROGRAM:
public class Three
{
public static void main(String args[])
{
int s1,s2,s3;
s1=Integer.parseInt(args[0]);
s2=Integer.parseInt(args[1]);
s3=s1+s2;
System.out.println("The sum of "+s1+" and "+s2+" is "+s3);
}
}
OUTPUT:
Q1) Write a program to check if a given integer number is Positive, Negative, or Zero.
PROGRAM:
public class One {
public static void main(String args[]) {
int number;
number=Integer.parseInt(args[0]);
if(number<0) System.out.println("negative");
else if(number==0) System.out.println("zero");
else System.out.println("positive");
}
}
OUTPUT:
Program:
public class Two {
public static void main(String args[]) {
int number;
number=Integer.parseInt(args[0]);
if(number%2==0)
System.out.println("even");
else
System.out.println("odd");
}
}
OUTPUT:
Q3) Write a program to check if the program has received command line arguments or not. If the
program has not received arguments then print "No Values", else print all the values in a single line
separated by ,(comma)
O/P: No values
O/P: Mumbai,Bangalore
[Hint: You can use length property of an array to check its length]
PROGRAM:
public class Three {
public static void main(String args[]) {
if(args.length==0) System.out.println("no values");
else
{
for(String i:args)
System.out.println(i+",");
}}}
OUTPUT:
Q4) Initialize two character variables in a program and display the characters in alphabetical order.
Example1) if the first character is 's' and second character is 'e' then the output should be e,s
Example2) if the first character is 'a' and second character is 'e', then the output should be a,e
PROGRAM:
public class Four {
public static void main(String args[]) {
char c1='a';
char c2='e';
if(c1>c2) System.out.println(c2+","+c1);
else System.out.println(c1+","+c2);
}
}
OUTPUT:
Q5): Initialize a character variable in a program and print 'Alphabhet' if the initialized value is an
alphabhet,
OUTPUT:
Q6) Write a program to accept gender ("Male" or "Female") and age from command line arguments
and print the percentage of interest based on the given conditions.
If the gender is 'Female' and age is between 1 and 58, the percentage of interest is 8.2%.
If the gender is 'Female' and age is between 59 and 100, the percentage of interest is 9.2%.
If the gender is 'Male' and age is between 1 and 58, the percentage of interest is 8.4%.
If the gender is 'Male' and age is between 59 and 100, the percentage of interest is 10.5%.
PROGRAM:
public class Six {
public static void main(String args[]) {
String s1;
s1=args[0];
int age;
age=Integer.parseInt(args[1]);
OUTPUT:
Q7) Initialize a character variable with an alphabhet in a program. If the character value is in
lowercase, the output should be displayed in uppercase in the following format.
Example1) i/p:a o/p:a->A If the character value is in uppercase, the output should be
displayed in lowercase in the following format.
Example2) i/p:A o/p:A->a
PROGRAM:
public class Seven {
public static void main(String args[]) {
char c='A';
if(Character.isLowerCase(c))
System.out.println(Character.toUpperCase(c));
else
System.out.println(Character.toLowerCase(c));
}
}
OUTPUT:
Q8) Write a program to receive a color code from the user (an Alphabhet). The program
should then print the color name, based on the color code given.
The following are the color codes and their corresponding color names. R->Red, B->Blue, G-
>Green, O->Orange, Y->Yellow, W->White.
If color code provided by the user is not valid then print "Invalid Code".
PROGRAM:
import java.util.Scanner;
public class Eight {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
char c=sc.next().charAt(0);
sc.close();
if(c=='r') System.out.println("red");
else if(c=='b') System.out.println("blue");
else if(c=='g') System.out.println("green");
else if(c=='o') System.out.println("orange");
else if(c=='y') System.out.println("yellow");
else if(c=='w') System.out.println("white");
else System.out.println("invalid code");
}
}
OUTPUT:
Q9) Write a program to receive a number and print the corresponding month name.
Example1)
C:\>java Sample 12
O/P Expected : December
Example2) C:\>java Sample
O/P Expected : Please enter the month in numbers
Example3) C:\>java Sample 15
O/P Expected : Invalid month
PROGRAM:
import java.util.Scanner;
public class Nine {
public static void main(String args[]) {
int number;
Scanner sc=new Scanner(System.in);
String[] months={"jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"};
number=sc.nextInt();
sc.close();
System.out.println(months[number-1]);
}
}
OUTPUT:
Q10) Write a program to print numbers from 1 to 10 in a single row with one tab space.
PROGRAM:
public class Ten {
public static void main(String args[]) {
for(int i=1;i<=10;i++) System.out.println("\t"+i);
}
}
OUTPUT:
11) Write a program to print even numbers between 23 and 57. Each number should be
printed in a separate row.
PROGRAM:
public class Eleven {
public static void main(String args[]) {
for(int i=23;i<=57;i++)
{
if(i%2==0) System.out.println(i);
}
}
}
OUTPUT:
12) Write a program to check if a given number is prime or not.
PROGRAM:
import java.util.Scanner;
public class Twelve {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
int number=sc.nextInt();
sc.close();
int fi=0;
for(int i=2;i<=number/2;i++)
{
if(number%i==0) fi=1;
}
if(fi==0) System.out.println("prime");
else System.out.println("not prime");
}
}
OUTPUT:
13) Write a program to print prime numbers between 10 and 99.
PROGRAM:
public class Thirteen {
public static void main(String args[]) {
for(int i=10;i<=99;i++)
{
int f=0;
for(int j=2;j<=i/2;j++)
{
if(i%j==0)
{
f=1;break;
}
}
if(f==0) System.out.println(i);
}
}
}
OUTPUT:
14) Write a program to print the sum of all the digits of a given number.
Example1) I/P:1234
O/P:10
PROGRAM:
import java.util.Scanner;
public class Fifteen {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
sc.close();
int ans=0;
String s=""+n;
for(int i=0;i<s.length();i++)
{
int d=Character.getNumericValue(s.charAt(i));
ans+=d;
//System.out.println(ans);
}
System.out.println(ans);
}
}
OUTPUT:
15) Write a program to print * in Floyds format (using for and while loop)
*
**
***
Example1) C:\>java Sample
O/P: Please enter an integer number
Example2) C:\>java Sample 3
O/P :
*
**
***
PROGRAM:
import java.util.Scanner;
public class Sixteen {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
sc.close();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print("*");
}
System.out.println();
}
}
}
OUTPUT:
16) Write a program to reverse a given number and print
Example1)
I/P: 1234
O/P:4321
Example2)
I/P:1004
O/P:4001
PROGRAM:
import java.util.Scanner;
public class Seventeen{
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
sc.close();
StringBuffer s=new StringBuffer(String.valueOf(n));
System.out.println(s.reverse());
}
}
OUTPUT:
17) Write a Java program to find if the given number is palindrome or not
Example1) C:\>java Sample 110011
O/P: 110011 is a palindrome
Example2) C:\>java Sample 1234
O/P: 1234 is not a palindrome
PROGRAM:
import java.util.Scanner;
public class Eighteen {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
sc.close();
int number=0;
int temp=n;
while(n>0)
{
int t=n%10;
number=number*10+t;
n/=10;
}
if(temp==number) System.out.println("palindrome");
else System.out.println("not palindrome");
}
}
OUTPUT:
1) Write a program to initialize an integer array and print the sum and average of the array.
PROGRAM:
public class One {
public static void main(String[] args) {
Integer [] arr={1,2,3,4};
int sum=0;
for(int i:arr)
{
sum+=i;
}
System.out.println(sum);
float avg=(float)sum/(float)arr.length;
System.out.println(avg);
}
}
OUTPUT:
2) Write a program to initialize an integer array and find the maximum and minimum value of the
array.
PROGRAM:
import java.util.Arrays;
import java.util.*;
public class Two {
public static void main(String[] args) {
Integer[] arr={1,5,7,23};
int min=Collections.min(Arrays.asList(arr));
int max=Collections.max(Arrays.asList(arr));
System.out.println("min value = "+ min);
System.out.println("max value = "+max);
}
}
OUTPUT:
3) Write a program to initialize an integer array with values One dimensional and check if a
given number is present in the array or not. If the number is not found, it will print -1 else it
will print the index value of the given number in the array.
Example 1) If the Array elements are {1,4,34,56,7} and the search element is 90, then the
output expected is -1.
Example 2)If the Array elements are {1,4,34,56,7} and the search element is 56, then the
output expected is 3.
PROGRAM:
import java.util.*;
public class Three {
public static void main(String[] args) {
int[] arr={1,5,2,6,22};
System.out.println("enter search value");
Scanner sc=new Scanner(System.in);
int search=sc.nextInt();
sc.close();
int index=-1;
for(int i=0;i<arr.length;i++)
{
if(arr[i]==search) index=i;
}
if(index==-1) System.out.println("-1");
else System.out.println(index);
}
}
OUTPUT:
4) Initialize an integer array with ascii values and print the corresponding character values in
a single row.
PROGRAM:
public class Four {
public static void main(String[] args) {
int[] arr={65,66,90,97,98,99,122};
for(int c:arr)
System.out.println((char)c)
}
}
OUTPUT:
5) Write a program to find the largest 2 numbers and the smallest 2 numbers in the
given array.
PROGRAM:
public class Five {
public static void main(String[] args) {
int[] arr={-1,1,6,99,1287,44,678,33,77,88};
int flarge=Integer.MIN_VALUE,slarge=Integer.MIN_VALUE;
int fsmall=Integer.MAX_VALUE,ssmall=Integer.MAX_VALUE;
for(int i:arr) if(i>flarge) flarge=i;
for(int i:arr) if(i>slarge && i!=flarge) slarge=i;
for(int i:arr) if(i<fsmall) fsmall=i;
for(int i:arr) if(i<ssmall && i!=fsmall) ssmall=i;
System.out.println("first large = "+flarge);
System.out.println("second large = "+slarge);
System.out.println("first small = "+fsmall);
System.out.println("second small = "+ssmall);
}
}
OUTPUT:
6) Write a program to initialize an array and print them in a sorted order.
PROGRAM:
import java.util.Arrays;
public class Six {
public static void main(String[] args) {
int[] arr={6,2,7,2,8,88,22,0};
Arrays.sort(arr);
for(int i:arr) System.out.print(i+" ");
System.out.println();
}
}
OUTPUT:
7) Write a program to remove the duplicate elements in an array and print the same.
Example) I/P:{12,34,12,45,67,89}
O/P:{12,34,45,67,89}
PROGRAM:
import java.util.Arrays;
public class Seven {
public static void main(String[] args) {
int[ ] arr={1,1,100,2,3,4,5,99,6,6,6,7,9};
Arrays.sort(arr);
int ele=arr[0];
System.out.println(ele);
for(int i=1;i<arr.length;i++)
{
if(ele!=arr[i]) System.out.println(arr[i]);
ele=arr[i];
}
}
}
OUTPUT:
PROGRAM:
import java.util.Scanner;
public class Eight {
public static void main(String[] args) {
int arr[]=new int[100];
Scanner sc=new Scanner(System.in);
System.out.println("no of elements");
int n=sc.nextInt();
for(int i=0;i<n;i++) arr[i]=sc.nextInt();
sc.close();
int left=6,right=7;
int lindex=-1,rindex=-1;
for(int i=0;i<arr.length;i++)
{
if(arr[i]==left) lindex=i;
if(arr[i]==right) rindex=i;
}
int sum=0;
if(lindex<rindex)
{
for(int i=lindex;i<=rindex;i++)
{
arr[i]=0;
}
for(int i:arr) sum+=i;
}
else
{
for(int i:arr)
{
sum+=i;
}
}
System.out.println(sum);
}
}
OUTPUT:
9) Return a version of the given array where all the 10's have been removed. The remaining
elements should shift left towards the start of the array as needed, and the empty spaces a
the end of the array should be 0. So {1, 10, 10, 2} yields {1, 2, 0, 0}. You may modify and
return the given array or make a new array.
PROGRAM:
public class RemoveTen {
if (nums[i] != 10) {
copy[j] = nums[i];
j++;
return copy;
System.out.println(result[i]+" ");
}
OUTPUT:
10) Return an array that contains the exact same numbers as the given array, but
rearranged so that all the even numbers come before all the odd numbers. Other than that,
the numbers can be in any order. You may modify and return the given array, or make a
new array.
PROGRAM:
public int[] evenOdd(int[] nums) {
int i = 0;
while(i < nums.length && nums[i] % 2 == 0)
i++;
for(int j = i + 1; j < nums.length; j++) {
if(nums[j] % 2 == 0) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
i++;
}
}
return nums;
}
OUTPUT:
PROGRAM:
public class Solution {
public boolean only14(int[] nums) {
for (int i = 0; i < nums.length; i++) {
if (nums[i] != 1 && nums[i] != 4) {
return false;
}
}
return true;
}
// Test cases
int[] test1 = {1, 4, 1, 4};
int[] test2 = {1, 4, 2, 4};
int[] test3 = {1, 1, 1, 1};
int[] test4 = {4, 4, 4, 4};
int[] test5 = {1, 4, 3, 4};
OUTPUT:
12) Given 2 int arrays, a and b, each length 3, return a new array length 2 containing their middle
elements.
OUTPUT:
PROGRAM:
public class Nine {
public static void main(String[] args) {
int n1=Integer.parseInt(args[0]);
int n2=Integer.parseInt(args[1]);
int n3=Integer.parseInt(args[2]);
int n4=Integer.parseInt(args[3]);
System.out.println("given array is");
System.out.println(n1+" "+n2);
System.out.println(n3+" "+n4);
int temp;
temp=n1;
n1=n4;
n4=temp;
temp=n2;
n2=n3;
n3=temp;
System.out.println("reversed array is");
System.out.println(n1+" "+n2);
System.out.println(n3+" "+n4);
}
}
OUTPUT:
OUTPUT:
MINI PROJECT
PROGRAM:
public class Project1 {
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("Please provide an employee ID as a command line argument.");
return;
}
int empId = Integer.parseInt(args[0]);
int[] empNos = {1001, 1002, 1003, 1004, 1005, 1006, 1007};
String[] empNames = {"Ashish", "Sushma", "Rahul", "Chahat", "Ranjan", "Suman",
"Tanmay"};
String[] joinDates = {"01/04/2009", "23/08/2012", "12/11/2008", "29/01/2013",
"16/07/2005", "1/1/2000", "12/06/2006"};
String[] designations = {"e", "c", "k", "r", "m", "e", "c"};
String[] departments = {"R&D", "PM", "Acct", "Front Desk", "Engg", "Manufacturing",
"PM"};
int[] basics = {20000, 30000, 10000, 12000, 50000, 23000, 29000};
int[] HRAs = {8000, 12000, 8000, 6000, 15000, 9000, 12000};
int[] ITs = {3000, 9000, 1000, 2000, 4400, 1000, 2000};
String[] designationCodes = {"e", "c", "k", "r", "m"};
String[] designationNames = {"Engineer", "Consultant", "Clerk", "Receptionist",
"Manager"};
int[] DAs = {20000, 32000, 12000, 15000, 40000};
int empIndex = -1;
for (int i = 0; i < empNos.length; i++) {
if (empNos[i] == empId) {
empIndex = i;
break;
}
}
if (empIndex == -1) {
System.out.println("There is no employee with empid: " + empId);
} else {
int empNo = empNos[empIndex];
String empName = empNames[empIndex];
String department = departments[empIndex];
String designationCode = designations[empIndex];
int basic = basics[empIndex];
int HRA = HRAs[empIndex];
int IT = ITs[empIndex];
String designation = "";
int DA = 0;
for (int i = 0; i < designationCodes.length; i++) {
if (designationCodes[i].equals(designationCode)) {
designation = designationNames[i];
DA = DAs[i];
break;
}
}
int salary = basic + HRA + DA - IT;
System.out.println("Emp No.\tEmp Name\tDepartment\tDesignation\tSalary");
System.out.println(empNo + "\t" + empName + "\t" + department + "\t" + designation
+ "\t" + salary);
}}}
OUTPUT: