1. Write a Program to mutiply 2 floating numbers ?
public static void floatMul(){
float a = 0.1f;
float b = 0.56f;
float c;
c = a*b;
System.out.println("multiplication of floating number is :"+c);
}
2. Write a program to swap two numbers without using third variable ?
public static void swapNum(){
int a=2,b=5;
a= a+b;
b= a-b;
a= a-b;
System.out.print("value of a is :"+a+" value of b is :"+b);
3.Write a java program to find if the number inputed is odd or even ?
public static void OddEven() throws IOException{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String s = bf.readLine();
System.out.println("entered line is :"+s);
int num = Integer.parseInt(s);
if(num!=0 || num!=1)
{
if(num%2==0)
{
System.out.println("the Entered number is Even");
}
else{
System.out.println("the Entered number is Odd");
}
}
4. Write a java Program to find the largest of 3 numbers ?
public static void largestNum() throws IOException{
int a[] = {34,23,21};
int largenum = 0;
if(a[0]>a[1])
largenum = a[0];
else
largenum = a[1];
if(largenum>a[2])
System.out.println("largest of 3 num is :"+largenum);
else
System.out.println("largest of 3 num is :"+a[2]);
}
by taking dynamic values :
public static void largestNum() throws IOException{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String s = reader.readLine();
String s1[] = s.split(" ");
int abc,i=0;
int a[] = new int[3];
for(String str : s1){
abc = Integer.parseInt(str);
a[i] = abc;
System.out.println("value at index"+i+" is :"+a[i]);
i++;
}
int largenum = 0;
if(a[0]>a[1])
largenum = a[0];
else
largenum = a[1];
if(largenum>a[2])
System.out.println("largest of 3 num is :"+largenum);
else
System.out.println("largest of 3 num is :"+a[2]);
}
by using Math function :
public static void largestNum() throws IOException{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String str = reader.readLine();
String s[] = str.split(" ");
int arr[] = new int[3];
int i=0;
for(String s1 : s)
{
int a = Integer.parseInt(s1);
arr[i] = a;
i++;
}
int largest = Math.max((Math.max(arr[0],arr[1])),arr[2]);
System.out.println(largest);
}
5. Write a program to display all prime numbers from 1 to N
public static void PrimeNum() throws IOException{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String str = reader.readLine();
int MaxNum = Integer.parseInt(str);
int arr[] = new int[MaxNum];
for(int i=2;i<=MaxNum;i++)
{
if(i ==2)
System.out.print(i);
else if(i%2 != 0)
System.out.print(i);
}
public static void PrimeNum() throws IOException{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String str = reader.readLine();
int MaxNum = Integer.parseInt(str);
int arr[] = new int[MaxNum];
for(int i=2;i<=MaxNum;i++)
{
if(i ==2)
System.out.print(i);
else if(i%2 != 0)
System.out.print(i);
}
String Related Programs
1. Write a Program to get a character from String ?
public static void getChar() throws IOException{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String str = reader.readLine();
char charArray[] = str.toCharArray();
System.out.println(charArray[5];);
}
2. Write a program to replace a character at a specific index ?
1. By Using string Class
public static void replaceChar() throws IOException{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String str = reader.readLine();
char charArray[] = str.toCharArray();
charArray[3] = 'F';
for(char c : charArray)
System.out.print(c);