0% found this document useful (0 votes)
12 views

Java Exp2

The document contains code snippets from 13 programming experiments: 1) Print "Hello World" 2) Convert a numeric string to an integer 3) Print addition of two integers 4) Take integers from command line and print their sum 5) Take two integers from command line, subtract smaller from greater and print result 6) Take n integers from command line and print their sum of products 7) Print sum of squares of two integers 8) Find square root of a positive integer using Heron's method 9) Sort and print names from command line in alphabetical order 10) Print total vowels and consonants in a given string 11) Check if two words are anagrams 12)

Uploaded by

Ayush Sonawane
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Java Exp2

The document contains code snippets from 13 programming experiments: 1) Print "Hello World" 2) Convert a numeric string to an integer 3) Print addition of two integers 4) Take integers from command line and print their sum 5) Take two integers from command line, subtract smaller from greater and print result 6) Take n integers from command line and print their sum of products 7) Print sum of squares of two integers 8) Find square root of a positive integer using Heron's method 9) Sort and print names from command line in alphabetical order 10) Print total vowels and consonants in a given string 11) Check if two words are anagrams 12)

Uploaded by

Ayush Sonawane
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Experiment no.

2.1
Aim : Write a program to print “Hello World”.
Code:

class HelloWorld {
public static void main(String[] args)
Output: { System.out.println("Hello,
World!");
}
}
Experiment no.
Aim : Write a program to convert a numeric string into int.
Code:
class HelloWorld {
public static void main(String[] args)
Output: { String characterNum = "12345";
int numberChanged = Integer.parseInt(characterNum);
System.out.println("Changed string into number is: " +
numberChanged);
}
}
Experiment no.
Aim : Write a program to print addition of two integers.
Code:
class HelloWorld {
public static void main(String[] args)
{ int a = 10;
Output: int b = 20;
int result = a + b;
System.out.println("Addition is: " +
result);
}
}
Experiment no.
Aim : Write a program to print addition of two integers input from
command line arguments.

Code:
public class assignment2_4 {
public static void main(String[] args) {
Output: int num1 =
Integer.parseInt(args[0]); int num2
= Integer.parseInt(args[1]); int
sum = num1 + num2;
System.out.println("Sum is: " +
sum);
}
Experiment no.
Aim : Write a program to take two integers from command line,
subtract the smaller number from the greater and print the result.

Code:
public class assignment2_5 {
public static void main(String[] args) {
Output: int num1 = Integer.parseInt(args[0]);
int num2 = Integer.parseInt(args[1]);
int result = 0;
if(num1 > num2){
result = num1 - num2;
}
else
{
result = num2 - num1;
}
System.out.println("Result is: " + result);
}
}
Experiment no.
Aim : Write a program to take n integers from command line
and print their sum of product (product of first number and
last number
added to product of second number and second last number and so on).
Code:
public class sumofproduct {
public static void main(String[] args) {

Output: int n = args.length;


int sumOfProducts = 0;
for(int i=0;i<n/2;i++){
int num1 = Integer.parseInt(args[i]);
int num2 = Integer.parseInt(args[n - 1 - i]);
int product = num1 * num2;
sumOfProducts += product;

}
System.out.println("Sum of Prodeuct is:" + sumOfProducts);
}
}
Experiment no.
Aim : Consider any two integers. Write a program to print sum of
their squares.
Code:
public class assignment2_7 {
Output: public static void main(String[] args) {
int num1 = 10;
int num2 = 20;
int result =
0;
result = (num1*num1)+(num2*num2);
System.out.println("Sum of square
is: " + result);
}
}
Experiment no.
Aim : Write a program to find square root of a given positive
integer using Heron’s method to find square root.

Code:
import java.util.Scanner;

Output:
public class assignment2_8 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number:-
"); double num = sc.nextDouble();
double guess = num/2;
double e =0.00001;
while(true){
double newguess =
(guess+num/guess)/2;
if(Math.abs(newguess-guess)<e)
break;
guess = newguess;
}
System.out.println("Square root:- " + guess);
}
}
Experiment No .
Aim : Write a program to sort and print the names of students
taken from command line in alphabetical order.

Code :
import java.util.*;
public class assignment2_9 {
public static void main(String[] args) {
Output : Arrays.sort(args);
System.out.println("Sorted array:- ");
for(String i : args){
System.out.println(i);
}
}
}
Experiment No .
Aim : Write a program to print total numbers of vowels
and consonants in a given string.
Code :
import java.util.Scanner;

public class assignment2_10 {


Output :
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a String:-
"); String str = sc.nextLine();
str=str.toLowerCase();
char[] a = str.toCharArray();
int vowels = 0;
int Continents = 0;
for(int i=0;i<a.length;i++){
if((a[i]=='a') || (a[i]=='e') || (a[i]=='i') || (a[i]=='o') ||
(a[i]=='u') )
vowels+
+; else
Continents++;
}
System.out.println("Vowels are: " + vowels + "\n Continents are: "
+ Continents);
}
}
Experiment No .
Aim : Given two English words, write a program to check if the first word is
anagram of the second word. ( An anagram is a word or phrase formed by
rearranging the letters of a different word or phrase, typically using all the
original letters exactly once. (Example: Anagram of TOM MARVOLO RIDDLE
is I AM LORD VOLDEMORT.)
Code :
import java.util.*;
public class assignment2_11 {
public static void main(String[] args) {
Output : Scanner sc = new Scanner(System.in);
System.out.println("Enter 2 strings:
"); String str = sc.nextLine();
String str2 =
sc.nextLine(); str =
str.toLowerCase(); str2 =
str2.toLowerCase();
char[] a1 = str.toCharArray();
char[] a2 =
str2.toCharArray();
Arrays.sort(a1);
Arrays.sort(a2);
if(Arrays.equals(a1,a2))
System.out.println("Anagram");
else
System.out.println("Not Anagram");
Experiment No .
Aim : Write a program to print a missing number in a sorted
integer array.

Code :
public class assignment2_12 {
public static void main(String[] args)
{ int[] arr = {0,1,2,3,4,5};
Output : int n = arr[arr.length-
1]; int sum =
(n*(n+1))/2;
for(int i=0;i<arr.length;i++){
sum = sum - arr[i];
}
System.out.println("Missing number is : " + sum);
}
}
Experiment No .
Aim : Write a program to find all the pairs of numbers on an
integer array whose sum is equal to a given number.

Code :
import java.util.Scanner;
public class
assignment2_13 {
Output :
public static void main(String[] args)
{ Scanner sc = new
Scanner(System.in); int[] arr =
{1,2,3,4,5,6,7,8,9};
int num = sc.nextInt();
for(int i=0;i<arr.length;i+
+){
for(int j = 0; j < arr.length;i+
+){ if(arr[i]+arr[j]==num){
System.out.println(arr[i] + " " + arr[j]);
}
}
}

You might also like