Sei sulla pagina 1di 11

1.

Java Program to Calculate average of numbers using Array


public class JavaExample {

public static void main(String[] args) {


double[] arr = {19, 12.89, 16.5, 200, 13.7};
double total = 0;

for(int i=0; i<arr.length; i++){


total = total + arr[i];
}

double average = total / arr.length;

System.out.format("The average is: %.3f", average);


}
}

2. Java Program to display first 100 prime numbers


class PrimeNumberDemo
{
public static void main(String args[])
{
int n;
int status = 1;
int num = 3;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the value of n:");
n = scanner.nextInt();
if (n >= 1)
{
System.out.println("First "+n+" prime numbers are:");
System.out.println(2);
}

for ( int i = 2 ; i <=n ; )


{
for ( int j = 2 ; j <= Math.sqrt(num) ; j++ )
{
if ( num%j == 0 )
{
status = 0;
break;
}
}
if ( status != 0 )
{
System.out.println(num);
i++;
}
status = 1;
num++;
}
}
}

3.java program to display all the prime numbers between 1 and 100
class prime1To100
{
public static void main()
{
int i,k,j;
for(i=1;i<100;i++)
{
k=0;
for(j=2;j<i;j++)
{
if(i%j==0)
{
k=1
break;
}
}
if(k==0)
{
System.out.println(i);
}
}
}
}

4.Java Program to Reverse a String using Recursion


public class prog {

public static void main(String[] args) {


String str = "hello java";
String reversed = reverse_string(str);
System.out.println("The reversed string is: " + reversed);
}

public static String reverse_String(String str)


{
if (str.isEmpty())
return str;
return reverse_string(str.substring(1)) + str.charAt(0);
}

5.Java Program to Reverse a number using for, while loop and recursion
class ReverseNumberDemo
{
public static void main(String args[])
{
int num=123456789;
int reversenum =0;
while( num != 0 )
{
reversenum = reversenum * 10;
reversenum = reversenum + num%10;
num = num/10;
}

System.out.println("Reverse of specified number is: "+reversenum);


}
}

6.Java Program to Find Sum of Natural Numbers


public class SumNatural {
public static void main(String[] args) {

int num = 100, sum = 0;

for(int i = 1; i <= num; ++i)


{
sum += i;
}

System.out.println("Sum = " + sum);


}
}

7.Java Program to find ASCII value of a character


public class AsciiValue {

public static void main(String[] args) {

char ch = 'a';
int ascii = ch;

int castAscii = (int) ch;

System.out.println("The ASCII value of " + ch + " is: " + ascii);


System.out.println("The ASCII value of " + ch + " is: " + castAscii);
}
}

8.Java Program to read number from Standard Input


public class Demo {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);


System.out.print("Enter any number: ");

int num = scan.nextInt();

scan.close();

System.out.println("The number entered by user: "+num);


}
}

9.Java Program to add two binary numbers


public static String binaryAddition(String s1, String s2) {
if (s1 == null || s2 == null) return "";
int first = s1.length() - 1;
int second = s2.length() - 1;
StringBuilder sb = new StringBuilder();
int carry = 0;
while (first >= 0 || second >= 0) {
int sum = carry;
if (first >= 0) {
sum += s1.charAt(first) - '0';
first--;
}
if (second >= 0) {
sum += s2.charAt(second) - '0';
second--;
}
carry = sum >> 1;
sum = sum & 1;
sb.append(sum == 0 ? '0' : '1');
}
if (carry > 0)
sb.append('1');

sb.reverse();
return String.valueOf(sb);
}

10.Java Program to add two complex numbers


class Complex
{
int Real,Imag;
Complex()
{}

Complex(int Real1,int Imag1)


{
Real=Real1;
Imag=Imag1;
}

Complex AddComplex(Complex C1,Complex C2)


{
Complex CSum=new Complex();
CSum.Real=C1.Real+C2.Real;
CSum.Imag=C1.Imag+C2.Imag;
return CSum;
}
}

class Complexmain
{
public static void main(String[] a)
{
Complex C1=new Complex(4,8);
Complex C2=new Complex(5,7);
Complex C3=new Complex();
C3=C3.AddComplex(C1,C2);
System.out.println("SUM:" + C3.Real +"+i" + C3.Imag);
}
}

11.Java Program to convert decimal to hexadecimal


class DecimalToHexExample
{
public static void main(String args[])
{
Scanner input = new Scanner( System.in );
System.out.print("Enter a decimal number : ");
int num =input.nextInt();

int rem;

String str2="";

char hex[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};

while(num>0)
{
rem=num%16;
str2=hex[rem]+str2;
num=num/16;
}
System.out.println("Method 2: Decimal to hexadecimal: "+str2);
}
}

12.Java Program to Convert Decimal to Binary


import java.util.Scanner;

public class DecimalToBinary {

public String toBinary(int n) {


if (n == 0) {
return "0";
}
String binary = "";
while (n > 0) {
int rem = n % 2;
binary = rem + binary;
n = n / 2;
}
return binary;
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int decimal = scanner.nextInt();
DecimalToBinary decimalToBinary = new DecimalToBinary();
String binary = decimalToBinary.toBinary(decimal);
System.out.println("The binary representation is " + binary);

}
}

13.Java Program to Convert Binary to Decimal


class BinaryToDecimal
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);

System.out.println("Enter a binary number:");


int n=s.nextInt();
int decimal=0,p=0;

while(n!=0)
{
decimal+=((n%10)*Math.pow(2,p));
n=n/10;
p++;
}

System.out.println(decimal);
}
}

14.Java Program to Convert Decimal to Octal


class DecimalToOctalConversionClass{
public static void main(String[] args){

Scanner sc = new Scanner(System.in);

System.out.println("Enter Any Decimal Number :");

int input_decimal_num = sc.nextInt();


int octal_num = 0,rem,temp_input_decimal_num,i=1;
temp_input_decimal_num = input_decimal_num;

while(temp_input_decimal_num > 0){


//Get remainder
rem = temp_input_decimal_num%8;
octal_num = octal_num+rem*i;
temp_input_decimal_num = temp_input_decimal_num/8;
i=i*10;
}

System.out.println("Conversion of decimal to octal is : " + octal_num);


}
}

15.Java Program to Get IP Address


import java.net.InetAddress;

class GetMyIPAddress
{
public static void main(String args[]) throws Exception
{

InetAddress myIP=InetAddress.getLocalHost();

/* public String getHostAddress(): Returns the IP


* address string in textual presentation.
*/
System.out.println("My IP Address is:");
System.out.println(myIP.getHostAddress());
}
}

16. Java Program to find duplicate characters in a String


public class Demo {
public static void main(String[] args) {
String str = "topjavatutorial";
int count = 0;
char c;
Map<Character, Integer> map = new HashMap<Character, Integer>();
for (int i = 0; i < str.length(); i++) {
c = str.charAt(i);
if (map.containsKey(c)) {
count = map.get(c);
map.put(c, ++count);
} else {
map.put(c, 1);
}
}
System.out.println(map);
}
}

17. Java Program to check Palindrome string using Recursion


class PalindromeCheck
{

public static boolean isPal(String s)


{
if(s.length() == 0 || s.length() == 1)
return true;
if(s.charAt(0) == s.charAt(s.length()-1))

return isPal(s.substring(1, s.length()-1));

return false;
}

public static void main(String[]args)


{

Scanner scanner = new Scanner(System.in);


System.out.println("Enter the String for check:");
String string = scanner.nextLine();

if(isPal(string))
System.out.println(string + " is a palindrome");
else
System.out.println(string + " is not a palindrome");
}
}

18.Java Program to find Factorial of a number using Recursion


public class Factorial {

public static void main(String[] args) {


int num = 6;
long factorial = multiplyNumbers(num);
System.out.println("Factorial of " + num + " = " + factorial);
}
public static long multiplyNumbers(int num)
{
if (num >= 1)
return num * multiplyNumbers(num - 1);
else
return 1;
}
}

19.Java Program to Add the elements of an Array


class add{
public static void main(String args[]){
int[] array = {10, 20, 30, 40, 50, 10};
int sum = 0;
for( int num : array) {
sum = sum+num;
}
System.out.println("Sum of array elements is:"+sum);
}
}

20.Java Program to Calculate the area of Triangle


class right_angle {
public static void main(String args[]) {
double base = 20.0;
double height = 110.5;
double area = (base* height)/2;
System.out.println("Area of Triangle is: " + area);
}
}

21.Java Program to convert char Array to String


class array_to_string
{
public static void main(String args[])
{
//1
char[] ch = {'w','h','a','t','s',' ','u','p','p'};
String str = new String(ch);
System.out.println(str);

//2
String str2 = String.valueOf(ch);
System.out.println(str2);
}
}

22.Java Program to Convert char to String and String to Char


class char_to_string
{
public static void main(String args[])
{
// 1
char ch = 'a';
String str = Character.toString(ch);
System.out.println("String is: "+str);

// 2
String str2 = String.valueOf(ch);
System.out.println("String is: "+str2);
}
}
23.Java Program to check whether input character is vowel or consonant
public class check {

public static void main(String[] args) {

char ch = 'i';

if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' )


System.out.println(ch + " is vowel");
else
System.out.println(ch + " is consonant");

}
}

24.Java Program to perform Arithmetic Operation using Method Overloading


public class arith
{
int add(int num1, int num2)
{
return num1+num2;
}
int add(int num1, int num2, int num3)
{
return num1+num2+num3;
}
int add(int num1, int num2, int num3, int num4)
{
return num1+num2+num3+num4;
}
public static void main(String[] args)
{
JavaExample obj = new JavaExample();

System.out.println("Sum of two numbers: "+obj.add(10, 20));

System.out.println("Sum of three numbers: "+obj.add(10, 20, 30));

System.out.println("Sum of four numbers: "+obj.add(1, 2, 3, 4));


}
}

25.Java Program to find Area of Geometric figures using method overloading


class JavaExample
{
void calculateArea(float x)
{
System.out.println("Area of the square: "+x*x+" sq units");
}
void calculateArea(float x, float y)
{
System.out.println("Area of the rectangle: "+x*y+" sq units");
}
void calculateArea(double r)
{
double area = 3.14*r*r;
System.out.println("Area of the circle: "+area+" sq units");
}
public static void main(String args[]){
JavaExample obj = new JavaExample();

obj.calculateArea(6.1f);

obj.calculateArea(10,22);

obj.calculateArea(6.1);
}
}

26.Java Program to reverse words in a String


public class abc
{
public void reverse_word(String str)
{

String[] words = str.split(" ");


String reversedString = "";
for (int i = 0; i < words.length; i++)
{
String word = words[i];
String reverseWord = "";
for (int j = word.length()-1; j >= 0; j--)
{

reverseWord = reverseWord + word.charAt(j);


}
reversedString = reversedString + reverseWord + " ";
}
System.out.println(str);
System.out.println(reversedString);
}
public static void main(String[] args)
{
Example obj = new Example();
obj.reverse_word("Welcome to BeginnersBook");
obj.reverse_word("This is an easy Java Program");
}
}

27.Java Program to reverse an array


public class Example
{
public static void main(String args[])
{
int counter, i=0, j=0, temp;
int number[] = new int[100];
Scanner scanner = new Scanner(System.in);
System.out.print("How many elements you want to enter: ");
counter = scanner.nextInt();

for(i=0; i<counter; i++)


{
System.out.print("Enter Array Element"+(i+1)+": ");
number[i] = scanner.nextInt();
}
j = i - 1;
i = 0;
scanner.close();
while(i<j)
{
temp = number[i];
number[i] = number[j];
number[j] = temp;
i++;
j--;
}

System.out.print("Reversed array: ");


for(i=0; i<counter; i++)
{
System.out.print(number[i]+ " ");
}
}
}

Potrebbero piacerti anche