Write a program in JAVA To find the sum of any number of integers entered as command line arguments

Write a program in JAVA To find the sum of any number of integers entered as command line arguments

Share:

Write a program in JAVA to find GCD of two number

    


Write a program in JAVA to find gcd of two number

import java.util.Scanner;

class GCD {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.print("Enter First Number : ");
int num1=sc.nextInt();
System.out.print("Enter Second Number : ");
int num2=sc.nextInt();
sc.close();
while (num1!=num2) {
if (num1>num2) {
num1=num1-num2;
} else {
num2=num2-num1;
}
}
System.out.print("GCD = "+num1);
}
}

OUTPUT

Enter First Number : 24
Enter Second Number : 78
GCD : 6
Share:

Write a program in Java to find and print all the roots of quadratic equation

  


Write a program in JAVA to find and print all the roots of quadratic equation

import java.util.Scanner;

public class QuadEq {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double a, b, c, val1, val2;
System.out.print("Enter The value of a in ax^2+bx+c : ");
a = sc.nextDouble();
System.out.print("Enter The value of b in ax^2+bx+c : ");
b = sc.nextDouble();
System.out.print("Enter The value of c in ax^2+bx+c : ");
c = sc.nextDouble();
sc.close();
double dis = Math.pow(b, 2) - 4 * a * c;
if (dis == 0) {
val1 = (-b) / (2 * a);
System.out.print("Roots are Real & Equal. X1 = x2 = : " + val1);
} else if (dis > 0) {
val1 = (-b + Math.sqrt(dis)) / (2 * a);
val2 = (-b - Math.sqrt(dis)) / (2 * a);
System.out.print("Roots are Real & Different. X1 = : " + val1 + " & X2 = " + val2);
} else {
val1 = -b / (2 * a);
val2 = (Math.sqrt(-dis)) / (2 * a);
System.out.println("Roots are Complex & Different.");
System.out.println("X1 = " + val1 + " + " + val2 + "i" + "\nX2 = " + val1 + " - " + val2 + "i");
}

}
}

OUTPUT

Enter The value of a in ax^2+bx+c : 1
Enter The value of b in ax^2+bx+c : -4
Enter The value of c in ax^2+bx+c : 6.25
Roots are Complex & Different.
X1 = 2.0 + 1.5i
X2 = 2.0 - 1.5i
Share:

Write a program in java to reverse the digit of given number

   


Write a program in JAVA to Reverse the digit of given number

import java.io.*;
public class RevNum {
public static void main(String[] args)throws IOException {
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Number");
int num = Integer.parseInt(obj.readLine());
int rem,rnum=0;
while (num!=0) {
rem=num%10;
rnum=rnum*10+rem;
num=num/10;
}
System.out.println("Reversed number is :"+rnum);
}
}

OUTPUT

Enter Number
12345
Reversed number is :54321
Share:

Write a program in Java to input and print 3x3 Matrix

  


Write a program in JAVA to input and print 3X3 Matrix

import java.io.*;

public class Matrix {
public static void main(String[] args) throws IOException {
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter elements of 3X3 matrix : ");
int arr[][] = new int[3][3];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
arr[i][j] = Integer.parseInt(obj.readLine());
}
}
System.out.println("Entered 3x3 Matrix is : ");
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}

OUTPUT

Enter elements of 3X3 matrix : 12
34
56
23
45
67
11
32
87

Entered 3x3 Matrix is :
12 34 56
23 45 67
11 32 87


Share:

Write a program in java to print sum of two 3X3 matrix

  


Write a program in JAVA to print sum of two 3X3 MATRIX

import java.io.*;

class SumMatrix3D {
public static void main(String[] args)throws IOException {
BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter elements of first 3X3 matrix : ");
int arr1[][]=new int[3][3];
for (int i = 0; i < arr1.length; i++) {
for (int j = 0; j < arr1.length; j++) {
arr1[i][j]=Integer.parseInt(obj.readLine());
}
}
System.out.print("Enter elements of second 3X3 matrix : ");
int arr2[][]=new int[3][3];
for (int i = 0; i < arr1.length; i++) {
for (int j = 0; j < arr1.length; j++) {
arr2[i][j]=Integer.parseInt(obj.readLine());
}
}
int sum[][]=new int[3][3];
for (int i = 0; i < arr1.length; i++) {
for (int j = 0; j < arr1.length; j++) {
sum[i][j]=arr1[i][j]+arr2[i][j];
}
}
System.out.println("Sum Of both Matrix : ");
for (int i = 0; i < arr1.length; i++) {
for (int j = 0; j < arr1.length; j++) {
System.out.print(sum[i][j]+" ");
}
System.out.println();
}
}
}

OUTPUT

Enter elements of first 3X3 matrix : 1
2
3
4
5
6
7
8
9
Enter elements of second 3X3 matrix : 9
2
3
4
5
1
6
7
8
Sum Of both Matrix : 
10 4 6 
8 10 7 
13 15 17 
Share:

Write a program in JAVA to input a number and check if it is armstrong or not

   

Write a program  in JAVA to input a number and check if it is  armstrong or not

Armstrong Number : Armstrong number is a number that is equal to the sum of cubes of its digits. For example 0, 1, 153, 370, 371 and 407 are the Armstrong numbers

import java.io.*;
public class Armstrong {
public static void main(String[] args)throws IOException {
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a Positive Number : ");
int num=Integer.parseInt(obj.readLine());
int ldigit,cube=0;
int temp=num;
while (temp!=0) {
ldigit=temp%10;
cube+=ldigit*ldigit*ldigit;
temp=temp/10;
}
if (cube == num) {
System.out.println(num+" Is Armstrong Number");
} else {
System.out.println(num+" Is Not an Armstrong Number");
}
}
}


OUTPUT

1. 
     Enter A Number : 371
     371 is armstrong number 

2. 
     Enter A Number : 476
     476 is not a armstrong number



Share:

Pageviews