logo

Computer Science Quiz

Programming in JAVA


Programming in JAVA
Elements
Attributes

Html5

API
Graphics

Programming in JAVA
import java.util.Scanner;
class AddTwoMatrix{
   public static void main(String args[])   {
      int m, n, c, d;
      Scanner in = new Scanner(System.in);
      System.out.println("Enter the number of rows and columns");
      m = in.nextInt();
      n  = in.nextInt();
	  int first[][] = new int[m][n];
      int second[][] = new int[m][n];
      int sum[][] = new int[m][n];
      System.out.println("Enter the elements of first matrix");
 
      for (  c = 0 ; c < m ; c++ )
         for ( d = 0 ; d < n ; d++ )
            first[c][d] = in.nextInt();
 
      System.out.println("Enter the elements of second matrix");
 
      for ( c = 0 ; c < m ; c++ )
         for ( d = 0 ; d < n ; d++ )
            second[c][d] = in.nextInt();
 
      for ( c = 0 ; c < m ; c++ )
         for ( d = 0 ; d < n ; d++ )
             sum[c][d] = first[c][d] + second[c][d];  
      System.out.println("Sum of entered matrices:-");
 
      for ( c = 0 ; c < m ; c++ )      {
         for ( d = 0 ; d < n ; d++ )
            System.out.print(sum[c][d]+"\t");
 
         System.out.println();
      }
   }
}
class Armstrong{  
  public static void main(String[] args)  {  
    int c=0,a,temp;  
    int n=153;
    temp=n;  
    while(n>0){  
    a=n%10;  
    n=n/10;  
    c=c+(a*a*a);  
    }  
    if(temp==c)  
    System.out.println("armstrong number");   
    else  
        System.out.println("Not armstrong number");   
   }  
} 
import java.util.Scanner;
class LargestThreeNumbers{
   public static void main(String args[])   {
      int x, y, z;
      System.out.println("Enter three integers ");
      Scanner in = new Scanner(System.in);
      x = in.nextInt();
      y = in.nextInt();
      z = in.nextInt();
      if ( x > y && x > z )
         System.out.println("First number is largest.");
      else if ( y > x && y > z )
         System.out.println("Second number is largest.");
      else if ( z > x && z > y )
         System.out.println("Third number is largest.");
      else   
         System.out.println("Entered numbers are not distinct.");
   }
}
import java.util.Scanner;
public class CelsiusToFahrenheit {
public static void main(String[] args) {
	System.out.println("Enter a temperature in Celsius: ");
	Scanner in = new Scanner(System.in);
	double Fahrenheit = 0;
	if(in.hasNextDouble()){
		Fahrenheit = (in.nextDouble() + 32)*9/5;
	}
	System.out.println("Temperature in Fahrenheit is: "+Fahrenheit);
}}
class Stringcomparison{  
 public static void main(String args[]){  
   String s1="Durai";  
   String s2="Durai";  
   System.out.println(s1.equals(s2));
 }  
}  
package com.tutorialspoint;
import java.lang.*;
public class MathDemo {
   public static void main(String[] args) {
      double x = 25;
      double y = 10;
		System.out.println("Math.cbrt(" + x + ")=" + Math.cbrt(x));
      System.out.println("Math.cbrt(" + y + ")=" + Math.cbrt(y));
     
   }
}
class Factorial{  
 public static void main(String args[]){  
  int i,fact=1;  
  int number=5;    
  for(i=1;i<=number;i++){    
      fact=fact*i;    
  }    
  System.out.println("Factorial of "+number+" is: "+fact);    
 }  
}  
import java.util.*;
class FahrenheitToCelsius {
  public static void main(String[] args) {
    float temperatue;
    Scanner in = new Scanner(System.in);      
    System.out.println("Enter temperatue in Fahrenheit");
    temperatue = in.nextInt();
    temperatue = ((temperatue - 32)*5)/9;
 
    System.out.println("Temperatue in Celsius = " + temperatue);
  }
}
class Fibonacci{  
public static void main(String args[]){    
 int n1=0,n2=1,n3,i,count=10;    
 System.out.print(n1+" "+n2);
 for(i=2;i
	
import java.util.Scanner;
class Numofdigit {
	public static void main(String[] args){
		int no,a=0;
		Scanner s=new Scanner(System.in);
		System.out.println("Enter any number : ");
		no = s.nextInt();

	if(no<0){
		no=no * -1;
	}
	else if(no==0){
		no=1;
	}
	while(no>0){
		no=no/10;
		a++;
	}
		System.out.println("Number of Digits in given number is: "+a);
	}
}
import java.util.Scanner;
class LargestOfThreeNumbers{
   public static void main(String args[])   {
      int x, y, z;
      System.out.println("Enter three integers ");
      Scanner in = new Scanner(System.in);
      x = in.nextInt();
      y = in.nextInt();
      z = in.nextInt();
      if ( x > y && x > z )
         System.out.println("First number is largest.");
      else if ( y > x && y > z )
         System.out.println("Second number is largest.");
      else if ( z > x && z > y )
         System.out.println("Third number is largest.");
      else   
         System.out.println("Entered numbers are not distinct.");
   }
}
import java.util.*;
class Gcd {
public static void main(String args[])throws Exception{
	Scanner sc = new Scanner(System.in);
	System.out.print("Enter the First no : ");
	int n1=sc.nextInt();
	System.out.print("Enter the Second no : ");
	int n2=sc.nextInt();
	int r;
	while(n2 != 0){
		r = n1 % n2;
		n1 = n2;
		n2 = r;
	}
	System.out.print("GCD = "+n1);
}
}
import java.util.Scanner;
public class JavaProgram{
    public static void main(String args[])    {
        int a, b, x, y, t, hcf, lcm;
        Scanner scan = new Scanner(System.in);
	    System.out.print("Enter Two Number : ");
        x = scan.nextInt();
        y = scan.nextInt();
	    a = x;
        b = y;
	    while(b != 0){
            t = b;
            b = a%b;
            a = t;
        }
		
        hcf = a;
        lcm = (x*y)/hcf;
	    System.out.print("HCF = " +hcf);
        System.out.print("\nLCM = " +lcm);
    }
}
public class LeapYear{
 public static void main(String[] args) {
  int year = 2004;
	if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
		System.out.println("Year " + year + " is a leap year");
	else
		System.out.println("Year " + year + " is not a leap year");
        }
}
import java.io.*;
public class Test {
   public static void main(String args[]) {
      String Str1 = new String("Welcome to this app");
      String Str2 = new String("Java" );

      System.out.print("String Length :" );
      System.out.println(Str1.length());

      System.out.print("String Length :" );
      System.out.println(Str2.length());
   }
}
import java.util.Scanner;
class MatrixMultiplication{
   public static void main(String args[])   {
      int m, n, p, q, sum = 0, c, d, k;
      Scanner in = new Scanner(System.in);
      System.out.println("Enter the number of rows and columns of first matrix");
      m = in.nextInt();
      n = in.nextInt();
      int first[][] = new int[m][n];
      System.out.println("Enter the elements of first matrix");
      for ( c = 0 ; c < m ; c++ )
         for ( d = 0 ; d < n ; d++ )
            first[c][d] = in.nextInt();
      System.out.println("Enter the number of rows and columns of second matrix");
      p = in.nextInt();
      q = in.nextInt();
 
      if ( n != p )
         System.out.println("Error");
      else  {
         int second[][] = new int[p][q];
         int multiply[][] = new int[m][q];
         System.out.println("Enter the elements of second matrix");
         for ( c = 0 ; c < p ; c++ )
            for ( d = 0 ; d < q ; d++ )
               second[c][d] = in.nextInt();
 
         for ( c = 0 ; c < m ; c++ ) {
            for ( d = 0 ; d < q ; d++ ) {   
               for ( k = 0 ; k < p ; k++ ){
                  sum = sum + first[c][k]*second[k][d];
               }
 
               multiply[c][d] = sum;
               sum = 0;
            }
         }
         System.out.println("Product of entered matrices:-");
 
         for ( c = 0 ; c < m ; c++ ){
            for ( d = 0 ; d < q ; d++ )
               System.out.print(multiply[c][d]+"\t");
 
            System.out.print("\n");
         }
      }
   }
}
mport java.util.Scanner;
class CheckEvenOdd{
  public static void main(String args[]) {
    int num;
    System.out.println("Enter an Integer number:");

    Scanner input = new Scanner(System.in);
    num = input.nextInt();
    
    if ( num % 2 == 0 )
        System.out.println("Entered number is even");
     else
        System.out.println("Entered number is odd");
  }
}
import java.util.Scanner;
class Palindrome {
public static void main(String[] args){
int a,no,b,temp=0;
Scanner s=new Scanner(System.in);
System.out.println("Enter any num: ");
no=s.nextInt();
b=no;
while(no>0)
{
a=no%10;
no=no/10;
temp=temp*10+a;
}
if(temp==b)
{
System.out.println("Palindrome");
}
else
{
System.out.println("not Palindrome");
}
}
}
import java.util.Scanner;
public class JavaPascalTriangle{
    public static void main(String args[])	{
	    int r, i, k, number=1, j;
		Scanner scan = new Scanner(System.in);
		System.out.print("Enter Number of Rows : ");
		r = scan.nextInt();
		for(i=0;ii; k--)			{
				System.out.print(" ");
			}
            number = 1;
			for(j=0;j<=i;j++)			{
				 System.out.print(number+ " ");
                 number = number * (i - j) / (j + 1);
			}
			System.out.println();
		}
	}
}
import java.lang.*;
public class MathDemo {
   public static void main(String[] args) {
	double x = 2.0;
    double y = 5.4;
	System.out.println(Math.pow(x, y));
    System.out.println(Math.pow(y, x));
   }
}
class PrimeNumbers
{
   public static void main (String[] args){		
       int i =0;
       int num =0;
       String  primeNumbers = "";
       for (i = 1; i <= 100; i++){ 		  	  
          int counter=0; 	  
          for(num =i; num>=1; num--){
             if(i%num==0){
				counter = counter + 1;
			 }
		}
	  if (counter ==2)
	  {
	     primeNumbers = primeNumbers + i + " ";
	  }	
       }	
       System.out.println("Prime numbers from 1 to 100 are :");
       System.out.println(primeNumbers);
   }
}
class ReverseNumber{
   public static void main(String args[]){
      int n, reverse = 0;
 
      System.out.println("Enter the number to reverse");
      Scanner in = new Scanner(System.in);
      n = in.nextInt();
      while( n != 0 ){
          reverse = reverse * 10;
          reverse = reverse + n%10;
          n = n/10;
      }
 
      System.out.println("Reverse of entered number is "+reverse);
   }
}
class StringReverse{  
 public static void main(String args[]){
	String str = "Durai";
	char[] strArray = str.toCharArray();
	for (int i = strArray.length - 1; i >= 0; i--)
	{
		System.out.print(strArray[i]);  
	}
}
}
import java.util.Scanner;
public class SimpleInterest{
    public static void main(String args[])    {
        float p, r, t;
        Scanner s = new Scanner(System.in);
        System.out.print("Enter the Principal : ");
        p = s.nextFloat();
        System.out.print("Enter the Rate of interest : ");
        r = s.nextFloat();
        System.out.print("Enter the Time period : ");
        t = s.nextFloat();
        float si;
        si = (r * t * p) / 100;
        System.out.print("The Simple Interest is : " + si);
    }
}
import java.util.Scanner;
public class Order {
    public static void main(String[] args)     {
        int n, temp;
        Scanner s = new Scanner(System.in);
        System.out.print("Enter no. of elements you want:");
        n = s.nextInt();
        int a[] = new int[n];
        System.out.println("Enter all the elements:");
        for (int i = 0; i < n; i++){
            a[i] = s.nextInt();
        }
        for (int i = 0; i < n; i++){
            for (int j = i + 1; j < n; j++) 
            {
                if (a[i] > a[j]) 
                {
                    temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        }
        System.out.print("Ascending Order:");
        for (int i = 0; i < n - 1; i++) 
        {
            System.out.print(a[i] + ",");
        }
        System.out.print(a[n - 1]);
    }
}
class FindSquareRoot {
	public static void main(String[] args) {
		double n = 20;
		double x = 1;
		for (int i = 0; i < n; i++) {
			x = 0.5 * (x + n / x);
		}
		System.out.println("Square root of " + n + " is " + x);
		}
	}
public class SumOfDigits{
public static void main(String args[]) {
	Scanner sc = new Scanner(System.in);
	int number = sc.nextInt();
	int sum = 0;
	int input = number;
 while (input != 0) {
	int lastdigit = input % 10;
	sum += lastdigit; input /= 10;
 }
 System.out.printf("Sum of digits of number %d is %d", number, sum);
 }
}
public class Natural{
public static void main(String args[]) {
	int x, i = 1 ;
	int sum = 0;
	System.out.println("Enter Number of items :");
	Scanner s = new Scanner(System.in);
	x = s.nextInt();
	while(i <= x) {
		sum = sum +i;
		i++;
	}
	System.out.println("Sum of "+x+" numbers is :"+sum);
} 
}
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
    int n = 11;
    int[] firstArray = {8, 9, 2,5,1,9,5,9,5,4,7};
    int[] secondArray = {8, 9, 2,5,1,9,5,9,5,4,7};
    List<Object> total = new ArrayList<>();
    for(int i = 0; i < n; i++){
        int sum = firstArray[i] + secondArray[i];
        total.add(sum);
    }
    Integer[] sumArray = total.toArray(new Integer[total.size()]);
    for(int i = 0; i < n; i++) {
        System.out.print(sumArray[i] + " ");
    }
}
}
public static void main(String a[]){
	int x = 10;
	int y = 20;
	System.out.println("Before swap:");
	System.out.println("x value: "+x);
	System.out.println("y value: "+y);
	x = x+y;
	y=x-y;
	x=x-y;
	System.out.println("After swap:");
	System.out.println("x value: "+x);
	System.out.println("y value: "+y);
 }
import java.io.*;
public class Test {
   public static void main(String args[]) {
      String Str = new String("WELCOME");
     
      System.out.println(Str.toLowerCase() );
   }
}

Most Viewed Computer Science Links